为什么 GCC-Windows 依赖于 cygwin?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/187990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:25:26  来源:igfitidea点击:

Why does GCC-Windows depend on cygwin?

windowsgcccygwinposixcompiler-construction

提问by benjismith

I'm not a C++ developer, but I've always been interested in compilers, and I'm interested in tinkering with some of the GCC stuff (particularly LLVM).

我不是 C++ 开发人员,但我一直对编译器很感兴趣,而且我对修补 GCC 的一些东西(尤其是 LLVM)很感兴趣。

On Windows, GCC requires a POSIX-emulation layer (cygwin or MinGW) to run correctly.

在 Windows 上,GCC 需要 POSIX 仿真层(cygwin 或 MinGW)才能正确运行。

Why is that?

这是为什么?

I use lots of other software, written in C++ and cross-compiled for different platforms (Subversion, Firefox, Apache, MySQL), and none of them require cygwin or MinGW.

我使用了许多其他软件,用 C++ 编写并针对不同平台(Subversion、Firefox、Apache、MySQL)进行交叉编译,它们都不需要 cygwin 或 MinGW。

My understanding about C++ best-practice programming is that you can write reasonably platform-neutral code and deal with all the differences during the compilation process.

我对 C++ 最佳实践编程的理解是,您可以编写合理的平台中立代码并处理编译过程中的所有差异。

So what's the deal with GCC? Why can't it run natively on Windows?

那么与 GCC 有什么关系呢?为什么它不能在 Windows 上本地运行?



EDIT:

编辑:

Okay, the two replies so far say, basically, "GCC uses the posix layer because it uses the posix headers".

好的,到目前为止的两个回复基本上说,“GCC 使用 posix 层,因为它使用 posix 标头”。

But that doesn't really answer the question.

但这并不能真正回答问题。

Let's say I already have a set of headers for my favorite standard library. Why would I still need the posix headers?

假设我已经有一组我最喜欢的标准库的头文件。为什么我仍然需要 posix 标头?

Does GCC require cygwin/mingw to actually RUN?

GCC 是否需要 cygwin/mingw 才能实际运行

Or does it only need the emulation layer for headers and libraries? If so, why can't I just give it a "lib" directory with the required resources?

或者它只需要头文件和库的仿真层?如果是这样,为什么我不能给它一个包含所需资源的“lib”目录?



EDIT AGAIN:

再次编辑:

Okay, I'll try again to clarify the question...

好的,我会再次尝试澄清问题...

I also write code in the D Programming Language. The official compiler is named "dmd" and there are official compiler binaries for both Windows and linux.

我还用D 编程语言编写代码。官方编译器名为“dmd”,Windows 和 linux 都有官方编译器二进制文件。

The Windows version doesn't require any kind of POSIX emulation. And the Linux version doesn't require any kind of Win32 emulation. If the compiler has assumptions about its environment, it hides those assumptions pretty well.

Windows 版本不需要任何类型的 POSIX 仿真。Linux 版本不需要任何类型的 Win32 模拟。如果编译器对其环境有假设,它就会很好地隐藏这些假设。

Of course, I have to tell the compiler where to find the standard library and where to find libraries to statically or dynamically link against.

当然,我必须告诉编译器在哪里可以找到标准库以及在哪里可以找到静态或动态链接的库。

GCC, by contrast, insists on pretending it's operating within a posix environment, and it asks ME to humor those assumptions by setting up an emulation layer.

相比之下,GCC 坚持假装它在 posix 环境中运行,并要求 ME 通过设置仿真层来满足这些假设。

But what, exactly, within GCC relies on that layer? Is it just looking for stdlib headers, and it assumes it'll find those headers within "/usr/lib"?

但是,在 GCC 中,究竟什么依赖于该层?它是否只是在寻找 stdlib 头文件,并且假定它会在“/usr/lib”中找到这些头文件?

If that's the case, shouldn't I just be able to tell it to look in "C:/gcc/lib" to find those header files?

如果是这种情况,我不应该只能告诉它在“C:/gcc/lib”中查找这些头文件吗?

Or does GCC itself rely on the POSIX libraries to access the file system (and to do other low-level stuff)? If that's the case, then I wonder why they don't just statically link with their favorite windows POSIX libraries. Why require the user to set up the dependencies, when they could build those dependencies right into the application?

