C++ cross platform compiling
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.
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.
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
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.
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.
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.