mingw跟cgywin的gcc编译器区别

本文探讨了Unix下的C代码如何在Win32环境下进行编译和运行的问题,介绍了两种主要的方法:一是使用MinGW将Unix系统调用转换为等效的Windows API;二是采用Cygwin,通过cygwin1.dll实现在运行时将Unix系统调用翻译为Windows API。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常情况下Unix下编译通过的C代码在Win32下编译是不能通过的,虽然大多数函数调用在Unix和Win32下是相同的,但是Unix有自己一些独特的系统调用如fork,spawn,signals,select,sockets等),如果代码中使用了这些系统调用,在Win32环境下运行的时候自然是找不到对应的库的。Windows提供的程序接口叫API(其实就是系统调用),它虽然函数名字和Unix下的不一样,但功能确是一样的。如Unix下的fork在Windows中对应的API就是CreateProcess.

所以如果你想在Windows上开发/编译Unix代码,可以有两种方法:
1、让Windows下的C语言编译器在编译期间将Unix代码中的特殊系统调用转换成相应的Windows API形式,MinGW就是这么做的。
2、编译器还是用Unix的编译器,在每个程序运行期间,当他要执行Unix系统调用时,由一个额外的动态链接库(这是Windows特有的东西,说白了就是一组函数库,在程序运行期间可以动态加载和卸载)将它翻译成Windows API,这就是Cygwin的做法,这个DLL就是cygwin1.dll。
由些可见,MinGW相比Cygwin来讲更加贴近win32,因为它几乎支持所有的Win32API。它所连接的程序,不需要任何第三方库即可运行,速度较快。但Cygwin编译出来的C代码就等同于Unix环境下编译出来的,所以可以不经修改直接运行在Unix平台,如果要在Windows上运行,加载一个cygwin1.dll就可以了,虽然速度上有点影响,但是实现了跨平台的代码开发。具体选用哪一个,就看各自的需要了。

In general C codes written in Unix can’t be compiled in Windows OS. Unix has some special system calls(like fork, spawn, signals, select, sockets and so on) which Windows doesn’t has, but Windows has other APIs with same function in that these two kinds of OS all  comply with ANSI C standard. So, fork can’t be called in Windows but Windows usesCreateProcess to accomplish the same function.
MinGW and Cygwin supply two types of C compiler to realize that programmers can code Unix C in Windows environment.
1. MinGW gcc compiler converts Unix calls to Windows APIs in equivalent in the progress of compile.
2.Cygwin gcc compiler is same to the gcc in Unix, for the purpose, it adds one filecygwin1.dll to each application to translate Unix system calls to Win APIs at runtime. Consequently, codes can be run in two OS seamlessly.
Now we can see that Cygwin would be better if your codes ran in Unix but developed in Windows. On the ohter hand, MinGW can compile a pure Win32 application from your Unix codes. Moreover the speed of applications compiled by MinGW is faster than those by Cygwin because the latter has an additional translating process at runtime executed incygwin1.dll. 
BTW: If you want to get MinGW RAR bag in size of 24MBytes, I’m glad to mail it to you. You can get my email in URl http://blog.classky.com/about/, and please ensure your mailbox could support the size of attachment.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值