在Linux上交叉编译windows程序 mingw32交叉编译
最近几天搞 wxWidgets,由于要同时编译应用程序的 Windows 版本,又懒得在虚拟机里面编译程序,于是想到用交叉编译的方式在 Linux 上编译好 Windows 版本的程序,再用 Wine 或者虚拟机来执行。试了一下,这种方法很好用,比在虚拟机里面编译程序快很多。我的操作系统是 debian sarge,其中已经有 mingw32 编译器,只需安装:apt-get instal
最近几天搞 wxWidgets,由于要同时编译应用程序的 Windows 版本,又懒得在虚拟机里面编译程序,于是想到用交叉编译的方式在 Linux 上编译好 Windows 版本的程序,再用 Wine 或者虚拟机来执行。试了一下,这种方法很好用,比在虚拟机里面编译程序快很多。
我的操作系统是 debian sarge,其中已经有 mingw32 编译器,只需安装:
apt-get install mingw32
这会安装好 mingw32 交叉编译系统,用 i586-mingw32msvc-gcc 即可为 Windows 编译程序。
在编译 wxWidgets 的 Windows 版本之前,需要安装 DirectX 的头文件。大概是由于版权之类的原因,在 Debian 的 mingw32 中并未包含 DirectX 的头文件,只包含了 DirectX 的库,因此需要自己安装。如果不安装,在编译 wxWidgets 的时候会有警告,并且系统不会产生 wxDisplay 这个类。安装并不复杂,只要从网络上找到相关的头文件和库文件,并解压到 /usr/i586-mingw32msvc 目录中即可。
然后就可以编译 wxWidgets 了,下载并解压源码后,用如下命令可以编译:
./configure --host=i586-mingw32msvc --target=i586-mingw32msvc --prefix=~/Workshop/mingw32 --enable-monolithic --disable-shared
make; make install-strip
make -C contrib; make -C contrib install-strip
编译完成后,进入 samples 目录,随便找一个例程 make 一下,便可产生 exe 文件了。要执行这个文件,需要 mingwm10.dll 这个文件,在 Debian 中,此文件位于 /usr/share/doc/mingw32-runtime 目录中,把它复制到当前目录,便可用 wine 执行了。如果没有问题,也可以把这个文件仍到 Wine 的 C:\Windows\system32 目录中。
下图是在 Wine 中执行程序的效果:
在Linux使用mingw32来编写win32程序 2006-09-01 19:26:07
分类:
MinGW - Minimalist GNU For Windows
Mingw32 是 GNU 計畫工具的集合,包含了大量的標頭檔(header files)、函式庫與指 令程式。目的在提供免費的工具以生產製作可於 Winodws 上直接執行而無須依賴輔助函式 庫的原生程式(Native Windows programs)。
在 Debian 系統中,您可以安裝 DebianPackages:mingw32 、DebianPackages:mingw32-binutils 與 DebianPackages:mingw32-runtime 三個套件軟體。
- DebianPackages:mingw32 - Minimalist GNU win32 (cross) compiler 包含 win32 跨平台編譯器
- DebianPackages:mingw32-binutils - Minimalist GNU win32 (cross) binutils 包含 win32 跨平台指令集
- DebianPackages:mingw32-runtime - Minimalist GNU win32 (cross) runtime 函式庫及標頭檔
DebianPackages:mingw32 包含的就是 g++(c++,cpp)、gcc(cc)、gcov、gccbug 等必備的 win32 跨平台編譯器,您需要這些編譯器產生可在 Microsoft Windows 上使用的 exe、dll 檔案等。DebianPackages:mingw32-binutils 則是 nm、strip、ar、ranlib、dlltool、as、ld、windres、addr2line、size、objdump、readelf 等,在產生原生程式時必要的工具。這些指令為了與原生編譯器有所區別,目錄與其他軟體大不相同,擺在 /usr/i586-mingw32msvc/ 中。指令命名原則也以 i586-mingw32msvc 開頭。
例如產生一個 console executable 執行檔。
user@debian:~$ cat hello.cpp #include <iostream> int main(int argc, char* argv[]) { std::cout << "Hello world\n"; return 0; } user@debian:~$ i586-mingw32msvc-g++ -o hello.exe hello.cpp user@debian:~$ file hello.exe hello.exe: MS Windows PE 32-bit Intel 80386 console executable not relocatable
如果你安裝了 wine,你甚至可以直接執行它。由於 wine 配合 binfmt-support 使用,因此你的系統應該懂著識別 .exe 檔案,並以 wine 執行他。
user@debian:~$ ./hello.exe Hello world Wine exited with a successful status
或者編譯一個顯示一個訊息窗的圖形化使用者介面程式
user@debian:~$ cat > hello.cpp #include <windows.h> int WINAPI WinMain(HINSTANCE d1, HINSTANCE d2, LPSTR d3, int d4) { MessageBox(NULL, "Hello, World!", "", MB_OK); return(0); } user@debian:~$ i586-mingw32msvc-g++ -o hello.exe hello.cpp -mwindows user@debian:~$ file hello.exe hello.exe: MS Windows PE 32-bit Intel 80386 GUI executable not relocatable
您大概注意到指令中新增了 "-mwindows" 參數,這是用來建立 "Windows Application" 而非 "Console Application",並在連結(linking) 過程中確保使用 Windows 函式庫。上述 我們只顯示 "Hello World" 字串的範例中,便是所謂的 "Console Application",執行時 會啟動一個主控台(Console)視窗。您可以加上 " -mconsole" 以編譯為 "Console Application"。
透過 mingw32 的協助,您可以在 Linux 系統上設計 "Write Once, Run Everywhere" 的 C / C++ 程式,例如在 Linux 平台編譯同時支援 Unix 、 Mac 與 Windows 的 putty。
更多推荐
所有评论(0)