本文操作按照《c&c++开源库编译指南》中内容规范编写,编译环境配置、工具下载、目录规划,及更多其他开源库编译方法请参考该文章。
c&c++开源库编译指南:https://blog.youkuaiyun.com/binary0006/article/details/144086155
本文章中的源代码已提交到gitee仓库,地址:https://gitee.com/binary0010/depends/tree/master/c/libiconv-1.11.1
1.libiconv 概述
libiconv 是一个广泛使用的字符编码转换库,由 GNU 项目开发并维护,旨在解决不同字符编码之间的转换问题,确保软件在多语言环境下能正确处理文本数据。
从技术实现上看,libiconv 支持多种常见的字符编码,如 ASCII、UTF - 8、GBK、ISO - 8859 等。它提供了一套简洁的 API 接口,包括 iconv_open 用于打开一个编码转换描述符,iconv 进行实际的编码转换操作,以及 iconv_close 关闭转换描述符。这些接口可以方便地集成到各种 C 或 C++ 程序中,也可通过包装器供其他编程语言调用。编译时,libiconv 具有良好的跨平台性,能在 Linux、Windows、macOS 等多种操作系统上进行编译,并且依赖较少,通常只需标准的 C 库支持。
在应用场景方面,libiconv 具有广泛的用途。在 Web 开发中,当服务器接收来自不同客户端的多语言数据时,可使用 libiconv 将其转换为统一的编码格式进行处理和存储;在文本处理工具中,如编辑器、翻译软件等,libiconv 能帮助用户在不同编码的文本文件之间进行转换。在开源社区,libiconv 基于 GPL 许可发布,代码托管在 GNU 的官方仓库。社区活跃度高,持续对其进行维护和更新,修复潜在的漏洞,添加对新编码格式的支持,拥有大量的用户和开发者群体,被众多知名项目所采用。
2.libiconv编译
2.1.源代码下载
libiconv源代码可以在gun的官方ftp上下载,源代码下载地址:https://ftp.gnu.org/pub/gnu/libiconv/,根据网上查到的资料显示支持vs编译最后一个版本为1.11.1版本,该版本的发布日期是2007-12-10,虽然看着有点老旧,实践测试功能是可以正常使用的,这里我们会使用vs2008来进行编译,选这个低版本也可以避免在vs2008中编译报错。

也可以自己复制这个地址下载:https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.11.1.tar.gz
2.2.windows编译
先解压源代码到指定目录,libiconv-1.11.1版本中中没有提供CMakeLists.txt编译脚步,提供了makefile文件,在windows上可以使用nmake来构建和编译,但是实际操作已经通过网上查阅的资料,在编译过程中始终会出现问题。这里我们自己编译CMakeLists.txt脚本来构建。

2.2.1.源代码分析
2.2.1.1.makfile文件分析
在编译CMakeLists.txt脚本之前我们先分析一下源代码目录中的Makefile.msvc,他会编译libcharsets、lib、srclib、src、po、man、tests等多个子工程,还会拷贝vs特化的一些头文件iconv.h.msvc-static|iconv.h.msvc-shared、config.h.msvc。
实际上以sdk方式做开发时,只需要libcharsets和lib子工程编译的库就可以了,后面根据Makefile.msvc文件的分析编写我们规范的CMakeLists.txt脚本文件,同时还会对源代码做部分修改。

2.2.1.2.源代码修改
针对libiconv源代码修改,实际上是新增源代码下lib、libcharset工程中需要使用的头文件,共有四个,后面会给出每个文件的源代码:
| libiconv-1.11.1/include/iconv.h libiconv-1.11.1/lib/config.h libiconv-1.11.1/libcharset/config.h libiconv-1.11.1/libcharset/include/localcharset.h |
libiconv-1.11.1/include/iconv.h代码:
/* Copyright (C) 1999-2003, 2005-2006 Free Software Foundation, Inc.
This file is part of the GNU LIBICONV Library.
The GNU LIBICONV Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
The GNU LIBICONV Library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU LIBICONV Library; see the file COPYING.LIB.
If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301, USA. */
/* When installed, this file is called "iconv.h". */
#ifndef _LIBICONV_H
#define _LIBICONV_H
#define _LIBICONV_VERSION 0x010B /* version number: (major<<8) + minor */
#if defined(__GNUC__) && !defined(__MINGW32__)
# if HAVE_VISIBILITY && BUILDING_LIBICONV
# define LIBICONV_DLL_EXPORTED __attribute__((__visibility__("default")))
# else
# define LIBICONV_DLL_EXPORTED
# endif
#elif defined(__MINGW32__)
#elif defined(_MSC_VER)
# include <Windows.h>
# ifdef ICONV_DLL
# ifdef BUILDING_LIBICONV
# define LIBICONV_DLL_EXPORTED __declspec(dllexport)
# else
# define LIBICONV_DLL_EXPORTED __declspec(dllimport)
# endif // ICONV_DLL
# else
# define LIBICONV_DLL_EXPORTED
# endif
#endif
extern LIBICONV_DLL_EXPORTED int _libiconv_version; /* Likewise */
/* We would like to #include any system header file which could define
iconv_t, 1. in order to eliminate the risk that the user gets compilation
errors because some other system header file includes /usr/include/iconv.h
which defines iconv_t or declares iconv after this file, 2. when compiling
for LIBICONV_PLUG, we need the proper iconv_t type in order to produce
binary compatible code.
But gcc's #include_next is not portable. Thus, once libiconv's iconv.h
has been installed in /usr/local/include, there is no way any more to
include the original /usr/include/iconv.h. We simply have to get away
without it.
Ad 1. The risk that a system header file does
#include "iconv.h" or #include_next "iconv.h"
is small. They all do #include <iconv.h>.
Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It
has to be a scalar type because (iconv_t)(-1) is a possible return value
from iconv_open().) */
/* Define iconv_t ourselves. */
#undef iconv_t
#define iconv_t libiconv_t
typedef void* iconv_t;
/* Get size_t declaration.
Get wchar_t declaration if it exists. */
#include <stddef.h>
/* Get errno declaration a

最低0.47元/天 解锁文章
2380

被折叠的 条评论
为什么被折叠?



