C++ cross platform compiling

2019-03-09 11:14发布

问题:

I'm using Windows 7 - 32bit operating system.

I want to compile my simple C++ source code into executable for all OSes and architectures.

I want to compile for this below operating systems from my OS.

  • Windows 32
  • Windows 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.

回答1:

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. Not a necessity, but will save some money: Get a virtualization software (e.g. VMWare Player is free) and run different OS.

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 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 would 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.



回答2:

You need a build system like the auto tools or CMake, and I recommend the latter. There is a Python utility called cookiecutter that 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:

$ 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 article to get an insight on this issue. Good luck.



回答3:

You can start using CMake and get your project ready for compilers in all the OSes.

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

GCC



回答4:

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. 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.

  2. 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.



回答5:

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...

What you really need is what cmake uses behind the scene anyway: 1. a compiler for each target OS 2. a well written 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.



回答6:

As answered before CMake is the best cross-platform compilation toolchain. It generates Visual Studio solutions, Makefiles, Eclipse projects etc for all platforms you mentioned above

You can also try SCons, it is a bit less known, but a bit simplier



回答7:

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.



回答8:

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. 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:

  • http://wiki.osdev.org/GCC_Cross-Compiler
  • http://www.mingw.org/wiki/HostedCrossCompilerHOWTO

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 build on windows 7, then using CMake won't do you any good.