或者 GCC 本身是否依赖 POSIX 库来访问文件系统(并执行其他低级操作)?如果是这样,那么我想知道为什么他们不只是静态链接到他们最喜欢的 Windows POSIX 库。为什么要求用户设置依赖项,当他们可以将这些依赖项直接构建到应用程序中时?

回答by David Cournapeau

Actually, the question premise is wrong: MinGWGCC does NOTrequire Cygwin.

其实,问题的前提是错误的:MinGW的GCC不会要求Cygwin的。

You will see you don't need Cygwin at all. It runs natively on Windows (32-bit, at least). Both the toolchain and the produced binaries are independent of Cygwin.

你会发现你根本不需要 Cygwin。它在 Windows(至少 32 位)上本地运行。工具链和生成的二进制文件都独立于 Cygwin。

The MinGW compilers available in Cygwin are different: they are built on the Cygwin platform, to generate code which does not depend on the Cygwin runtime. The compilers themselves do depend on Cygwin in that case. But that's because you installed them from Cygwin.

Cygwin 中可用的 MinGW 编译器是不同的:它们构建在 Cygwin 平台上,以生成不依赖于 Cygwin 运行时的代码。在这种情况下,编译器本身确实依赖于 Cygwin。但那是因为您是从 Cygwin 安装的。

回答by Brad Gilbert

The Cygwinversion of GCC requires Cygwinto be installed, for programs it compiles.

GCC的Cygwin版本需要安装Cygwin,用于它编译的程序。

The MinGWversion does not require anything after compiling, other than a working copy of Windows.

MinGW的版本不需要编译后的任何其他比Windows的工作副本。

You can't really mix the Cygwin environment, and the MinGW compilers together, because Cygwin changes the paths of the precompiled libraries.

您不能真正将 Cygwin 环境和 MinGW 编译器混合在一起,因为 Cygwin 更改了预编译库的路径。

If you need a bash style shell, but don't want to use Cygwin, I would recommend MSYS.

如果你需要一个 bash 风格的 shell,但不想使用 Cygwin,我会推荐MSYS

Cygwin in contrast to MinGW

Cygwin 与 MinGW 的对比

copied from MinGW Wiki

复制自MinGW Wiki

Cygwin applications by principle are not considered a "Native Win32 application" because it relies on the Cygwin? POSIX Emulation DLL or cygwin1.dllfor Posix functions and does not use win32 functions directly. MinGW on the other hand, provides functions supplied by the Win32 API. While porting applications under MinGW, functions not native to Win32 such as fork(), mmap()or ioctl()will need to be reimplemented into Win32 equivalents for the application to function properly.

回答by AJ S.

POSIX (Portable Operating System Interface) "is an evolving, growing document that is being produced by IEEE and standardized by ANSI and ISO. The goal of POSIX is the source-code portability of application" [1].

POSIX(便携式操作系统接口)“是由 IEEE 生成并由 ANSI 和 ISO 标准化的不断发展的、不断增长的文档。POSIX 的目标是应用程序的源代码可移植性”[1]。

In practical terms, the goal is defined as the ability to write one source implementation and have it run on different (POSIX-compliant) systems with only recompilation.

实际上,目标被定义为能够编写一个源实现并使其在不同的(符合 POSIX 的)系统上运行,而只需重新编译。

GCC is a compiler capable of delivering that promise and as such, it needs a layer of code that brings a machine "up to" POSIX standards.

GCC 是一个能够实现这一承诺的编译器,因此,它需要一层使机器“达到”POSIX 标准的代码。

That is the core of the answer to your question.

这是你问题答案的核心。

To help see what I mean, I'll offer you this exercise:

为了帮助理解我的意思,我将为您提供以下练习:

  • Write a program with no OS-specific #ifdefs that takes as input from the user some directory path and writes to stdout a listing of its contents (one level).
  • 编写一个没有特定于操作系统的 #ifdefs 的程序,它将用户的某个目录路径作为输入,并将其内容列表(一级)写入标准输出。

I think you will find that it is very difficult to write code that uses only the native WIN32 API that compiles on any UNIX or LINUX system

我想您会发现编写仅使用可在任何 UNIX 或 LINUX 系统上编译的本机 WIN32 API 的代码是非常困难的

