简体   繁体   English

C++跨平台编译

[英]C++ cross platform compiling

I'm using Windows 7 - 32bit operating 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视窗 32
  • Windows 64视窗 64
  • Linux 32 Linux 32
  • Linux 64 Linux 64
  • OSX 32 OSX 32
  • OSX 64 OSX 64

Is it possible or not?有可能吗?

Note: I need to compile C++ for all OSes with only Win7 32bit.注意:我需要为只有 Win7 32 位的所有操作系统编译 C++。

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.您需要的是每个操作系统的工具链和“make”工具。 CMake has powerful crosscompiling abilities. CMake 具有强大的交叉编译能力。 This is not a necessity, but it will save some money: Get virtualization software (eg VMWare Player is free) and run on a different OS.这不是必需的,但可以节省一些钱:获取虚拟化软件(例如 VMWare Player 是免费的)并在不同的操作系统上运行。

I would recommend clang (OSX), gcc (Linux), TDM gcc (Windows) as toolchains (MSVC is also nice, but is not free).我会推荐 clang (OSX)、gcc (Linux)、TDM gcc (Windows) 作为工具链(MSVC 也不错,但不是免费的)。 The difference between 32bit and 64bit should not be the problem. 32bit 和 64bit 的区别应该不是问题。 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.当您使用不同的编译器时,我建议您通过在每个编译器上打开最迂腐的编译器标志来坚持标准。

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.只需在网上搜索有关交叉编译、工具链和持续集成的进一步阅读。

You need a build system like the auto tools or CMake, and I recommend the latter.您需要像自动工具或 CMake 这样的构建系统,我推荐后者。 There is a Python utility called cookiecutter that allows you to create a simple CMake/C++ template project using Python (the BoilerplatePP template).有一个名为cookiecutter的 Python 实用程序,它允许您使用 Python( BoilerplatePP模板)创建一个简单的 CMake/C++ 模板项目。 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:初始项目应如下所示:

$ 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. CMake 支持从 2.6 版本开始交叉编译。 Read this article to get an insight on this issue.阅读本文以深入了解此问题。 Good luck.祝你好运。

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.要仅使用 Win7 32 位交叉编译所有内容,您可以使用 GCC 交叉编译器。 So far, GCC or CLANG are the only compilers available on Windows, Mac and Linux.到目前为止,GCC 或 CLANG 是 Windows、Mac 和 Linux 上唯一可用的编译器。 Follow this wiki if you want to build your project for other targets rather than only Windows 32bit如果您想为其他目标而不仅仅是 Windows 32 位构建项目,请遵循此 wiki

GCC海湾合作委员会

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.简而言之,我们有两个问题:源代码兼容性和工具链兼容性。

  1. Toolchain compatibility.工具链兼容性。 Here we need to consider 3 parts: compiler, libraries and build tools.这里我们需要考虑 3 部分:编译器、库和构建工具。 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.它在所有转世、cmake、忍者等中都是著名的 make。我更喜欢 cmake 作为最简单的方法,但你总是可以拿起你选择的武器。 Compiler.编译器。 GCC is good choice, but today we have CLang which is also good choice. GCC 是不错的选择,但今天我们有 CLang,这也是不错的选择。 I'd bet for CLang as more interesting solution which is cleaner and better supported on Windows.我敢打赌 CLang 是更有趣的解决方案,它在 Windows 上更干净,更好地支持。 Another good choice is Intel C++/C but it costs.另一个不错的选择是 Intel C++/C,但它很昂贵。 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. Boost 做到这一点,Boost 团队是最好的团队之一。 You can dig into boost configs and get a lot of interesting macroses and headers.您可以深入研究 boost 配置并获得许多有趣的宏和标题。 C++ standard library and other libraries. C++ 标准库和其他库。 This is one of the most tricky things.这是最棘手的事情之一。 C++ is somewhat standard but might have issues after update. C++ 有点标准,但更新后可能会出现问题。 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.另一个不错的选择是依赖第三方跨平台库,如 Qt 或 WxWidgets。

  2. Source compatibility is tightly available with particular various OS subsystem implementation.源兼容性与特定的各种 OS 子系统实现紧密相关。 For example, threads, shared memory, sockets, scatter-gather I/O, GUI.例如,线程、共享内存、套接字、分散-聚集 I/O、GUI。 Usually it is not very complicate but takes time to write all that wrappers.通常它不是很复杂,但编写所有这些包装器需要时间。

