在Linux使用mingw32来编写win32程序 (debain/fedora/ubuntu)

Mingw:在Linux系统下编译Windows的程序
http://bt.antiy.org.cn/redmine/w ... 4%E7%A8%8B%E5%BA%8F

Ubuntu下可以直接安装:
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime

安装后编译程序可以:
i586-mingw32msvc-g++(编译C++程序)
i586-mingw32msvc-gcc(编译C程序)

用法和gcc/g++非常类似。
如hello.c程序:

int WINAPI WinMain (HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     PSTR szCmdLine,
                     int iCmdShow)
{
     MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
     return (0);
}

编译:
i586-mingw32msvc-gcc hello.c -o hello.exe -mwindows

注:-mwindows指示其编译的是windows程序,若编译的是console(命令行)程序,则不需要这个参数。

若安装了wine,还可以测试一下这个程序:
wine hello.exe

 

2008-05-16 15:33

在Linux使用mingw32来编写win32程序

MinGW - Minimalist GNU For Windows

Mingw32 是 GNU 计画工具的集合,包含了大量的标头档(header files)、函式库与指 令程式。目的在提供免费的工具以生产制作可於 Winodws 上直接执行而无须依赖辅助函式 库的原生程式(Native Windows programs)。

在 Debian 系统中,您可以安装 DebianPackages:mingw32DebianPackages:mingw32-binutils DebianPackages:mingw32-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
初次执行的时候会自动编译一些东西的,知道最后wine: '/home/user/.wine' created successfully.
然后才是执行的程序

或者编译一个显示一个讯息窗的图形化使用者介面程式

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

取自"http://wiki.debian.org.tw/index.php/Mingw32"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值