MinGW-64安装和使用
原文出处:
CompileGraphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64
http://www.kineticsystem.org/?q=node/19
1 安装MSYS
从下面的地址下载并运行:mingw-get-inst-20120426.exe
按下面的选项安装:
安装完毕之后运行下面的命令行:
C:\DEVPACK\MinGW\msys\1.0\msys.bat
逐条输入下面的语句,执行安装软件包:
$ mingw-get install msys-rxvt
$ mingw-get install msys-wget
$ mingw-get install msys-zip
$ mingw-get install msys-unzip
$ mingw-get install msys-xz
右键点击下面的文件在桌面上创建快捷方式, 并在"msys.bat - 快捷方式" 上点击右键, 按下图修改快捷方式的属性.
C:\DEVPACK\MinGW\msys\1.0\msys.bat
2 安装rubenvb MinGW-w64
从下面的链接进入下载页面:
下载(为64位Windows编译原生64位):
x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb.7z
从下面的链接进入下载页面:
下载(为32位Windows编译,也可以运行在Win64上):
i686-w64-mingw32-gcc-4.8.0-win64_rubenvb.7z
这个包的命名规范是:
[i686-w64-mingw32]-[gcc-4.8.0]-[win64]_rubenvb
[目标平台]-[gcc-4.8.0]-[工具链OS]_rubenvb
目标平台指MinGW最终编译好的二进制代码运行的平台,[i686...]表示Win32平台,[x86_64-...]表示Win64平台。工具链OS表示你安装MinGW的计算机OS。
例如,如果想在WinXP32位平台上编译运行于Win7 64位平台的程序或库(dll),需要选择:x86_64-w64-mingw32-gcc-4.8.0-win32_rubenvb.7z。
因为我的机器是64位Win7,因此我选择了上面2个包。解压后得到:
C:\DEVPACK\MinGW\mingw32\
C:\DEVPACK\MinGW\mingw64\
另外添加3个目录如下,用于存放特定的工具链:
C:\DEVPACK\MinGW\opt
C:\DEVPACK\MinGW\local32
C:\DEVPACK\MinGW\local64
修改文件C:\DEVPACK\MinGW\msys\1.0\etc\fstab如下:
# Win32_Path Mount_Point
C:\DEVPACK\MinGW\ /mingw
C:\DEVPACK\MinGW\mingw32\ /mingw32
C:\DEVPACK\MinGW\mingw64\ /mingw64
C:\DEVPACK\MinGW\opt\ /opt
C:\DEVPACK\MinGW\local32\ /local32
C:\DEVPACK\MinGW\local64 /local64
重新打开MSYS命令行工具,输入下面的命令以创建目录:
$ mkdir /opt/bin/local{32,64}/{bin,etc,include,lib,share,dst,src}
$ mkdir/local{32,64}/lib/pkgconfig
最后的目录结构看起来是下图的样子:
3 安装CMake
在下面的地址下载cmake-2.8.10.2-win32-x86.zip,解压到/opt下面。
http://www.cmake.org/cmake/resources/software.html
解压后的目录结构如下:
/opt/bin
/opt/doc
/opt/man
/opt/share
4 配置32位和64位编译环境
打开MSYS命令行工具,运行下面的命令创建目录:
$ mkdir -p/mingw/local/{bin,docs,include,lib,share,src}
为了能分开编译32和64位的程序,为32位编译创建下面的配置文件/local32/etc/profile.local:
$ cat >/local64/etc/profile.local << "EOF"
#==================Beginof profile.local =====================
alias dir='ls -la--color=auto'
alias ls='ls--color=auto'
exportPKG_CONFIG_PATH="/local32/lib/pkgconfig"
exportCPPFLAGS="-I/local32/include"
exportCFLAGS="-I/local32/include -mms-bitfields -mthreads -mtune=pentium3"
exportCXXFLAGS="-I/local32/include -mms-bitfields -mthreads-mtune=pentium3"
exportLDFLAGS="-L/local32/lib -lpthread -mthreads"
exportPATH=".:/local32/bin:/mingw32/bin:/mingw/bin:/bin:/opt/bin:$PATH"
exportPS1='\[\033[32m\]\u@\h \[\033[33m\w\033[0m\]$ '
exportSRC_DIR=/local32/src
exportDST_DIR=/local32/dst
exportMINGW_HOME=/mingw32
exportPKG_CONFIG=$DST_DIR/pkgconfig/bin/pkg-config.exe
# Configure PATH andFLAGS.
APPLICATION_ARRAY=(
"LIBICONV_PREFIX:$DST_DIR/libiconv"
"ZLIB_PREFIX:$DST_DIR/zlib"
"BZIP2_PREFIX:$DST_DIR/bzip2"
"LIBTOOL_PREFIX:$DST_DIR/libtool"
"LIBXML2_PREFIX:$DST_DIR/libxml2"
"LIBPNG_PREFIX:$DST_DIR/libpng"
"LIBJPEG_PREFIX:$DST_DIR/libjpeg"
"LIBTIFF_PREFIX:$DST_DIR/libtiff"
"FREETYPE2_PREFIX:$DST_DIR/freetype2"
"LCMS2_PREFIX:$DST_DIR/lcms2"
"FFTW_PREFIX:$DST_DIR/fftw"
"BOTAN_PREFIX:$DST_DIR/botan"
"QT_PREFIX:$DST_DIR/qt"
"OPENCV_PREFIX:$DST_DIR/opencv"
"LIBUSBX_PREFIX:$DST_DIR/libusbx"
"LIBFFI_PREFIX:$DST_DIR/libffi"
"PKGCONFIG_PREFIX:$DST_DIR/pkg-config"
"GETTEXT_PREFIX:$DST_DIR/gettext"
"GLIB_PREFIX:$DST_DIR/glib"
"GMQ8_PREFIX:$DST_DIR/gm-q8"
"GMQ16_PREFIX:$DST_DIR/gm-q16"
"IMQ8_PREFIX:$DST_DIR/im-q8"
"IMQ16_PREFIX:$DST_DIR/im-q16"
"BOOST_PREFIX:$DST_DIR/boost"
"LIBUSBX_PREFIX:$DST_DIR/libusbx"
"GLIB_PREFIX:$DST_DIR/glib"
"YAFARAY_PREFIX:$DST_DIR/yafaray"
"SQLITE3_PREFIX:$DST_DIR/sqlite3"
)
for app in"${APPLICATION_ARRAY[@]}" ; do
KEY=${app%%:*}
VALUE=${app##*:}
export "$KEY"="$VALUE"
export PATH="$VALUE/bin:$PATH"
export LDFLAGS="-L$VALUE/lib$LDFLAGS"
export CFLAGS="-I$VALUE/include$CFLAGS"
export CPPFLAGS="-I$VALUE/include$CPPFLAGS"
export CXXFLAGS="-I$VALUE/include$CXXFLAGS"
exportPKG_CONFIG_PATH="$VALUE/lib/pkgconfig:$PKG_CONFIG_PATH"
done
exportLIBFFI_CFLAGS="-I $DST_DIR/libffi/lib/libffi-3.0.11/include"
exportLIBFFI_LIBS="-L$DST_DIR/libffi/lib"
#==================End of profile.local =====================
EOF
为64位编译创建下面的配置文件/local64/etc/profile.local:
$ cat > /local64/etc/profile.local << "EOF"
#==================Beginof profile.local =====================
alias dir='ls -la--color=auto'
alias ls='ls--color=auto'
exportPKG_CONFIG_PATH="/local64/lib/pkgconfig"
exportCPPFLAGS="-I/local64/include"
exportCFLAGS="-I/local64/include -mms-bitfields -mthreads"
exportCXXFLAGS="-I/local64/include -mms-bitfields -mthreads"
exportLDFLAGS="-L/local64/lib -lpthread"
export PATH=".:/local64/bin:/mingw64/bin:/mingw/bin:/bin:/opt/bin:$PATH"
exportPS1='\[\033[32m\]\u@\h \[\033[33m\w\033[0m\]$ '
exportSRC_DIR=/local64/src
exportDST_DIR=/local64/dst
exportMINGW_HOME=/mingw64
exportPKG_CONFIG=$DST_DIR/pkgconfig/bin/pkg-config.exe
# Configure PATH andFLAGS.
APPLICATION_ARRAY=(
"LIBICONV_PREFIX:$DST_DIR/libiconv"
"ZLIB_PREFIX:$DST_DIR/zlib"
"BZIP2_PREFIX:$DST_DIR/bzip2"
"LIBTOOL_PREFIX:$DST_DIR/libtool"
"LIBXML2_PREFIX:$DST_DIR/libxml2"
"LIBPNG_PREFIX:$DST_DIR/libpng"
"LIBJPEG_PREFIX:$DST_DIR/libjpeg"
"LIBTIFF_PREFIX:$DST_DIR/libtiff"
"FREETYPE2_PREFIX:$DST_DIR/freetype2"
"LCMS2_PREFIX:$DST_DIR/lcms2"
"FFTW_PREFIX:$DST_DIR/fftw"
"BOTAN_PREFIX:$DST_DIR/botan"
"QT_PREFIX:$DST_DIR/qt"
"OPENCV_PREFIX:$DST_DIR/opencv"
"LIBUSBX_PREFIX:$DST_DIR/libusbx"
"LIBFFI_PREFIX:$DST_DIR/libffi"
"PKGCONFIG_PREFIX:$DST_DIR/pkg-config"
"GETTEXT_PREFIX:$DST_DIR/gettext"
"GLIB_PREFIX:$DST_DIR/glib"
"GMQ8_PREFIX:$DST_DIR/gm-q8"
"GMQ16_PREFIX:$DST_DIR/gm-q16"
"IMQ8_PREFIX:$DST_DIR/im-q8"
"IMQ16_PREFIX:$DST_DIR/im-q16"
"BOOST_PREFIX:$DST_DIR/boost"
"LIBUSBX_PREFIX:$DST_DIR/libusbx"
"GLIB_PREFIX:$DST_DIR/glib"
"YAFARAY_PREFIX:$DST_DIR/yafaray"
"SQLITE3_PREFIX:$DST_DIR/sqlite3"
)
for app in"${APPLICATION_ARRAY[@]}" ; do
KEY=${app%%:*}
VALUE=${app##*:}
export "$KEY"="$VALUE"
export PATH="$VALUE/bin:$PATH"
export LDFLAGS="-L$VALUE/lib$LDFLAGS"
export CFLAGS="-I$VALUE/include$CFLAGS"
export CPPFLAGS="-I$VALUE/include$CPPFLAGS"
export CXXFLAGS="-I$VALUE/include$CXXFLAGS"
exportPKG_CONFIG_PATH="$VALUE/lib/pkgconfig:$PKG_CONFIG_PATH"
done
exportLIBFFI_CFLAGS="-I $DST_DIR/libffi/lib/libffi-3.0.11/include"
exportLIBFFI_LIBS="-L$DST_DIR/libffi/lib"
#==================End of profile.local =====================
EOF
默认采用64位编译环境,因此更改脚本:
$ cat >> /etc/profile << "EOF"
if [ -f/local64/etc/profile.local ]; then
source /local64/etc/profile.local
fi
EOF
在MYSYS命令行中使用下面的语句可以在32/64位编译环境之间切换:
$ source /local64/etc/profile.local
$ source /local32/etc/profile.local
5 编译Pkg-config Lite
pkg-config程序就是向用户向程序提供相应库的路径、版本号等信息的程序。譬如说我们运行以下命令pkg-config 查看gcc的CFLAGS参数:
$pkg-config --libs --cflags opencv
会显示如下信息:
-I/usr/include/opencv -lcxcore -lcv -lhighgui-lcvaux
这就是用gcc编译连接时CFLAGS的参数。因此当我们需要编译连接某个库时,我们只需把上面那行加入gcc 的参数里面即可。这也是configure的作用,它会检查你需要的包,产生相应的信息。
下载:pkg-config-lite-0.27.1-1.tar.gz
地址:http://sourceforge.net/projects/pkgconfiglite/files/0.27.1-1/
解压到$SRC_DIR:/local64/src
$ cd $SRC_DIR
$ tar -zxf pkg-config-lite-0.27.1-1.tar.gz
$ cd pkg-config-lite-0.27.1-1
$ sh configure --prefix=$PKGCONFIG_PREFIX
$ make -j4 # 指定使用4CPU构建,仅仅加速编译过程,不影响最后结果
$ make install
按照上面的过程,很顺利我们编译好了64位版本的程序pkg-config。下面继续。
6 编译libiconv
这是个字符编码转换库和工具。比如GBK->UTF-8。
下载到$SRC_DIR:
libiconv-1.14.tar.gz
地址:
http://ftp.gnu.org/pub/gnu/libiconv/
运行下面的命令,解压源码包,编译和安装:
$ tar -zxf libiconv-1.14.tar.gz
$ cd $SRC_DIR/libiconv-1.14/
$ sh configure –-prefix=$LIBICONV_PREFIX
$ make -j4
$ make install
7 编译LibTIFF
下载:tiff-4.0.3.tar.gz
地址:ftp://ftp.remotesensing.org/pub/libtiff/
解压到:$SRC_DIR (32位是/local32/src, 64位是/local64/src)
$ tar -xf tiff-4.0.3.tar.gz 2> /dev/null || /bin/true
$ cd $SRC_DIR/tiff-4.0.3/
$ sh configure –-prefix=$LIBTIFF_PREFIX
$ make -j4
$ make install
8 编译Sqlite3
下载:sqlite-autoconf-3071602.tar.gz
地址:http://sqlite.org/2013/sqlite-autoconf-3071602.tar.gz
运行:
$ tar -zxf sqlite-autoconf-3071602.tar.gz
$ sh configure --prefix=$SQLITE3_PREFIX
$ make -j4
$ make install
仿照这个,可以编译其他库。
9 使用Libiconv的64位版本
使用MSVS2010创建win32控制台空项目,添加下面的文件main.c。然后更改项目平台为为:x64。项目属性里面,链接器->高级里面选择:MachineX64 (/MACHINE:X64)。经过这样的配置,就可以编译64位windows程序了。main.c的全部代码如下:
/* main.c * using Libiconv 64bits * cheungmine * 2013-5-13 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #include <windows.h> #include "C:/DEVPACK/MinGW/local64/dst/libiconv/include/iconv.h" #ifdef _DEBUG # pragma comment(lib, "C:/DEVPACK/MinGW/local64/dst/libiconv/lib/libiconv.dll.a") #endif typedef iconv_t (*def_libiconv_open) (const char* tocode, const char* fromcode); typedef size_t (*def_libiconv) (iconv_t cd, char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); typedef int (*def_libiconv_close) (iconv_t cd); int main (int argc, char **argv) { size_t ret; iconv_t cd, cd2; char *inbuf, *in, *outbuf, *out; size_t inlen, outlen; HMODULE hDll; def_libiconv_open __iconv_open; def_libiconv_close __iconv_close; def_libiconv __iconv; in = (char*) malloc(100); strcpy(in, "Hello, 中国!"); inbuf = in; inlen = strlen(in) + 1; out = (char*) malloc(100); *out = 0; outbuf = out; outlen = 100; printf("build for %d-bits Windows.\n", sizeof(size_t)*8); hDll = LoadLibrary("C:/DEVPACK/MinGW/local64/dst/libiconv/bin/libiconv-2.dll"); if (!hDll) { printf("DLL not found.\n"); exit(0); } __iconv_open = (def_libiconv_open) GetProcAddress(hDll, "libiconv_open"); if (!__iconv_open) { printf("libiconv_open not found.\n"); exit(0); } __iconv = (def_libiconv) GetProcAddress(hDll, "libiconv"); if (!__iconv_open) { printf("libiconv not found.\n"); exit(0); } __iconv_close = (def_libiconv_close) GetProcAddress(hDll, "libiconv_close"); if (!__iconv_open) { printf("libiconv_close not found.\n"); exit(0); } cd = __iconv_open("UTF-8", "GBK"); cd2 = __iconv_open("GBK", "UTF-8"); ret = __iconv(cd, (char**)&in, &inlen, (char**)&out, &outlen); printf("GBK(%s) => UTF-8(%s)\n", inbuf, outbuf); /* now in is utf-8 string */ in = outbuf; inlen = strlen(in)+1; /* now out is empty */ *inbuf = 0; out = inbuf; outlen = 100; ret = __iconv(cd2, (char**)&in, &inlen, (char**)&out, &outlen); printf("UTF-8(%s) => GBK(%s)\n", outbuf, inbuf); __iconv_close(cd); __iconv_close(cd2); free(inbuf); free(outbuf); FreeLibrary(hDll); return 0; }(结束)