在linux 上编译生成windows上运行的exe程序,交叉编译环境的配置( 平台:gentoo linux)

本文详细介绍了如何在Gentoo Linux上安装i686-mingw32交叉编译环境,包括使用crossdev工具配置目标平台、下载并安装必要的软件包,以及最终编译生成适用于Windows的exe文件的过程。通过实践示例,读者可以轻松掌握交叉编译的基本操作。

gentoo 给安装交叉编译环境,进行了简化,

首先安装crossdev

emerge crossdev

这个东西安装好后并不能真正编译exe 程序 ,它只是一个帮你安装交叉编译环境的工具

你可以运行这个命令试试

crossdev

[c-sharp]  view plain copy
  1. Usage: crossdev [options] --target TARGET  
  2. Options:  
  3.     --b, --binutils ver   Specify version of binutils to use  
  4.     --g, --gcc ver        Specify version of gcc to use  
  5.     --k, --kernel ver     Specify version of kernel headers to use  
  6.     --l, --libc ver       Specify version of libc to use  
  7.     -S, --stable          Use latest stable versions as default  
  8.     -C, --clean target    Uninstall specified target  
  9.     -P, --portage opts    Options to pass to emerge (see emerge(1))  
  10.     --with[out]-headers   Build C library headers before C compiler?  
  11. Stage Options:  
  12.     -s0, --stage0         Build just binutils  
  13.     -s1, --stage1         Also build a C compiler (no libc/C++)  
  14.     -s2, --stage2         Also build kernel headers  
  15.     -s3, --stage3         Also build the C library (no C++)  
  16.     -s4, --stage4         Also build a C++ compiler [default]  
  17. Extra Fun (must be run after above stages):  
  18.     --ex-only             Skip the stage steps above  
  19.     --ex-gcc              Build extra gcc targets (gcj/ada/etc...)  
  20.     --ex-gdb              Build a cross gdb  
  21.     --ex-insight          Build a cross insight  
  22. Target (-t) takes a tuple ARCH-VENDOR-OS-LIBC; see 'crossdev -t help'  

然后再运行 crossdev -t help

会显示出所有支持的平台

[c-sharp]  view plain copy
  1. Supported Architectures:  
  2.    - alpha                                     - arm / armeb  
  3.    - hppa (parisc)                             - ia64  
  4.    - i386 / i486 / i586 / i686 (x86)           - m68k  
  5.    - mips / mipsel / mips64 / mips64el  
  6.    - powerpc (ppc) / powerpc64 (ppc64)  
  7.    - sparc / sparc64                           - s390 / s390x  
  8.    - sh / sh[1-5] / sh64                       - x86_64 (amd64)  
  9. Supported C Libraries:  
  10.    - glibc (gnu)  
  11.    - klibc       [prob wont work]  
  12.    - newlib      [bare metal/no operating system]  
  13.    - uclibc      [not all arches are ported]  
  14. Special Targets:  
  15.    - avr      http://www.nongnu.org/avr-libc/  
  16.    - bfin     http://blackfin.uclinux.org/  
  17.    - h8300    http://h8300-hms.sourceforge.net/  
  18.    - mingw32  http://www.mingw.org/  
  19.    - msp430   http://mspgcc.sourceforge.net/  
  20.    - nios2    http://www.altera.com/products/ip/processors/nios2/ni2-index.html  
  21.    - xc16x    http://www.infineon.com/  
  22.    - ee / iop / dvp (ps2) [Playstation 2 targets]  
  23.    - ppu / spu (cell) [Cell/Playstation 3 targets]  
  24. Softfloat toolchains:  
  25.    Include 'softfloat' in the 'vendor' field  
  26.    e.g. armeb-softfloat-linux-uclibc  powerpc-booya_softfloat-linux-gnu  

运行 这个命令才真正安装目标平台的交叉编译环境 ,我安装的是i686 ,mingw32(好像是表示win32 ,不太明白,但是安装这个可以编译生成exe) 

 crossdev i686-mingw32

这个过程中会下载并安装binutil ,gcc 等软件

[c-sharp]  view plain copy
  1. * Host Portage ARCH:     x86  
  2.  * Target Portage ARCH:   x86  
  3.  * Target System:         i686-mingw32  
  4.  * Stage:                 4 (C/C++ compiler)  
  5.  * binutils:              binutils-[latest]  
  6.  * gcc:                   gcc-[latest]  
  7.  * headers:               w32api-[latest]  
  8.  * libc:                  mingw-runtime-[latest]  
  9.  * PORTDIR_OVERLAY:       /resource/pkg/gentoo/layman/gentoo-china  
  10.  * PORT_LOGDIR:           /var/log/portage  
  11.  * PKGDIR:                /resource/pkg/gentoo/binPackages/cross/i686-mingw32  
  12.  * PORTAGE_TMPDIR:        /var/tmp/cross/i686-mingw32   

 

完成之后,你会发现会有许多以i686-mingw32开头的命令如


[c-sharp]  view plain copy
  1. i686-mingw32-addr2line  i686-mingw32-g++        i686-mingw32-ranlib  
  2. i686-mingw32-ar         i686-mingw32-gcc        i686-mingw32-readelf  
  3. i686-mingw32-as         i686-mingw32-gcov       i686-mingw32-size  
  4. i686-mingw32-c++        i686-mingw32-gprof      i686-mingw32-strings  
  5. i686-mingw32-c++filt    i686-mingw32-ld         i686-mingw32-strip  
  6. i686-mingw32-cpp        i686-mingw32-nm         i686-mingw32-windmc  
  7. i686-mingw32-dlltool    i686-mingw32-objcopy    i686-mingw32-windres  
  8. i686-mingw32-dllwrap    i686-mingw32-objdump      

 

进行测试一下

hello.c

[c-sharp]  view plain copy
  1. #include<stdio.h>  
  2. int main(){  
  3. printf("hello!");  
  4. }  

编译 i686-mingw32-gcc  hello.c  -o hello.exe

在目录 下会生成一个hello.exe

拿到windows 下的命令行下测试一下,发现可以运行


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值