我想让我的c++项目跨平台,我正在考虑使用Cygwin/MinGW。 但是它们之间有什么区别呢?

另一个问题是,我是否能够在没有Cygwin/MinGW的系统上运行二进制文件?


当前回答

其他答案已经击中了目标。我只是想增加一个快速捕捉的例子。

其他回答

维基百科说:

MinGW forked from version 1.3.3 of Cygwin. Although both Cygwin and MinGW can be used to port UNIX software to Windows, they have different approaches: Cygwin aims to provide a complete POSIX layer that provides emulations of several system calls and libraries that exist on Linux, UNIX, and the BSD variants. The POSIX layer runs on top of Windows, sacrificing performance where necessary for compatibility. Accordingly, this approach requires Windows programs written with Cygwin to run on top of a copylefted compatibility library that must be distributed with the program, along with the program's source code. MinGW aims to provide native functionality and performance via direct Windows API calls. Unlike Cygwin, MinGW does not require a compatibility layer DLL and thus programs do not need to be distributed with source code. Because MinGW is dependent upon Windows API calls, it cannot provide a full POSIX API; it is unable to compile some UNIX applications that can be compiled with Cygwin. Specifically, this applies to applications that require POSIX functionality like fork(), mmap() or ioctl() and those that expect to be run in a POSIX environment. Applications written using a cross-platform library that has itself been ported to MinGW, such as SDL, wxWidgets, Qt, or GTK+, will usually compile as easily in MinGW as they would in Cygwin. The combination of MinGW and MSYS provides a small, self-contained environment that can be loaded onto removable media without leaving entries in the registry or files on the computer. Cygwin Portable provides a similar feature. By providing more functionality, Cygwin becomes more complicated to install and maintain. It is also possible to cross-compile Windows applications with MinGW-GCC under POSIX systems. This means that developers do not need a Windows installation with MSYS to compile software that will run on Windows without Cygwin.

Cygwin使用一个DLL, Cygwin . DLL,(或者可能是一组DLL)在Windows上提供类似posix的运行时。

MinGW编译为本地Win32应用程序。

如果您使用Cygwin构建某个东西,那么您安装它的任何系统也将需要Cygwin DLL。MinGW应用程序不需要任何特殊的运行时。

Cygwin is designed to provide a more-or-less complete POSIX environment for Windows, including an extensive set of tools designed to provide a full-fledged Linux-like platform. In comparison, MinGW and MSYS provide a lightweight, minimalist POSIX-like layer, with only the more essential tools like gcc and bash available. Because of MinGW's more minimalist approach, it does not provide the degree of POSIX API coverage Cygwin offers, and therefore cannot build certain programs which can otherwise be compiled on Cygwin.

就两者生成的代码而言,Cygwin工具链依赖于到大型运行时库cygwin1.dll的动态链接,而MinGW工具链将代码编译为二进制文件,动态链接到Windows原生C库msvcrt.dll,以及静态链接到glibc的某些部分。Cygwin可执行文件因此更紧凑,但需要一个单独的可重新分发的DLL,而MinGW二进制文件可以独立发布,但往往更大。

基于cygwin的程序需要单独的DLL才能运行,这一事实也导致了许可限制。Cygwin运行时库是在GPLv3下授权的,对于具有符合osi的许可证的应用程序有一个链接例外,因此希望围绕Cygwin构建闭源应用程序的开发人员必须从Red Hat获得商业许可证。另一方面,MinGW代码既可以用于开源应用程序,也可以用于闭源应用程序,因为其头文件和库都是经过许可的。

从移植C程序的角度来看,理解这一点的一个好方法是举一个例子:

#include <sys/stat.h>
#include <stdlib.h>

int main(void)
{
   struct stat stbuf;
   stat("c:foo.txt", &stbuf);
   system("command");
   printf("Hello, World\n");
   return 0;
}

如果我们把stat改为_stat,我们可以用Microsoft Visual c编译这个程序。我们也可以用MinGW和Cygwin编译这个程序。

在Microsoft Visual C下,程序将被链接到MSVC可重分发的运行时库:mxvcrtnn.dll,其中nn是某个版本后缀。为了发布这个程序,我们必须包含那个DLL。DLL提供了_stat、system和printf。(我们还可以选择静态链接运行时。)

