Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cross platform compiling

Tags:

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.

like image 307
Chandra Nakka Avatar asked Aug 06 '14 05:08

Chandra Nakka


People also ask

What is cross-compiler in C?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.

Are C programs cross-platform?

The language C itself is cross-platform, because you don't directly run C code on machines. The C source code is compiled to assembly, and assembly is the platform specific code. The only non cross-platform part are the compilers and the resulting assembly.

Why is cross compiling so hard?

"building a cross-compiler is significantly harder than building a compiler that targets the platform it runs on." The problem exists due to the way libraries are built and accessed. In the normal situation all the libraries are located in a specific spot, and are used by all apps on that system.

What means cross compiling?

To cross-compile is to build on one platform a binary that will run on another platform. When speaking of cross-compilation, it is important to distinguish between the build platform on which the compilation is performed, and the host platform on which the resulting executable is expected to run.


2 Answers

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.

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.

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.

like image 183
Stefan Weiser Avatar answered Sep 24 '22 12:09

Stefan Weiser


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.

like image 38
aaragon Avatar answered Sep 25 '22 12:09

aaragon