cmake is used as a meta-language as it abstracts all the toolchain and OS dependencies; cmake 被用作元语言,因为它抽象了所有工具链和操作系统依赖项; sure it's useful but can be difficult to use the first time around...确定它很有用,但第一次使用可能很难......

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 行为,或者如果您真的想要精简版本,您只需在构建代码时调用正确的编译器(对于所需的操作系统)。

As answered before CMake is the best cross-platform compilation toolchain.正如之前回答的那样, CMake是最好的跨平台编译工具链。 It generates Visual Studio solutions, Makefiles, Eclipse projects etc for all platforms you mentioned above它为您上面提到的所有平台生成 Visual Studio 解决方案、Makefile、Eclipse 项目等

You can also try SCons , it is a bit less known, but a bit simplier您也可以尝试SCons ,它不太为人所知,但有点简单

Golang offers easy cross-compilation (specify $GOOS and $GOARCH and run "go build"); Golang 提供简单的交叉编译(指定 $GOOS 和 $GOARCH 并运行“go build”); depending on your requirements (eg do you need hard RTOS), it may be the right tool for the job.根据您的要求(例如,您是否需要硬实时操作系统),它可能是适合该工作的工具。

Using Windows 10 , this is actually easy now with CLion IDE and WSL .使用Windows 10 ,现在使用CLion IDE 和WSL这实际上很容易。 You can use your WSL installation to compile your code using a Linux compiler toolchain (usually GCC ) while working on a Windows host.Windows主机上工作时,您可以使用WSL安装来使用 Linux 编译器工具链(通常是GCC )编译代码。 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.然后,您当然也可以切换到像MinGW这样的Windows工具链或使用Visual StudioMSVC再次编译以获取您的Windows二进制文件。

At the end of the day, this gives you both binaries while it feels like you were only using Windows the whole time.归根结底,这会为您提供两个二进制文件,而感觉就像您一直只使用Windows Some people say WSL is one of the best things Microsoft has done in recent years.有人说WSLMicrosoft近年来所做的最好的事情之一。 Indeed, it is awesome for cross-platform C/C++ development.事实上,它对于跨平台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的虚拟机。

Just pointing out that, technically speaking, if your question is that you want to compile the same code on windows 7 but targeting those other OSs , then what you need is a cross compiler for all those different targets that will work on windows.只是指出,从技术上讲,如果您的问题是您想在 windows 7 上编译相同的代码但针对那些其他操作系统,那么您需要的是一个交叉编译器,用于所有那些可以在 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.为此,我认为您最好的选择是使用 Cygwin 或 MinGW32,并从 GCC 源代码为您的各种架构构建交叉编译器。 GCC (and possibly clang) are the only compilers that are a) free, and b) able to support all your targets. GCC(可能还有 clang)是唯一一个 a) 免费,b) 能够支持所有目标的编译器。 Building a cross compiler is not easy, but should be feasible.构建交叉编译器并不容易,但应该是可行的。 Some references:一些参考:

The answers that say "use CMake!"答案是“使用 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 build on windows 7, then using CMake won't do you any good.给你很好的建议,因为他们鼓励你使用一个构建系统,该系统可以支持在所有这些系统上本地构建你的源代码,但如果你真的只能在 Windows 7 上构建,那么使用 CMake 不会对你有任何帮助好的。

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

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