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

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


当前回答

Cygwin模拟整个POSIX环境,而MinGW是仅用于编译的最小工具集(编译本地Win应用程序)。所以,如果你想让你的项目跨平台,MinGW显然是两个选择之一。

尽管你可能会考虑在Windows上使用VS,在Linux/ unix上使用GCC。大多数开源项目都这样做(例如Firefox或Python)。

其他回答

维基百科在这里做了一个比较。

来自Cygwin网站:

Cygwin是一个用于Windows的类似linux的环境。它由两部分组成:DLL (cygwin1.dll),作为Linux API模拟层,提供大量的Linux API功能。 提供Linux外观的工具集合。

摘自Mingw网站:

MinGW(“Minimalistic GNU for Windows”)是一个免费提供和可自由分发的Windows特定头文件和导入库的集合,结合了GNU工具集,允许人们生成不依赖任何第三方C运行时dll的本地Windows程序

阅读这些已回答的问题,了解Cygwin和MinGW之间的区别。


问题#1:我想创建一个应用程序,我写源代码一次,编译一次,并在任何平台上运行(例如Windows, Linux和Mac OS X…)

答案#1:把源代码写进去 JAVA。编译源代码一次,然后 在任何地方运行它。


问题2:我想创建一个应用程序,我只写一次源代码,但我可以分别为任何平台编译源代码(例如Windows, Linux和Mac OS X…)

答案#2:用C语言编写源代码 或c++。使用标准头文件 只有。使用合适的编译器 平台(如Visual Studio for Windows, GCC for Linux, XCode for Mac)。注意,您不应该使用任何一种 高级编程特性 全部编译源代码 成功的平台。如果你使用 没有C或c++标准类或 函数,你的源代码没有 在其他平台编译。


问题#3:回答问题#2,在每个平台上使用不同的编译器是困难的,有跨平台的编译器吗?

答案#3:是的,使用GCC编译器。它 是一个跨平台编译器。来 在Windows中编译源代码 使用MinGW提供GCC编译器 并编译您的源代码 代码转换为本地Windows程序。不 使用任何高级编程特性 (如Windows API)来编译你的 所有平台的源代码 成功。如果你使用Windows API 函数,你的源代码没有 在其他平台编译。


问题4:C或c++标准头文件不提供任何高级编程特性,如多线程。我该怎么办?

Answer #4: You should use POSIX (Portable Operating System Interface [for UNIX]) standard. It provides many advanced programming features and tools. Many operating systems fully or partly POSIX compatible (like Mac OS X, Solaris, BSD/OS and ...). Some operating systems while not officially certified as POSIX compatible, conform in large part (like Linux, FreeBSD, OpenSolaris and ...). Cygwin provides a largely POSIX-compliant development and run-time environment for Microsoft Windows.


因此:

要在Windows下充分利用GCC跨平台编译器的优势,请使用MinGW。

要在Windows中使用POSIX标准高级编程特性和工具的优势,请使用Cygwin。

   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

维基百科说:

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使用兼容层,而MinGW是本地的。这是主要的区别之一。