在MinGW下,程序将被链接到msvcrt.dll,这是一个内部的,无文档的,未版本的库,是Windows的一部分,并且禁止应用程序使用。该库本质上是MS Visual C的可重分发运行时库的一个分支,供Windows本身使用。

在这两种情况下,程序将具有类似的行为:

stat函数将返回非常有限的信息——例如,没有有用的权限或inode号。 路径“c:file.txt”根据当前与c:盘相关联的工作目录进行解析。 系统使用cmd.exe /c执行外部命令。

我们也可以在Cygwin下编译程序。类似于MS Visual C使用的可重分发运行时,Cygwin程序将被链接到Cygwin的运行时库:cygwin1.dll (Cygwin本身)和cyggcc_s-1.dll (GCC运行时支持)。由于Cygwin现在处于LGPL之下,我们可以将程序打包,即使它不是与gpl兼容的自由软件,也可以发布程序。

在Cygwin下,库函数会有不同的表现:

the stat function has rich functionality, returning meaningful values in most of the fields. the path c:file.txt is not understood at all as containing a drive letter reference, since c: isn't followed by a slash. The colon is considered part of the name and somehow mangled into it. There is no concept of a relative path against a volume or drive in Cygwin, no "currently logged drive" concept, and no per-drive current working directory. the system function tries to use the /bin/sh -c interpreter. Cygwin will resolve the / path according to the location of your executable, and expect a sh.exe program to be co-located with your executable.

Cygwin和MinGW都允许使用Win32函数。如果你想调用MessageBox或CreateProcess,你可以这样做。您还可以在MinGW和Cygwin下使用gcc -mwindows轻松构建一个不需要控制台窗口的程序。

Cygwin不是严格意义上的POSIX。除了提供对Windows API的访问,它还提供了自己的一些Microsoft C函数的实现(在msvcrtnn.dll或可重新分发的msvcrtnn.dll运行时中找到的东西)。一个例子是spawn*家族的函数,如spawnvp。在Cygwin上使用它们来代替fork和exec是一个好主意,因为它们更好地映射到没有fork概念的Windows进程创建模型。

因此:

Cygwin programs are no less "native" than MS Visual C programs on grounds of requiring the accompaniment of libraries. Programming language implementations on Windows are expected to provide their own run-time, even C language implementations. There is no "libc" on Windows for public use. The fact that MinGW requires no third-party DLL is actually a disadvantage; it is depending on an undocumented, Windows-internal fork of the Visual C run-time. MinGW does this because the GPL system library exception applies to msvcrt.dll, which means that GPL-ed programs can be compiled and redistributed with MinGW. Due to its much broader and deeper support for POSIX compared to msvcrt.dll, Cygwin is by far the superior environment for porting POSIX programs. Since it is now under the LGPL, it allows applications with all sorts of licenses, open or closed source, to be redistributed. Cygwin even contains VT100 emulation and termios, which work with the Microsoft console! A POSIX application that sets up raw mode with tcsetattr and uses VT100 codes to control the cursor will work right in the cmd.exe window. As far as the end-user is concerned, it's a native console app making Win32 calls to control the console.

然而:

作为一个本地Windows开发工具,Cygwin有一些怪癖,比如路径处理与Windows无关,依赖于一些硬编码的路径(如/bin/sh)以及其他问题。这些差异使得Cygwin程序“非原生”。如果一个程序以一个路径作为参数,或者从一个对话框中输入,Windows用户希望该路径以与其他Windows程序相同的方式工作。如果不是这样,那就有问题了。

插入:在LGPL发布后不久,我启动了Cygnal (Cygwin本机应用程序库)项目,以提供Cygwin DLL的一个分支,旨在修复这些问题。程序可以在Cygwin下开发,然后使用cygwin1.dll的Cygnal版本进行部署,无需重新编译。随着这个库的改进,它将逐渐消除对MinGW的需求。

When Cygnal solves the path handling problem, it will be possible to develop a single executable which works with Windows paths when shipped as a Windows application with Cygnal, and seamlessly works with Cygwin paths when installed in your /usr/bin under Cygwin. Under Cygwin, the executable will transparently work with a path like /cygdrive/c/Users/bob. In the native deployment where it is linking against the Cygnal version of cygwin1.dll, that path will make no sense, whereas it will understand c:foo.txt.

Cygwin使用兼容层,而MinGW是本地的。这是主要的区别之一。