歡迎您光臨本站 註冊首頁

在Gentoo上構建windows交叉編譯環境

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

最近公司開發一款跨平台的產品,主要支持的平台包括linux和windows,由於我的日常工作操作系統是linux,因此要調試windows部分代碼極其不方便,要麼在linux下安裝一個虛擬機跑windows,要麼雙系統重啟機器進入windows,總之是比較費時且麻煩。於是不安份的情緒讓我折騰了一上午時間,終於成功地在我的Gentoo Linux上構建好了windows交叉編譯環境。

其實在Gentoo Linux上構建交叉編譯環境非常方便,crossdev一行命令就搞定一切,關於crossdev的用法很簡單,可以參考Gentoo官方網站或者Gentoo-Wiki網站上的相關文檔。構建windows交叉編譯環境直接執行crossdev i686-mingw32即可。

構建好交叉編譯環境之後可以寫個hello world程序嘗試一下,i686-mingw32-gcc -o test.exe test.c,然後file test.exe即可看到文件類型為「MS-DOS executable PE for MS Windows (console) Intel 80386 32-bit」,你也可以用wine test.exe執行一下看看結果。

似乎到此就圓滿結束了?為了更方便地進行交叉編譯,繼續往下看吧。

開源軟體大多數採用autotools來完成項目的自動構造,我們能不能利用autotools來方便地進行管理規模稍大的項目呢?答案是肯定的,今天上午為了測試交叉編譯寫了一個簡單的win32 service,可以通過以下鏈接下載:SMQService,方便懶人們快速嘗試交叉編譯,不過你可能馬上就會遇到挫折,來看看結果吧。

debianl@ldb /data/codes/test/win32service $ ./configure --target=i686-mingw32 --host=i686-mingw32 --build=i686-mingw32 --prefix=/usr/i686-mingw32/usr
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for i686-mingw32-gcc... i686-mingw32-gcc
checking for C compiler default output file name... a.exe
checking whether the C compiler works... configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details.

仔細看一下config.log即可看出configure會採用編譯器編譯一個測試程序並執行,在這兒會產生一個a.exe並直接執行,在linux直接執行exe文件如果不做點工作是不行的,直接執行a.exe報錯configure也就停下了,告訴你"cannot run C compiled programs",其實compiler工作得好好的,那麼我們現在只要解決直接在linux下運行exe文件就可以讓autotools順利地工作了。

其實要實現這個功能很簡單,編譯內核時注意選上Executable file formats->Kernel support for MISC binaries,如果編譯成模塊modprobe binfmt_misc即可載入,然後給你的/etc/fstab里加上一行「none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0」,在系統引導完成之後執行"echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register",在Gentoo下可以將這行指令寫到/etc/conf.d/local.start文件,現在再試應該就好了。

以下是我的編譯日誌:
debianl@ldb /data/codes/test/win32service $ ./configure --target=i686-mingw32 --host=i686-mingw32 --build=i686-mingw32 --prefix=/usr/i686-mingw32/usr
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for i686-mingw32-gcc... i686-mingw32-gcc
checking for C compiler default output file name... a.exe
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... .exe
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether i686-mingw32-gcc accepts -g... yes
checking for i686-mingw32-gcc option to accept ISO C89... none needed
checking dependency style of i686-mingw32-gcc... gcc3
checking how to run the C preprocessor... i686-mingw32-gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking stdio.h usability... yes
checking stdio.h presence... yes
checking for stdio.h... yes
checking windows.h usability... yes
checking windows.h presence... yes
checking for windows.h... yes
checking for an ANSI C-conforming const... yes
checking for inline... inline
checking for size_t... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible malloc... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/config.h
config.status: executing depfiles commands

debianl@ldb /data/codes/test/win32service $ make
Making all in src
make[1]: Entering directory `/data/codes/test/win32service/src'
make all-am
make[2]: Entering directory `/data/codes/test/win32service/src'
i686-mingw32-gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
i686-mingw32-gcc -g -O2 -o SMQService.exe main.o
make[2]: Leaving directory `/data/codes/test/win32service/src'
make[1]: Leaving directory `/data/codes/test/win32service/src'
make[1]: Entering directory `/data/codes/test/win32service'
make[1]: Nothing to be done for `all-am'.
make[1]: Leaving directory `/data/codes/test/win32service'

debianl@ldb /data/codes/test/win32service $ file src/SMQService.exe
src/SMQService.exe: MS-DOS executable PE for MS Windows (console) Intel 80386 32-bit

[火星人 ] 在Gentoo上構建windows交叉編譯環境已經有605次圍觀

http://coctec.com/docs/linux/show-post-187615.html