It will be only slightly less difficult to write code that uses POSIX API - as you can on any LINUX box - and have it compile under Windows (DevStudio2005 has a surprising number of POSIX-compliant headers now...you might be able to get close).

编写使用 POSIX API 的代码的难度会稍微小一些——就像在任何 LINUX 机器上一样——并让它在 Windows 下编译(DevStudio2005 现在有数量惊人的 POSIX 兼容头文件......你可能能够得到关闭)。

Take you LINUX program from above and now compile it under GCC running under Cygwin or MinGW. I'll bet it compiles and runs.

从上面带你 LINUX 程序,现在在 Cygwin 或 MinGW 下运行的 GCC 下编译它。我敢打赌它会编译并运行。

How did GCC perform that bit of magic? The POSIX headers and implementations underlying them provided by Cygwin or MinGW.

GCC 是如何施展这种魔法的?Cygwin 或 MinGW 提供的 POSIX 标头和它们底层的实现。

Does GCC's reliance on Cygwin/MinGW under Windows make more sense now?

GCC 对 Windows 下的 Cygwin/MinGW 的依赖现在更有意义了吗?

  1. POSIX.4: Programming for the Real World, Bill O. Gallmeister, O'Reilly & Associates, Inc., pg 2
  1. POSIX.4:现实世界的编程,Bill O. Gallmeister,O'Reilly & Associates, Inc.,第 2 页

回答by Walter Bright

I try to make my programs under Windows behave like a good Windows citizen, and under Linux like a good Linux citizen.

我试图让我的程序在 Windows 下表现得像一个优秀的 Windows 公民,在 Linux 下表现得像一个优秀的 Linux 公民。

回答by Artyom

Why? Because when GCCwas created, Windows 32 bit even hadn't exits...

为什么?因为在创建GCC时,Windows 32 位甚至还没有退出......

To be more correct --- it was developed for UNIX/Posix OS. Later it was ported to windows.

更准确地说——它是为 UNIX/Posix OS 开发的。后来它被移植到windows。

Windows is not POSIX compient system. It even does not provide very basic functionality. Try to find readdiror statunder Windows compiler? And this is so extreamly basic functionality that you need to write compiler!

Windows 不是 POSIX 兼容系统。它甚至不提供非常基本的功能。试试看readdir还是stat在windows下编译?这是非常基本的功能,您需要编写编译器!

Just to be clear, GCC compiled programs usually required only one mingw32.dll to add missing functionality to be able to run.

需要说明的是,GCC 编译的程序通常只需要一个 mingw32.dll 来添加缺失的功能即可运行。

So... You ask why GCC requires some POSIX layer? Because Windows OS is not POSIX operating system.

所以...你问为什么 GCC 需要一些 POSIX 层?因为 Windows 操作系统不是 POSIX 操作系统。

回答by Sebastian Mach

I'm not a C++ developer, but I've always been interested in compilers, and I'm interested in tinkering with some of the GCC stuff (particularly LLVM)

我不是 C++ 开发人员,但我一直对编译器很感兴趣,而且我有兴趣修改一些 GCC 的东西(特别是 LLVM)

