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

时间:2021-03-04 17:30:50

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

我有一个C程序,我在Linux中运行得非常好。现在我想在Windows上运行它。

How do I easily run it on Windows?

如何在Windows上轻松运行它?

4 个解决方案

#1


27  

Elaborating a bit on the answers from caf and jartieda...

详细阐述了caf和jartieda的答案......

Cygwin is 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 install idiom. 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的系统变得棘手。

MinGW is 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本身一起分发)MSVCRT.DLL C运行时库的本机Windows应用程序。它不会尝试模拟POSIX操作系统,但主要基于标准C库的应用程序通常基本上不会构建。

MSYS is a compile-time environment that provides enough Unix utilities (including bash) to often allow ./configure to 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 not build 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中运行的Windows或单独的PC上测试他们的产品。

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 IUP all leap to mind, but there are others out there.

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

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开发的一个更传统的编译器上。打开Watcom,Borland编译器和微软都有免费或廉价的版本,尽管通常不足以使开源社区完全满意。

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环境之间的差异变得更加明显。对于历史上不像GCC那样完全符合标准的Microsoft编译器来说尤其如此。

#2


3  

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

最简单的方法是安装Cygwin环境,并在其下编译。

#3


2  

best way recompile it all using mingw compiler.

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

easy way recompile it under cygwin environment.

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

#4


1  

The answers you got so far, focus on installing the GNU operating system on 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操作系统;但你也可以使用微软的免费工具。如果你想继续在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 Qt framework. 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的集成,它运行得很好。

#1


27  

Elaborating a bit on the answers from caf and jartieda...

详细阐述了caf和jartieda的答案......

Cygwin is 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 install idiom. 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的系统变得棘手。

MinGW is 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本身一起分发)MSVCRT.DLL C运行时库的本机Windows应用程序。它不会尝试模拟POSIX操作系统,但主要基于标准C库的应用程序通常基本上不会构建。

MSYS is a compile-time environment that provides enough Unix utilities (including bash) to often allow ./configure to 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 not build 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中运行的Windows或单独的PC上测试他们的产品。

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 IUP all leap to mind, but there are others out there.

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

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开发的一个更传统的编译器上。打开Watcom,Borland编译器和微软都有免费或廉价的版本,尽管通常不足以使开源社区完全满意。

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环境之间的差异变得更加明显。对于历史上不像GCC那样完全符合标准的Microsoft编译器来说尤其如此。

#2


3  

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

最简单的方法是安装Cygwin环境,并在其下编译。

#3


2  

best way recompile it all using mingw compiler.

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

easy way recompile it under cygwin environment.

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

#4


1  

The answers you got so far, focus on installing the GNU operating system on 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操作系统;但你也可以使用微软的免费工具。如果你想继续在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 Qt framework. 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的集成,它运行得很好。