構建好交叉編譯環境之後可以寫個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執行一下看看結果。
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