大家好,又见面了,我是你们的朋友全栈君。
MinGW32和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
右键点击文件
C:\DEVPACK\MinGW\msys\1.0\msys.bat,在桌面上创建快捷方式, 并在”msys.bat – 快捷方式” 上点击右键, 按下图修改快捷方式的属性:
2 安装rubenvb MinGW-w64
1)从下面的链接进入下载页面:
下载(为64位Windows编译原生64位):
x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb.7z
2)从下面的链接进入下载页面:
下载(为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命令行工具(以后不特别说明,凡是以$开头的都是指msys-bat命令行MSYS,无$开头指windows命令行cmd),输入下面的命令以创建目录:
$ 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}
1)为了能分开编译32和64位的程序,为32位编译创建配置文件:/local32/etc/profile.local
$ cat >/local32/etc/profile.local << “EOF”
#==================Begin of /local32/etc/profile.local ====================
alias dir='ls -la --color=auto'
alias ls='ls --color=auto'
export PKG_CONFIG_PATH="/local32/lib/pkgconfig"
export CPPFLAGS="-I/local32/include"
export CFLAGS="-I/local32/include -mms-bitfields -mthreads -mtune=pentium3"
export CXXFLAGS="-I/local32/include -mms-bitfields -mthreads -mtune=pentium3"
export LDFLAGS="-L/local32/lib -lpthread -mthreads"
export PATH=".:/local32/bin:/mingw32/bin:/mingw/bin:/bin:/opt/bin:$PATH"
export PS1='\[\033[32m\]\u@\h \[\033[33m\w\033[0m\]$ '
export SRC_DIR=/local32/src
export DST_DIR=/local32/dst
export MINGW_HOME=/mingw32
export PKG_CONFIG=$DST_DIR/pkgconfig/bin/pkg-config.exe
# Configure PATH and FLAGS.
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"
"LIBTIFF_PREFIX:$DST_DIR/libtiff"
)
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"
export PKG_CONFIG_PATH="$VALUE/lib/pkgconfig:$PKG_CONFIG_PATH"
done
export LIBFFI_CFLAGS="-I $DST_DIR/libffi/lib/libffi-3.0.11/include"
export LIBFFI_LIBS="-L$DST_DIR/libffi/lib"
#==================End of /local32/etc/profile.local =====================
EOF
2)为64位编译创建配置文件:/local64/etc/profile.local
$ cat >/local64/etc/profile.local << “EOF”
#==================Begin of /local64/etc/profile.local ====================
alias dir='ls -la --color=auto'
alias ls='ls --color=auto'
export PKG_CONFIG_PATH="/local64/lib/pkgconfig"
export CPPFLAGS="-I/local64/include"
export CFLAGS="-I/local64/include -mms-bitfields -mthreads"
export CXXFLAGS="-I/local64/include -mms-bitfields -mthreads"
export LDFLAGS="-L/local64/lib -lpthread"
export PATH=".:/local64/bin:/mingw64/bin:/mingw/bin:/bin:/opt/bin:$PATH"
export PS1='\[\033[32m\]\u@\h \[\033[33m\w\033[0m\]$ '
export SRC_DIR=/local64/src
export DST_DIR=/local64/dst
export MINGW_HOME=/mingw64
export PKG_CONFIG=$DST_DIR/pkgconfig/bin/pkg-config.exe
# Configure PATH and FLAGS.
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"
"LIBTIFF_PREFIX:$DST_DIR/libtiff"
)
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"
export PKG_CONFIG_PATH="$VALUE/lib/pkgconfig:$PKG_CONFIG_PATH"
done
export LIBFFI_CFLAGS="-I $DST_DIR/libffi/lib/libffi-3.0.11/include"
export LIBFFI_LIBS="-L$DST_DIR/libffi/lib"
#==================End of /local64/etc/profile.local ======================
EOF
3)每次打开msys时,默认采用64位编译环境,因此更改脚本:
$ cat >> /etc/profile << "EOF"
if [ -f /local64/etc/profile.local ]; then
source /local64/etc/profile.local
fi
EOF
如果每次打开msys时,默认想用win32, 则/etc/profile的内容为:
.....
if [ -f /local32/etc/profile.local ]; then
source /local32/etc/profile.local
fi
4)在MYSYS命令行中使用下面的语句可以在32/64位编译环境之间切换(只对当前窗口有效):
$ source /local64/etc/profile.local
$ source /local32/etc/profile.local
5 编译安装x64库
5.1 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。下面继续。
5.2 编译zlib
下载:zlib-1.2.8.tar.gz 到$SRC_DIR
$ source /local64/etc/profile.local
$ tar -zxf zlib-1.2.8.tar.gz
$ make -f win32/Makefile.gcc
$ export "INCLUDE_PATH=$ZLIB_PREFIX/include"
$ export "LIBRARY_PATH=$ZLIB_PREFIX/lib"
$ export "BINARY_PATH=$ZLIB_PREFIX/bin"
$ mkdir -p $ZLIB_PREFIX/bin && cp zlib1.dll $ZLIB_PREFIX/bin/
$ make -f win32/Makefile.gcc install
5.3 编译libiconv
这是个字符编码转换库和工具。比如GBK->UTF-8。
下载libiconv-1.14.tar.gz到$SRC_DIR:
地址:
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
同样的办法可以编译win32版本的libiconv:
$ source /local32/etc/profile.local
$ tar -zxf libiconv-1.14.tar.gz
$ cd $SRC_DIR/libiconv-1.14/
$ sh configure –-prefix=$LIBICONV_PREFIX
$ make -j4
$ make install
5.4 编译LibTIFF
下载:tiff-4.0.3.tar.gz
地址:ftp://ftp.remotesensing.org/pub/libtiff/
解压到:$SRC_DIR (32位是/local32/src, 64位是/local64/src)
$ tar -zxf tiff-4.0.3.tar.gz
$ cd $SRC_DIR/tiff-4.0.3/
$ sh configure –-prefix=$LIBTIFF_PREFIX
$ make -j4
$ make install
5.5 编译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
总结一下,编译和安装 YourLib 步骤是:
3-1)下载库源码包 YourLib.tar.gz 到 $SRC_DIR
3-2)在/local??/etc/profile.local文件中添加我们要编译的库配置,例如:
......
"SQLITE3_PREFIX:$DST_DIR/sqlite3"
"YourLib_PREFIX:$DST_DIR/yourlib"
3-3)运行解压和编译命令:
$ tar -zxf YourLib.tar.gz
$ sh configure --prefix=$YourLib_PREFIX
$ make -j4
$ make install
然后就可以把YourLib.dll拷贝到Win32/64系统中使用了。仿照此方法,可以编译其他库。
6 使用Libiconv的64位版本
使用MSVS2010创建win32控制台空项目,添加下面的文件main.c。然后更改项目平台为:x64。
项目属性里面,链接器->高级里面选择:MachineX64 (/MACHINE:X64)。常规->字符集->未设置。经过这样的配置,就可以编译32 / 64位windows程序了。main.c的全部代码如下,可以很好地编译出32位和64位版本:
/* main.c
* using Libiconv 64bits
* cheungmine
* 2013-5-13
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <windows.h>
/* x64 */
#include "C:/DEVPACK/MinGW/local64/dst/libiconv/include/iconv.h"
/* x86-32 */
#include "C:/DEVPACK/MinGW/local32/dst/libiconv/include/iconv.h"
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;
if (sizeof(void*) == 8) {
printf("build for x64 Windows.\n");
hDll = LoadLibrary("C:/DEVPACK/MinGW/local64/dst/libiconv/bin/libiconv-2.dll");
} else {
printf("build for 32-bits Windows.\n");
hDll = LoadLibrary("C:/DEVPACK/MinGW/local32/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;
}
7 总结
MinGW作为组织开发、持续集成大型Windows项目,充分利用开源软件,的一款极为优秀的交叉编译环境,提供了我们在不同的平台上编译 Windows程序的能力。举例子来说:
1)x86_64-w64-mingw32-gcc-4.8.0-cygwin_rubenvb.tar.xz
这个包在Cygwin环境下使用MinGW编译出原生的64位Windows二进制APP(静态库,动态库和可执行程序)。
2)x86_64-w64-mingw32-gcc-4.8.0-linux64_rubenvb.tar.xz
这个包在Linux amd64环境下使用MinGW编译出原生的64位Windows二进制APP。
3)i686-w64-mingw32-gcc-4.8.0-win64_rubenvb.7z
这个包在Window x64环境下使用MinGW编译出原生的32位Windows二进制APP。
在我写作此文的过程中,参考了文章开头的链接,但是全部内容并不是简单的翻译,而是我亲自实践的总结。
谢谢!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/126624.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...