cygwin下使用GCC生成的可执行文件
我正在使用 Cygwin 并已将 GCC (版本 4.3.4 20090804 (release) 1 )安装为 Cygwin 软件包。
当我在 Cygwin shell 下使用 GCC 构建 C 代码时,生成的可执行输出文件是类型为可执行文件(PE32executable for MS Windows (console) Intel 80386 32-bit),并且它只能在 Cygwin shell 下执行/运行,而不是作为Windows shell/命令提示符下的独立 .exe。如果我尝试在 Windows 命令提示符下独立运行它,则会出现一个错误窗口,显示“程序无法运行,因为您的计算机中缺少 cygwin.dll”。
如何使这个 .exe 独立,它可以在任何其他系统的命令提示符甚至在我自己的系统中执行?
我以为 Cygwin 下的 GCC 会构建 Linux 可执行文件(ELF 32 位 LSB 可执行文件),但事实并非如此。如何使用 gcc-cygwin 组合生成 *.out 类型的 Linux 可执行文件?
此外,我无法运行在 Linux-gcc 组合上生成的 Linux 可执行文件以在 Cygwin 下执行。
任何指示都会有帮助。
I am using Cygwin and have GCC (version 4.3.4 20090804 (release) 1 ) installed as Cygwin package.
When I built C code using GCC under Cygwin shell, the generated executable output file is a executable of type (PE32 executable for MS Windows (console) Intel 80386 32-bit) and it can only be executed/run under Cygwin shell, not as standalone .exe on Windows shell/command prompt. If I try to run it standalone on Windows command prompt it gives an error window saying "The program can't run because cygwin.dll is missing from your computer".
How can one make this .exe standalone, which can be executed on a command prompt of any other system or even in my own system?
I thought GCC under Cygwin would build a Linux executable (ELF 32-bit LSB executable), but it's not so. How can I use the gcc-cygwin combination to generate a *.out kind of Linux executable file?
Also, I cannot run a Linux executable generated on a Linux-gcc combination to execute under Cygwin.
Any pointers would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的路径中需要有 cygwin.dll。
或者直接使用 MinGW 编译原生 Windows 代码,无需依赖。
You need to have cygwin.dll in your path.
Or just use MinGW to compile native windows code without dependencies.
虽然不是完整的答案,但我想我可以给出一些指导。
1) http://www.cygwin.com/cygwin-ug-net/programming .html 表示您应该使用 -mswindows 参数。看看 MinGW。
2)你需要一个跨海湾合作委员会来做到这一点。默认情况下,cygwin gcc 生成链接到 cygwin.dll 的二进制文件。
3)因为它是linux二进制文件。最简单的方法是在cygwin中重新编译软件。
Not a complete answer, but I think I am able to give some pointers.
1) http://www.cygwin.com/cygwin-ug-net/programming.html says you should use -mswindows parameter. Take a look of MinGW.
2) You need a cross gcc to do this. By default cygwin gcc produces binaries linked against cygwin.dll.
3) That it because it is a linux binary. Easiest way is to recompile software in cygwin.
ad 1) 目前有三种不同的 mingw 交叉编译器可用于 cygwin:
旧的 gcc3 -mno-cygwin 已弃用。
有适用于 32 位 (mingw64-i686-gcc) 和 64 位 Windows 目标 (mingw64-x86_64-gcc) 的新 mingw64 交叉编译器。
目前还没有与官方 cygwin gcc4 编译器相匹配的 mingw-i686-gcc。
ad 2) 目前还没有像 cygwin 包那样的 Linux 交叉编译器。但人们报告说自己成功构建了这样一个交叉编译器。
ad 3) 没有 cygwin 交叉编译器作为 linux 软件包可用,但有很多 mingw 交叉编译器。这些mingw可执行文件也可以在cygwin下执行,尽管它们不能使用cygwin功能,只能使用简单的Windows运行时。
纠正其他人帖子中的错误:
-mswindows
无效,-mwindows
告诉链接器生成不带控制台的 GUI 应用程序。-mno-cygwin
仅对旧的已弃用的 gcc3 编译器有效,不再受支持。不要使用它。对于 cygwin,您应该像使用其他交叉编译器一样使用普通的主机和目标三元组。ad 1) There are currently three different mingw cross-compilers available for cygwin:
The old gcc3 -mno-cygwin is deprecated.
There are new mingw64 cross-compilers for 32 bit (mingw64-i686-gcc) and 64 bit windows targets (mingw64-x86_64-gcc).
There's no mingw-i686-gcc matching the official cygwin gcc4 compiler yet.
ad 2) There's no linux cross-compiler as cygwin package yet available. But people report success building such a cross-compiler by themselves.
ad 3) There's no cygwin cross-compiler as linux package available, but many mingw cross-compilers. Those mingw executables can also be executed under cygwin, though they cannot use cygwin features, just the simple windows runtime.
Correcting errors in others people posts:
-mswindows
is not valid,-mwindows
tells the linker to generate a GUI app without console.-mno-cygwin
is only valid for the old deprecated gcc3 compiler and is not supported anymore. Don't use it. With cygwin you should use ordinary host and target triples as with every other cross-compiler.尽管有广泛的谣言,Cygwin 并不是 Linux 模拟器,即它不生成或运行 Linux 可执行文件。为此,您需要虚拟机或 coLinux。
相反,Cygwin 是一个兼容层,旨在在 Windows 中实现尽可能多的 POSIX 和 Linux API。这意味着程序必须专门为 Cygwin 编译,但这也意味着它可以更好地与 Windows 集成,例如,它允许您在同一程序中混合 POSIX 和 Windows API。
cygwin1.dll 提供了该兼容性层。如果您使用 gcc-3 的 -mno-cygwin 开关或 MinGW 编译器之一构建非 Cygwin 可执行文件,那么您当然不能使用任何 POSIX/Linux 特定的 API,并且任何此类调用都需要替换为 Windows等价物。
Despite widespread rumours, Cygwin is not a Linux emulator, i.e. it doesn't produce or run Linux executables. For that you'll need a virtual machine or coLinux.
Instead, Cygwin is a compatibility layer, which aims to implement as much as possible of the POSIX and Linux APIs within Windows. This means that programs have to be compiled specifically for Cygwin, but it also means that it's better integrated with Windows, e.g. it lets you mix POSIX and Windows APIs in the same program.
It's the cygwin1.dll that provides that compatibility layer. If you build non-Cygwin executables using gcc-3's -mno-cygwin switch or one of the MinGW compilers, then of course you can't use any POSIX/Linux-specific APIs, and any such calls will need to be replaced with Windows equivalents.
Cygwin 是一个仿真层。如果您将 UNIX 代码编译为使用模拟层,它允许 UNIX 代码在 Windows 上运行。对于 Windows 来说,它看起来就像任何普通的 DLL 一样,并且可以像平常一样进行操作系统调用。但是,当您针对它进行编译时,它会显示与 UNIX 相同的功能(好吧,技术上是 POSIX,但它确实是 UNIX)
1) 当您使用 cygwin 构建时,它会自动引入此 cygwin1.dll。这是使它看起来像 UNIX 所需的代码。有标志使其不使用此 cygwin dll,这意味着忽略 UNIX 内容并使用本机 Windows 调用。 -mno-cygwin 标志将使二进制文件成为本机 Windows 二进制文件,而不是使用 cygwin 模拟。正如其他人所说,您还需要 -mwindows 来使用 cygwin gcc 制作实际的 GUI 应用程序。
2)要在一个平台上编译以在另一个平台上运行,您需要所谓的交叉编译器。您还需要其他系统的库和标头。对于你想要的东西,你需要一个 cygwin-Linux 交叉编译器、Linux 头文件和库。在虚拟机中运行 Linux 可能比设置它要容易得多。
3) 请记住,Cygwin 只是看起来像 UNIX 到 UNIX
源
代码,而不是 Linux 二进制文件。一旦你编译了东西,函数调用就是 Windows DLL 调用。 cygwin 程序仍然是 Windows 程序(这就是为什么您需要 Windows 风格的 DLL 来运行它)。 Cygwin 提供了 UNIX 函数/系统调用(例如 fork())的代码,但现在甚至调用它们的方法也不同了。 fork() 现在调用 Windows DLL,而不是 Linux 内核。这仍然是一个非常不同的系统。Cygwin is an emulation layer. It allows UNIX code to run on Windows, if you compile it to use the emulation layer. To Windows it looks like any normal DLL and makes OS calls as normal. But when you compile against it, it shows the same functions as UNIX (well, POSIX technically, but UNIX it is)
1) When you build with cygwin, it automatically brings in this cygwin1.dll. This is the code that you need to make it look like UNIX. There are flags to make it not use this cygwin dll, meaning ignore the UNIX stuff and use native Windows calls. the
-mno-cygwin
flag will make the binary a native Windows binary, not using cygwin emulation. As others have said, you also need the-mwindows
to make actual GUI apps using cygwin gcc.2) To compile on one platform to run on another platform, you need what's called a cross compiler. You also need the libraries and headers for the other system. For what you want you'd need a cygwin-Linux cross compiler, Linux headers and libraries. It would probably be much easier to run Linux in a virtual machine than to set this up.
3) Remember that Cygwin just looks like UNIX to UNIX
source
code, not to Linux binaries. Once you compile things, the function calls are windows DLL calls. A cygwin program is still a Windows program (which is why you need a Windows style DLL to run it). Cygwin provides code for UNIX functions/system calls such as fork(), but even the method of calling them is now different. fork() now calls into a Windows DLL, not into a Linux kernel. It's still a very different system.