windows使用VS编译libiconv for x64备忘

1、安装vs 2017 enterprise x64版本。

2、下载libiconv源码,当前源码版本为1.16。解压到某个没有中文环境的目录,比如:D:\devel\libiconv-1.16。

3、安装cygwin,比如安装到D:\cygwin64,打开cygwin终端,运行命令:

mkdir -p $HOME/msvc

在$HOME/msvc下创建两个sh脚本,一个名叫ar-lib,一个叫compile,内容在:https://github.com/gcc-mirror/gcc/blob/master/ar-lib和https://github.com/gcc-mirror/gcc/blob/master/compile。然后授予可执行权限

cd $HOME/msvc
chmod a+x ar-lib compile

4、在终端下输入如下指令:

WindowsCrtIncludeDir='C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt'
WindowsCrtLibDir='C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\'
INCLUDE="${WindowsCrtIncludeDir};$INCLUDE"
LIB="${WindowsCrtLibDir}x64;$LIB"

WindowsSdkIncludeDir='C:\Program Files (x86)\Windows Kits\8.1\Include\'
WindowsSdkLibDir='C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\'
INCLUDE="${WindowsSdkIncludeDir}um;${WindowsSdkIncludeDir}shared;$INCLUDE"
LIB="${WindowsSdkLibDir}x64;$LIB"

VSINSTALLDIR='C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise'
VCINSTALLDIR="${VSINSTALLDIR}"'\VC\Tools\MSVC\14.16.27023\'
PATH=`cygpath -u "${VCINSTALLDIR}"`/bin/Hostx64/x64:"$PATH"
INCLUDE="${VCINSTALLDIR}"'\include;'"${INCLUDE}"
LIB="${VCINSTALLDIR}"'\lib\x64;'"${LIB}"

export INCLUDE LIB

这是临时设置头文件与库文件地址的,Windows SDK版本与VS版本需要自行查看本机设置,然后更改成相应版本号即可。

5、继续输入指令:

export PATH=/usr/local/msvc64/bin:$PATH

win32_target=_WIN32_WINNT_WINXP   # for MSVC 9.0
win32_target=_WIN32_WINNT_VISTA   # possibly for MSVC >= 10.0
win32_target=_WIN32_WINNT_WIN7    # possibly for MSVC >= 10.0
win32_target=_WIN32_WINNT_WIN8    # possibly for MSVC >= 10.0

cd /cygdrive/d/devel/libiconv-1.16 #进入libiconv的解压目录

./configure --host=x86_64-w64-mingw32 --prefix=/usr/local/msvc64 \
            CC="$HOME/msvc/compile cl -nologo" \
            CFLAGS="-MD" \
            CXX="$HOME/msvc/compile cl -nologo" \
            CXXFLAGS="-MD" \
            CPPFLAGS="-D_WIN32_WINNT=$win32_target -I/usr/local/msvc64/include" \
            LDFLAGS="-L/usr/local/msvc64/lib" \
            LD="link" \
            NM="dumpbin -symbols" \
            STRIP=":" \
            AR="$HOME/msvc/ar-lib lib" \
            RANLIB=":"

make -j4
make check
make install

win32_target根据自己的OS版本来设置,我设置成_WIN32_WINNT_WIN10。--prefix是libiconv的最终安装路径。

6、make -j4中之后会有一个提示:

warning: remember to run 'libtool --finish /usr/local/msvc64/lib'

不明白什么意思,反正编译之后就运行一下。另外还有一个错误,即libcharset_set_relocation_prefix重定义错误,冲突文件为.\libcharset\include\libcharset.h与.\libcharset\lib\relocatable-stub.c,将relocatable-stub.c中的定义注释掉

//就是这两行代码
//extern LIBCHARSET_DLL_EXPORTED void
//libcharset_set_relocation_prefix (const char *orig_prefix, const char *curr_prefix);

7、make install时会有个错误:

D:\devel\libiconv-1.16\src\.libs\lt-iconv.c : fatal error C1083: 无法打开编译器生成的文件: “D:\linux2win32\src\libiconv-1.16\src\lt-iconv.obj”: Permission denied

//虽然产生这个错误,但是lt-iconv.obj已被删除,于是继续运行make install,使其生成lt-iconv.obj,然后更改权限

chmod 777 src/lt-iconv.obj

//再次运行make install,OK。

安装完成后的目录大概是这样的:

$ ls -R /usr/local/msvc64

/usr/local/msvc64:
bin  include  lib  share

/usr/local/msvc64/bin:
charset-1.dll  iconv.exe  iconv-2.dll

/usr/local/msvc64/include:
iconv.h  libcharset.h  localcharset.h

/usr/local/msvc64/lib:
charset.dll.lib  iconv.dll.lib  charset.lib
libcharset.la libiconv.la

#share目录省略

好像没有静态链接库,以后再摸索了。

8、运行测试,在命令行当前目录建立一个test.txt文件,输入中文,保存为UTF-8,然后运行

/usr/local/msvc64/bin/iconv -t "GBK" -f "utf-8" ./test.txt

9,cygwin和msys2中使用MingGW安装更加简便,直接./configure --host=x86_64-w64-mingw32 --prefix=/usr/local/iconv && make -j4 && make check && make install即可,但是这两个模拟器中的gcc是posix线程,与win32线程还是有些区别的,比如ISAPI就无法识别posix的dll。

你可能感兴趣的:(C/C++)