简体   繁体   English

在Windows和Linux上进行交叉编译

[英]Cross-compiling on Windows and Linux

I am developing an application that will have to run on both Windows and Linux. 我正在开发一个必须在Windows和Linux上运行的应用程序。

So far I have it running on Linux using GCC 4.4.1 using my Makefile. 到目前为止,我使用我的Makefile在使用GCC 4.4.1的Linux上运行它。

However, I also need to compile on Windows. 但是,我还需要在Windows上编译。 The source code will compile on Windows as I have #defined all the areas of the code that separate the different compilers. 源代码将在Windows上编译,因为我已经#defined分隔了不同编译器的代码的所有区域。 Ie: 即:

#ifdefined (LINUX)
/* Do linux stuff */
#else
/* Do windows stuff */
#endif

And so far the code is very simple as I am just starting this program. 到目前为止,代码非常简单,因为我刚刚开始这个程序。 Basically I just want to test my idea. 基本上我只想测试我的想法。

However, is it as simple as compiling the source code on Linux? 但是,它是否像在Linux上编译源代码一样简单?

Then when I want to compile on to Windows I could copy the files to the Windows platform. 然后,当我想编译到Windows时,我可以将文件复制到Windows平台。 Then opening the source code files in Visual Studio C++ and creating a project and then compiling to create my binary? 然后在Visual Studio C ++中打开源代码文件并创建项目然后编译以创建我的二进制文件?

看看CMake吧。

Using Visual C++ is one way of building your C project on Windows. 使用Visual C ++是在Windows上构建C项目的一种方法。 It's not the only way, though. 不过,这不是唯一的方法。

VC has project files that you will need to create. VC包含您需要创建的项目文件。 But then realize that you'll have to maintain the build configuration in two different places (make & .vcproj). 但后来意识到你必须在两个不同的地方维护构建配置(make&.vcproj)。

There are other options as well: 还有其他选择:

If I were you I'd try to use as few different build tools as possible. 如果我是你,我会尝试使用尽可能少的不同构建工具。

SCons is an excellent cross-platform build system. SCons是一个出色的跨平台构建系统。 Instead of writing a Makefile , you write a SConstruct file, which is usually simpler and yet more powerful. 您可以编写SConstruct文件,而不是编写Makefile ,该文件通常更简单但功能更强大。 The scons command will (by default) build for the operating system on which it's run, providing an easy way to manage cross-platform builds. scons命令(默认情况下)将为运行它的操作系统构建,提供了一种管理跨平台构建的简便方法。

For simple console applications (and even more complex ones, like the Qt framework) you can build on Windows using MinGW . 对于简单的控制台应用程序(甚至更复杂的应用程序,如Qt框架),您可以使用MinGW在Windows上构建 Building with MinGW is pretty similar to building on Linux, and it isn't unlikely that you can use the same Makefile with no or only minor modifications. 使用MinGW进行构建非常类似于在Linux上构建,并且您不可能在没有修改或仅进行微小修改的情况下使用相同的Makefile This has the major advantage that you don't need to maintain two sets of Makefiles / project files. 这样做的主要优点是您不需要维护两组Makefile /项目文件。

You can also use Cygwin , which basically is an emulation layer of the Linux API for the substantial functionality. 您还可以使用Cygwin ,它基本上是Linux API的仿真层,用于实质性功能。 Using Cygwin however has the disadvantage that it links to its own library cygwin1.dll . 然而,使用Cygwin的缺点是它链接到自己的库cygwin1.dll This might not be an issue for you as you can simply ship this DLL with your program. 这对您来说可能不是问题,因为您只需将此DLL与您的程序一起发送即可。 You can also suppress using this by using the option -mno-cygwin as compiler option for gcc / g++ . 您还可以使用选项-mno-cygwin作为gcc / g++编译器选项来禁止使用它。 However, for some functionality the cygwin1.dll functionality is required. 但是,对于某些功能,需要cygwin1.dll功能。

I can recommend MinGW for building programs on Windows that have originally been written for Linux. 我可以推荐MinGW在Windows上构建最初为Linux编写的程序。 I'm using this for various command line programs, and for GUI applications I'm using the Qt framework with the MinGW compiler. 我将它用于各种命令行程序,对于GUI应用程序,我使用Qt框架和MinGW编译器。 If you want to build GUI applications you should definitely use a platform independent GUI framework as it makes things much easier. 如果你想构建GUI应用程序,你肯定应该使用独立于平台的GUI框架,因为它使事情变得更容易。

CMake can help you keep your build portable across several toolchains. CMake可以帮助您在多个工具链中保持构建的可移植性。 However, you still have to deal with the incompatibilities between those at the language and library level. 但是,您仍然必须处理语言和库级别之间的不兼容性。

Which is why I recommend using GCC, the autotools, etc on all platforms. 这就是为什么我建议在所有平台上使用GCC,autotools等。 On windows, the GNU toolchain is called MinGW/MSys. 在Windows上,GNU工具链称为MinGW / MSys。 Look at MSysGit for an example. 以MSysGit为例。

You may be able to use MinGW to cross-compile Windows binaries directly from Linux. 您可以使用MinGW直接从Linux交叉编译Windows二进制文件。 Most of the popular Linux distros now have MinGW packages available that make this very straightforward. 大多数流行的Linux发行版现在都有MinGW软件包,这使得它非常简单。

Then you can build all your Windows and Linux binaries in one go, on one machine, from one Makefile. 然后,您可以在一台计算机上一次性构建所有Windows和Linux二进制文件。 No need to mess around trying to get GNU Make to work properly in Windows, let alone worry about Visual Studio. 无需为了让GNU Make在Windows中正常工作而烦恼,更不用说担心Visual Studio了。

Yes. 是。 It's that simple. 就这么简单。 Just keep in mind the whole time that you need to enclose all OS-specific stuff in #ifdef s. 请记住,您需要在#ifdef包含所有特定于操作系统的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM