我想让我的c++项目跨平台,我正在考虑使用Cygwin/MinGW。 但是它们之间有什么区别呢?
另一个问题是,我是否能够在没有Cygwin/MinGW的系统上运行二进制文件?
我想让我的c++项目跨平台,我正在考虑使用Cygwin/MinGW。 但是它们之间有什么区别呢?
另一个问题是,我是否能够在没有Cygwin/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 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代码既可以用于开源应用程序,也可以用于闭源应用程序,因为其头文件和库都是经过许可的。
Cygwin试图在Windows上创建一个完整的UNIX/POSIX环境。为此,它使用了各种dll。虽然这些dll由GPLv3+覆盖,但它们的许可包含一个异常,该异常不强制派生作品由GPLv3+覆盖。MinGW是一个C/ c++编译器套件,它允许你创建Windows可执行文件而不依赖于这些dll -你只需要正常的MSVC运行时,这是任何正常的微软Windows安装的一部分。
您还可以获得一个类似UNIX/POSIX的小型环境,使用MinGW编译,称为MSYS。它不具备Cygwin的所有功能,但对于想要使用MinGW的程序员来说是理想的。
其他答案已经击中了目标。我只是想增加一个快速捕捉的例子。
MinGW (or MinGW-w64) Cygwin
-------------------- ------
Your program written Your program written
for Unix and GNU/Linux for Unix and GNU/Linux
| |
| |
V V
Heavy modifications Almost no modifications
| |
| |
V V
Compilation Compilation
Program compiled with Cygwin ---> Compatibility layer ---> Windows API
Program compiled with MinGW (or MingGW-w64) -------------> Windows API
从移植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.