C++跨平台编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25153044/
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
C++ cross platform compiling
提问by Chandra Nakka
I'm using Windows 7 - 32bitoperating system.
我使用的是Windows 7 - 32 位操作系统。
I want to compile my simple C++ source code into executable for all OSes and architectures.
我想将我的简单 C++ 源代码编译为适用于所有操作系统和体系结构的可执行文件。
I want to compile for this below operating systems from my OS.
我想从我的操作系统编译以下操作系统。
- Windows 32
- Windows 64
- Linux 32
- Linux 64
- OSX 32
- OSX 64
- 视窗 32
- 视窗 64
- Linux 32
- Linux 64
- OSX 32
- OSX 64
Is it possible or not?
有可能吗?
Note:I need to compile C++ for all OSes with only Win7 32bit.
注意:我需要为只有 Win7 32 位的所有操作系统编译 C++。
回答by Stefan Weiser
It is much easier to compile it on the target OS than cross compiling it. What you need is a toolchain for every OS and a "make" tool. CMake has powerful crosscompiling abilities. This is not a necessity, but it will save some money: Get virtualization software (e.g. VMWare Player is free) and run on a different OS.
在目标操作系统上编译它比交叉编译要容易得多。您需要的是适用于每个操作系统的工具链和“make”工具。CMake 具有强大的交叉编译能力。这不是必需的,但可以节省一些钱:获取虚拟化软件(例如 VMWare Player 是免费的)并在不同的操作系统上运行。
I would recommend clang (OSX), gcc (Linux), TDM gcc (Windows) as toolchains (MSVC is also nice, but is not free). The difference between 32bit and 64bit should not be the problem. When you are working with different compilers, I advise you to stick to the standard by turning the most pedantic compiler flags on at each.
我会推荐 clang (OSX)、gcc (Linux)、TDM gcc (Windows) 作为工具链(MSVC 也不错,但不是免费的)。32bit 和 64bit 的区别应该不是问题。当您使用不同的编译器时,我建议您通过在每个编译器上打开最迂腐的编译器标志来坚持标准。
I would also recommend you to have a continuous integration server somewhere with one client for every OS target and architecture. This will ease the pain of incompatible changes that you make in one OS. And you will also be able to ensure installation by package manager or installer.
我还建议您在某处拥有一个持续集成服务器,每个操作系统目标和架构都有一个客户端。这将减轻您在一个操作系统中所做的不兼容更改的痛苦。您还可以确保通过包管理器或安装程序进行安装。
Just search the web for further readings on cross compilation, toolchains and continuous integration.
只需在网上搜索有关交叉编译、工具链和持续集成的进一步阅读。
回答by aaragon
You need a build system like the auto tools or CMake, and I recommend the latter. There is a Python utility called cookiecutterthat allows you to create a simple CMake/C++ template project using Python (the BoilerplatePP
template). In that link you have the instructions on how to use it to create a starting project. The initial project should look something like this:
您需要像自动工具或 CMake 这样的构建系统,我推荐后者。有一个名为cookiecutter的 Python 实用程序,它允许您使用 Python(BoilerplatePP
模板)创建一个简单的 CMake/C++ 模板项目。在该链接中,您有关于如何使用它来创建起始项目的说明。初始项目应如下所示:
$ tree cpp/
cpp/
├── CMakeLists.txt
├── README.md
├── include
│?? └── Calculator.hpp
├── src
│?? ├── CMakeLists.txt
│?? └── Calculator.cpp
├── test
│?? ├── CMakeLists.txt
│?? └── CalculatorTests.cpp
└── thirdparity
└── catch
└── include
└── catch.hpp
CMake supports cross-compiling from version 2.6. Read this articleto get an insight on this issue. Good luck.
CMake 支持从 2.6 版本开始交叉编译。阅读本文以深入了解此问题。祝你好运。
回答by madduci
You can start using CMake and get your project ready for compilers in all the OSes.
您可以开始使用 CMake 并为所有操作系统中的编译器准备好您的项目。
In some special case, you should adapt your code including preprocessors checks on which OS you are using. For example:
在某些特殊情况下,您应该调整您的代码,包括对您使用的操作系统进行预处理器检查。例如:
#ifdef WIN32
//do some stuff for Windows
#elif __APPLE__
//do some stuff for Apple
#elif __linux__
//do stuff for Linux
#endif
Here at this link, you can find the list of all predefined macros.
在此链接中,您可以找到所有预定义宏的列表。
To crosscompile everything using only your Win7 32bit, you can use GCC cross compiler. So far, GCC or CLANG are the only compilers available on Windows, Mac and Linux. Follow this wiki if you want to build your project for other targets rather than only Windows 32bit
要仅使用 Win7 32 位交叉编译所有内容,您可以使用 GCC 交叉编译器。到目前为止,GCC 或 CLANG 是 Windows、Mac 和 Linux 上唯一可用的编译器。如果您想为其他目标而不仅仅是 Windows 32 位构建项目,请遵循此 wiki
回答by Tanuki
I think that the short answer is yes but then we have details (devil always hides in details). Briefly we have two questions: source compatibility and toolchain compatibility.
我认为简短的回答是肯定的,但我们有细节(魔鬼总是隐藏在细节中)。简而言之,我们有两个问题:源代码兼容性和工具链兼容性。
Toolchain compatibility. Here we need to consider 3 parts: compiler, libraries and build tools. Luckily the build tool exists and even not one. It is famous make in all reincarnations, cmake, ninja, etc. I prefer cmake as the easiest but you can always pick up your weapon of choice. Compiler. GCC is good choice, but today we have CLang which is also good choice. I'd bet for CLang as more interesting solution which is cleaner and better supported on Windows. Another good choice is Intel C++/C but it costs. Another way is to use different compilers for different systems and this is also not bad choice. Boost do it and boost team is one of the best. You can dig into boost configs and get a lot of interesting macroses and headers. C++ standard library and other libraries. This is one of the most tricky things. C++ is somewhat standard but might have issues after update. Other libraries should be build with compiler you use for the particular system. So be prepared to distribute your software with library bundle. Another good option is to rely upon 3rd party cross-platform libraries like Qt or WxWidgets.
Source compatibility is tightly available with particular various OS subsystem implementation. For example, threads, shared memory, sockets, scatter-gather I/O, GUI. Usually it is not very complicate but takes time to write all that wrappers.
工具链兼容性。这里我们需要考虑 3 部分:编译器、库和构建工具。幸运的是,构建工具存在,甚至没有。它在所有转世、cmake、忍者等中都是著名的 make。我更喜欢 cmake 作为最简单的方法,但你总是可以拿起你选择的武器。编译器。GCC 是不错的选择,但今天我们有 CLang,这也是不错的选择。我敢打赌 CLang 是更有趣的解决方案,它在 Windows 上更干净,更好地支持。另一个不错的选择是 Intel C++/C,但它很昂贵。另一种方法是为不同的系统使用不同的编译器,这也是不错的选择。Boost 做到这一点,Boost 团队是最好的团队之一。您可以深入研究 boost 配置并获得许多有趣的宏和标题。C++ 标准库和其他库。这是最棘手的事情之一。C++ 有点标准,但更新后可能会出现问题。其他库应该使用您用于特定系统的编译器构建。所以准备好用库包分发你的软件。另一个不错的选择是依赖第三方跨平台库,如 Qt 或 WxWidgets。
源兼容性与特定的各种 OS 子系统实现紧密相关。例如,线程、共享内存、套接字、分散-聚集 I/O、GUI。通常它不是很复杂,但编写所有这些包装器需要时间。
回答by Tanuki
As answered before CMakeis the best cross-platform compilation toolchain. It generates Visual Studio solutions, Makefiles, Eclipse projects etc for all platforms you mentioned above
正如之前回答的那样,CMake是最好的跨平台编译工具链。它为您上面提到的所有平台生成 Visual Studio 解决方案、Makefile、Eclipse 项目等
You can also try SCons, it is a bit less known, but a bit simplier
您也可以尝试SCons,它不太为人所知,但有点简单
回答by Homer Simpson
Golang offers easy cross-compilation(specify $GOOS and $GOARCH and run "go build"); depending on your requirements (e.g. do you need hard RTOS), it may be the right tool for the job.
Golang 提供简单的交叉编译(指定 $GOOS 和 $GOARCH 并运行“go build”);根据您的要求(例如,您是否需要硬实时操作系统),它可能是适合该工作的工具。
回答by BullyWiiPlaza
Using Windows 10
, this is actually easy now with CLion
IDE and WSL
. You can use your WSL
installation to compile your code using a Linux compiler toolchain (usually GCC
) while working on a Windows
host. Then, you can of course also switch to a Windows
toolchain like MinGW
or use Visual Studio
with MSVC
to compile again to get your Windows
binary.
使用Windows 10
,现在使用CLion
IDE 和WSL
. 在主机上工作时,您可以使用WSL
Linux 编译器工具链(通常是GCC
)使用您的安装来编译代码Windows
。然后,您当然也可以切换到Windows
类似的工具链MinGW
或使用Visual Studio
withMSVC
再次编译以获取您的Windows
二进制文件。
At the end of the day, this gives you both binaries while it feels like you were only using Windows
the whole time. Some people say WSL
is one of the best things Microsoft
has done in recent years. Indeed, it is awesome for cross-platform C/C++
development.
归根结底,这会为您提供两个二进制文件,而感觉就像您一直在使用Windows
。有人说这WSL
是Microsoft
近年来所做的最好的事情之一。事实上,它对于跨平台C/C++
开发来说非常棒。
Please note that compiling for OS X
is not included in this, so you will still need to have a Mac
or a virtual machine running OS X
, unfortunately.
请注意,编译OS X
不包括在此中,因此不幸的是,您仍然需要Mac
运行 或 虚拟机OS X
。
回答by Pandrei
cmake is used as a meta-language as it abstracts all the toolchain and OS dependencies; sure it's useful but can be difficult to use the first time around...
cmake 被用作元语言,因为它抽象了所有工具链和操作系统依赖项;确定它很有用,但第一次使用可能很难......
What you really need is what cmake uses behind the scene anyway: 1. a compiler for each target OS 2. a well written makefile
无论如何,您真正需要的是 cmake 在幕后使用的内容: 1. 每个目标操作系统的编译器 2. 编写良好的 makefile
you can turn on flags to modify makefile behaviour or if you really want the stripped down version you just call the right compiler(for the desired OS) when building the code.
您可以打开标志来修改 makefile 行为,或者如果您真的想要精简版本,您只需在构建代码时调用正确的编译器(对于所需的操作系统)。
回答by clemej
Just pointing out that, technically speaking, if your question is that you want to compile the same code on windows 7but targeting those other OSs, then what you need is a cross compiler for all those different targets that will work on windows. To that end, I think your best bet is to use Cygwin or MinGW32, and build cross compilers for your various architectures from GCC source. GCC (and possibly clang) are the only compilers that are a) free, and b) able to support all your targets. Building a cross compiler is not easy, but should be feasible. Some references:
只是指出,从技术上讲,如果您的问题是您想在 windows 7 上编译相同的代码但针对那些其他操作系统,那么您需要的是一个交叉编译器,用于所有那些可以在 windows 上运行的不同目标。为此,我认为您最好的选择是使用 Cygwin 或 MinGW32,并从 GCC 源代码为您的各种架构构建交叉编译器。GCC(可能还有 clang)是唯一一个 a) 免费,b) 能够支持所有目标的编译器。构建交叉编译器并不容易,但应该是可行的。一些参考:
The answers that say "use CMake!" are giving you good advice, in that they're encouraging you to use a build system that can supporting building your source natively on all those systems, but if you really can only buildon windows 7, then using CMake won't do you any good.
答案是“使用 CMake!” 给你很好的建议,因为他们鼓励你使用一个构建系统,该系统可以支持在所有这些系统上本地构建你的源代码,但如果你真的只能在 Windows 7 上构建,那么使用 CMake 不会对你有任何帮助好的。