[编码]使用MinGW 编译 iconv 库

C/C++第三方开源库的介绍和相关讨论
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

[编码]使用MinGW 编译 iconv 库

帖子 523066680 »

编辑:523066680
于 Code-By.Org,转载请注明出处

GNU页面
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

已有的环境:msys2.0, mingw, GnuWin32工具集

解压后先别急着configure
先看看 README.woe32
Building requires the mingw or cygwin development environment (includes gcc). I recommend to use the cygwin environment as the development environment and mingw only as the target (runtime, deployment) environment. For this, you need to install - cygwin, - the mingw runtime package, also from the cygwin site.
首先安装cygwin,
进入目录后执行 ./configure , 第一个问题
checking if gcc supports -fno-rtti -fno-exceptions... ./configure: line 7594: diff: command not found
缺个diff.exe,用everything 搜索到 C:\Program Files (x86)\GnuWin32\bin 下面有一个,复制到 C:\cygwin64\bin,
先不急着执行./configure,单独执行diff.exe 提示缺少 libintl3.dll,将这个文件也复制过来,
再次执行diff.exe 提示缺少 libiconv2.dll,再复制,没问题了。
(如果你从别的地方获取diff,可能不需要依赖dll,也可能依赖别的库,用类似的方法配置齐全就是了)

执行 ./configure 没有出现明显问题

执行 make ,提示
./relocatable.c: In function 'libcharset_relocate': <command-line>:0:12: error: expected expression before '/' token ./relocatable.c:423:37: note: in expansion of macro 'INSTALLDIR' const char *orig_installdir = INSTALLDIR; ^~~~~~~~~~ make[2]: *** [Makefile:65: relocatable.lo] Error 1 make[2]: Leaving directory 'C:/Users/ViANG/Desktop/libiconv-1.14/libcharset/lib' make[1]: *** [Makefile:34: all] Error 2 make[1]: Leaving directory 'C:/Users/ViANG/Desktop/libiconv-1.14/libcharset' make: *** [Makefile:42: lib/localcharset.h] Error 2
好像是说这个 INSTALLDIR 的宏里面,包含了不可预料的表达式(在/之前),但是我也不知道怎么看INSTALLDIR的值,
用grep -R "INSTALLDIR" * 看了一下
build-aux/install-reloc: -D"INSTALLPREFIX=\"$prefix\"" -D"INSTALLDIR =\"$installdir\"" \ lib/Makefile:-DENABLE_RELOCATABLE=1 -DIN_LIBRARY -DINSTALLDIR=\"$(libdir)\" -DNO _XMALLOC \ lib/Makefile.in:-DENABLE_RELOCATABLE=1 -DIN_LIBRARY -DINSTALLDIR=\"$(libdir)\" - DNO_XMALLOC \
大概是从某个参数中传递进去的,既然它的内容有毒,而且只有一处,那自己改写试试:
找到:libiconv-1.14\libcharset\lib\relocatable.c


const char *orig_installdir = INSTALLDIR;
改为
const char *orig_installdir = "/usr/local/cygwin";

同时 mkdir /usr/local/cygwin
然后执行
$ ./configure --prefix=/usr/local/cygwin
$ make
提示错误
C:\MinGW\bin\ranlib.exe: '/cygdrive/c/Users/name/Desktop/libiconv-1.14/lib/libcharset.a': No such file
试了一下,对于mingw和msys的那一套,可以接受的路径是 /c/usr/...这样的,而在cygwin中的路径是 /cygdrive/c/usr...
试着用sed和grep 批量替换,以及寻找 生成路径的 cygpath命令,修改相应的参数,但还是失败了。
分割线
既然Cygwin 路径和 Msys mingw不兼容,而我对这一系列的东西不太熟,只能再换MSYS试试了
按cygwin的方法把diff.exe和相关的文件复制到 c:/mingw/bin

打开 MSYS2 Shell
$ ./configure configure: error: cannot guess build type; you must specify one
添加参数:
$ ./configure --build=mingw32
看上去没什么问题
$ make
跑了一大段后,出现前面同样的问题:
./relocatable.c: In function 'libiconv_relocate': <command-line>:0:12: error: 'C' undeclared (first use in this function) ./relocatable.c:423:37: note: in expansion of macro 'INSTALLDIR' const char *orig_installdir = INSTALLDIR; ^~~~~~~~~~ <command-line>:0:12: note: each undeclared identifier is reported only once for each function it appears in./relocatable.c:423:37: note: in expansion of macro 'INSTALLDIR' const char *orig_installdir = INSTALLDIR; ^~~~~~~~~~ <command-line>:0:13: error: expected ',' or ';' before ':' token ./relocatable.c:423:37: note: in expansion of macro 'INSTALLDIR' const char *orig_installdir = INSTALLDIR; ^~~~~~~~~~ make[1]: *** [Makefile:87: relocatable.lo] Error 1 make[1]: Leaving directory 'C:/Users/ViANG/Desktop/libiconv-1.14/lib' make: *** [Makefile:33: all] Error 2
找到
libiconv-1.14/lib/relocatable.c 和
libiconv-1.14/libcharset/lib/relocatable.c
将const char *orig_installdir = INSTALLDIR 改为 const char *orig_installdir = "/usr/local" 继续,
$ make $ make install
看上去没什么问题了 :coverface1

总结起来就是
STRA='= \INSTALLDIR'
STRB='= \"\/usr\/local\"'

./configure --build=mingw32

sed -i "s/$STRA/$STRB/" `grep -rl -P "= \INSTALLDIR"`
make
make install
相关的库文件位于
\libiconv-1.14\lib\.libs

头文件位于
\libiconv-1.14\include
补充一个,网上找到的,预编译的库文件
编译好的64位库文件
https://github.com/holy-shit/iconv-for-windows
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

编译iconv示例代码,utf8转gbk并输出

帖子 523066680 »

不知道何年何月存了一份示例代码
#include <stdio.h>
#include <string.h>
//包函libiconv库头文件
#include <iconv.h>

int ChangeCode( const char* pFromCode,
const char* pToCode,
const char* pInBuf,
size_t* iInLen,
char* pOutBuf,
size_t* iOutLen );

int main( int argc, char* argv[] )
{
char sInBuf[100];
char sOutBuf[100];
size_t iInLen = 0;
size_t iOutLen = 100;
int iRet;
strcpy( sInBuf, "中文Source" );
memset( sOutBuf, 0x0, 100 );
iInLen = strlen( sInBuf );
iRet = ChangeCode( "UTF-8", "GBK", sInBuf, &iInLen, sOutBuf, &iOutLen );
printf("%s\n", sOutBuf);

for (int i = 0; i < 15 ; i++)
{
printf("%x ", (unsigned char)sOutBuf[i] );
}
return 0;
}

int ChangeCode( const char* pFromCode,
const char* pToCode,
const char* pInBuf,
size_t* iInLen,
char* pOutBuf,
size_t* iOutLen )
{
int iRet;
//打开字符集转换
iconv_t hIconv = iconv_open( pToCode, pFromCode );
if ( hIconv == NULL)
{
return -1;//打开失败,可能不支持的字符集
}
//开始转换
iRet = iconv( hIconv, (char**)(&pInBuf), iInLen, (char**)(&pOutBuf), iOutLen );
//关闭字符集转换
iconv_close( hIconv );
return iRet;
}
代码保存为utf8编码格式,假设前面配置过的 libiconv-1.14 文件夹就在当前目录,编译命令:
>gcc iconv.c -I./libiconv-1.14/include -L./libiconv-1.14/lib/.libs -liconv
运行提示
a.exe - 应用程序错误
应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。
将 libiconv-2.dll 复制到当前目录,运行 a.exe
>a.exe 中文Source d6 d0 ce c4 53 6f 75 72 63 65 0 0 0 0 0
Done
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

Re: [编码]使用MinGW 编译 iconv 库

帖子 523066680 »

:look_right
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客