Note that LLVM and GCC are not related. LLVM is largely the result of the research done by Chris Lattner (http://llvm.org/developers.cgi) about modern optimization. His papers are available on http://llvm.org. Nowadays, it is heavily sponsored by Apple. GCC's C/C++/Obj-C frontend is used for llvm-gcc, which emits LLVM machine code (and after a ton of optimizations in llvm, a final executable comes out); llvm-gcc is a kind of hack to couple some ready C/C++/Obj-C-frontend to LLVM.

请注意,LLVM 和 GCC 不相关。LLVM 主要是 Chris Lattner ( http://llvm.org/developers.cgi) 关于现代优化的研究结果。他的论文可以在http://llvm.org 上找到。如今,它得到了苹果的大力赞助。GCC 的 C/C++/Obj-C 前端用于 llvm-gcc,它发出 LLVM 机器代码(在 llvm 中经过大量优化,最终的可执行文件出来);llvm-gcc 是一种将一些现成的 C/C++/Obj-C 前端耦合到 LLVM 的技巧。

Note anyways that the LLVM crew also builds an own, complete C/C++/Obj-C compiler, called clang. It's C implementation is near complete, C++ support is getting better and better, dunno about Obj-C though.

无论如何请注意,LLVM 团队还构建了一个自己的、完整的 C/C++/Obj-C 编译器,称为 clang。它的 C 实现接近完成,C++ 支持越来越好,但不知道 Obj-C。

So, if someone says "compiler llvm", he really either means llvm-gcc, or clang. LLVM itself is just the Low Level Virtual Machine, with only a handful of instructions in Static Single Assignment form (approx. 32 instructions, afair), but a megaton of optimization passes upon thy syntax tree.

所以,如果有人说“编译器 llvm”,他的意思要么是 llvm-gcc,要么是 clang。LLVM 本身只是一个低级虚拟机,只有少数静态单分配形式的指令(大约 32 条指令),但是大量的优化传递给你的语法树。

回答by Remo.D

Windows does not offer a standard POSIX library so cygwin provides one (cygwin1.dll). The gcc packages that comes with cygwin uses it.

Windows 不提供标准的 POSIX 库,因此 cygwin 提供了一个 (cygwin1.dll)。cygwin 附带的 gcc 包使用它。

mingw, on the other hand, does not necessarely provide a POSIX layer. The mingw installation that I use, for example, does not even have a pthread library.

另一方面,mingw 并不一定提供 POSIX 层。例如,我使用的 mingw 安装甚至没有 pthread 库。

Should I need it I would have to install it. Mingw-gcc produces Win32 native code (and in fact relies on MSVCRT.DLL).

如果我需要它,我将不得不安装它。Mingw-gcc 生成 Win32 本机代码(实际上依赖于 MSVCRT.DLL)。

EDIT: reading your edit I'm no longer sure if you're asking why gcc itself needs mingw/cygwin libraries or if the programs compiled with gcc on Win require those libraries

编辑:阅读您的编辑我不再确定您是否在问为什么 gcc 本身需要 mingw/cygwin 库,或者在 Win 上使用 gcc 编译的程序是否需要这些库

回答by Dark Shikari

Much of that software that is compiled for different platforms is compiled... in MinGW. The only difference with gcc is that it is a compiler itself, which means it needs all the headers that normally get compiled in with the program, and normally which one does not need to run the resulting program.

为不同平台编译的大部分软件都是在 MinGW 中编译的。与 gcc 的唯一区别是它本身就是一个编译器,这意味着它需要通常与程序一起编译的所有头文件,并且通常不需要运行生成的程序。

回答by tim18

You have an option when building gcc to specify enable-threads options other than posix, if you don't want to support pthreads or OpenMP. Needless to say, those aren't as well tested. There is some 3rd party closed source support for OpenMP with Windows threads, but its use with gcc appears to violate the license. As the Windows pthreads library is a higher level interface to Windows threads functionality, it's perhaps not surprising when it doesn't perform as well or encounters Microsoft's rejection of affinity support. Only recently has Microsoft begun to tolerate gcc on Windows. There was a time when they actually said they would not work on bugs reported by gcc users, even if they could be reproduced with exclusively Microsoft tools.

如果您不想支持 pthreads 或 OpenMP,则在构建 gcc 时可以选择指定 posix 以外的启用线程选项。不用说,那些没有经过很好的测试。使用 Windows 线程对 OpenMP 有一些 3rd 方闭源支持,但它与 gcc 的使用似乎违反了许可证。由于 Windows pthreads 库是 Windows 线程功能的更高级别接口,因此当它表现不佳或遇到 Microsoft 拒绝关联性支持时,这可能不足为奇。直到最近,Microsoft 才开始在 Windows 上容忍 gcc。曾经有一段时间,他们实际上表示他们不会处理 gcc 用户报告的错误,即使它们可以仅使用 Microsoft 工具重现。

回答by Tom Johnson

The MinGW-w64 port of gcc creates native code for 32- and 64-bit Windows without any additional dependencies. See for example http://mstenberg.com/blog/2010/06/13/gcc-for-windows/for a quick getting-started guide.

gcc 的 MinGW-w64 端口为 32 位和 64 位 Windows 创建本机代码,没有任何额外的依赖项。例如,请参阅http://mstenberg.com/blog/2010/06/13/gcc-for-windows/以获取快速入门指南。