在 Windows 上运行用 Linux 编写的 C 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3474685/
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 15:01:51  来源:igfitidea点击:

Run C program written in Linux on Windows

cwindowslinux

提问by Tanaji

I have C program which I wrote in Linux that runs very well. Now I want to run it on Windows.

我有我在 Linux 中编写的 C 程序,它运行得很好。现在我想在 Windows 上运行它。

How do I easily run it on Windows?

如何在 Windows 上轻松运行它?

回答by RBerteig

Elaborating a bit on the answers from cafand jartieda...

阐述了一下从答案咖啡馆jartieda...

Cygwinis an attempt to emulate a (nearly) complete POSIX execution environment in a native Windows process. It is complete enough that a surprising amount of Unix application code simply compiles and runs using the familiar ./configure && make && make installidiom. This trick was done by supplying a DLL that emulates POSIX system calls using the Windows API. Based on that, you get a complete GCC toolchain, bash, and all the usual command line utilities you are used to. One downside is that the compiled program is dependent on the Cygwin DLL, which makes it tricky to deliver the result to a system that does not already have Cygwin installed and whose user doesn't want to use a Unix shell.

Cygwin试图在本机 Windows 进程中模拟(几乎)完整的 POSIX 执行环境。它已经足够完整了,数量惊人的 Unix 应用程序代码只需使用熟悉的./configure && make && make install习惯用法即可编译和运行。这个技巧是通过提供一个使用 Windows API 模拟 POSIX 系统调用的 DLL 来完成的。在此基础上,您将获得完整的 GCC 工具链、bash 以及您习惯的所有常用命令行实用程序。一个缺点是编译的程序依赖于 Cygwin DLL,这使得将结果传送到尚未安装 Cygwin 且其用户不想使用 Unix shell 的系统变得棘手。

MinGWis a port of the GCC toolchain that generates native Windows applications that depend on the well known (and distributed with Windows itself) MSVCRT.DLL C runtime library. It makes no attempt to emulate a POSIX operating system, but applications that are mostly based on the standard C libraries, will often build essentially unchanged.

MinGW是 GCC 工具链的一个端口,它生成本机 Windows 应用程序,这些应用程序依赖于众所周知的(并与 Windows 本身一起分发)MSVCRT.DLL C 运行时库。它没有尝试模拟 POSIX 操作系统,但主要基于标准 C 库的应用程序通常会在本质上保持不变。

MSYSis a compile-time environment that provides enough Unix utilities (including bash) to often allow ./configureto run, and if the project supports the results, finish the build with MinGW's GCC. The result is a native Windows executable that does not depend on any DLLs you don't deliberately use aside from MSVCRT.DLL. Although the MSYS environment itself was a fork of an early version of the Cygwin project, it is primarily intended to be used to provide a unix-like place to compile native Windows applications: one would generally notbuild new applications based on its runtime environment.

MSYS是一个编译时环境,它提供了足够的 Unix 实用程序(包括 bash)以经常允许./configure运行,如果项目支持结果,则使用 MinGW 的 GCC 完成构建。结果是本机 Windows 可执行文件不依赖于除 MSVCRT.DLL 之外您无意使用的任何 DLL。尽管 MSYS 环境本身是 Cygwin 项目早期版本的一个分支,但它主要用于提供一个类似 Unix 的地方来编译本机 Windows 应用程序:人们通常不会基于其运行时环境构建新的应用程序。

Another approach to compiling for Windows is to use the MinGW cross compiler on linux. A number of MinGW's core developers work that way, testing they product either under Wine, or under Windows running in a VM or a separate PC.

另一种为 Windows 编译的方法是在 linux 上使用 MinGW 交叉编译器。许多 MinGW 的核心开发人员以这种方式工作,在 Wine 或在 VM 或单独 PC 中运行的 Windows 下测试他们的产品。

If the program has a GUI component, then you may have additional difficulties. Some GUI frameworks are available for both Linux and Windows. Qt, wxWidgets, and IUPall leap to mind, but there are others out there.

如果程序具有 GUI 组件,那么您可能会遇到其他困难。一些 GUI 框架可用于 Linux 和 Windows。QtwxWidgetsIUP都会浮现在脑海中,但还有其他的。

Edit:I've improved the paragraph above about MSYS to emphasize that it is intended to be a compile-time environment for building programs that run as native Windows applications, and not a full POSIX environment like Cygwin.

编辑:我改进了上面关于 MSYS 的段落,以强调它旨在成为一个编译时环境,用于构建作为本机 Windows 应用程序运行的程序,而不是像 Cygwin 这样的完整 POSIX 环境。

Note, also, that there is always the option of porting the project to one of the more traditional compilers for Windows development. Open Watcom, the Borland compilers, and Microsoft all have free or inexpensive versions, although often enough not under licenses that make the opens source community entirely happy.

另请注意,始终可以选择将项目移植到用于 Windows 开发的更传统的编译器之一。Open Watcom、Borland 编译器和 Microsoft 都有免费或廉价的版本,尽管通常没有获得让开源社区完全满意的许可证。

This approach probably requires more effort because the differences between a Linux environment and the Windows environment become more visible when you also switch from one toolchain to another. This is especially true of the Microsoft compilers which are historically not as fully standards compliant as GCC.

这种方法可能需要更多的努力,因为当您也从一个工具链切换到另一个工具链时,Linux 环境和 Windows 环境之间的差异变得更加明显。对于 Microsoft 编译器来说尤其如此,这些编译器历来不像 GCC 那样完全符合标准。

回答by caf

The easiest way is to install the Cygwinenvironment, and compile it under that.

最简单的方法是安装Cygwin环境,然后在该环境下进行编译。

回答by jartieda

best way recompile it all using mingw compiler.

最好的方法是使用 mingw 编译器重新编译它。

easy way recompile it under cygwin environment.

在cygwin环境下重新编译它的简单方法。

回答by Eike

The answers you got so far, focus on installing the GNU operating systemon the Windows kernel; but you could also use freeware tools from Microsoft. This solution is probably most attractive if you want to continue to develop on Linux, and only do a little work for porting on Windows:

到目前为止你得到的答案,重点是在Windows 内核上安装GNU 操作系统;但您也可以使用 Microsoft 的免费软件工具。如果您想继续在 Linux 上开发,并且只为在 Windows 上移植做一点工作,那么这个解决方案可能是最有吸引力的:

Microsoft offers a simplified version of their development environment as freeware: Visual Studio Express(download). If your program does only file IO it will probably compile unmodified, because Microsoft supplies the C standard library too.

Microsoft 以免费软件的形式提供了其开发环境的简化版本:Visual Studio Express下载)。如果您的程序只执行文件 IO,它可能会未经修改地编译,因为 Microsoft 也提供了 C 标准库。

For GUI I also wholeheartedly recommend the Qtframework. It is very well written and documented; it is now licensed under the LGPL, so you can link it to proprietary applications without any cost. However it is written in C++ so you would need to change the programing language. For integration of QT with Visual Studio Express: I believe it works, but you need to try it out. About five years ago I tested the integration of (proprietary) QT and (professional) Visual Studio for my employer, and it worked perfectly.

对于 GUI,我也全力推荐Qt框架。它写得很好,有据可查;它现在根据LGPL获得许可,因此您可以将其链接到专有应用程序而无需任何费用。但是它是用 C++ 编写的,因此您需要更改编程语言。对于 QT 与 Visual Studio Express 的集成:我相信它有效,但您需要尝试一下。大约五年前,我为我的雇主测试了(专有的)QT 和(专业的)Visual Studio 的集成,它运行良好。