Reputation: 15548
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.
Is it possible or not?
Note: I need to compile C++ for all OSes with only Win7 32bit.
Upvotes: 32
Views: 53795
Reputation: 2292
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.
Upvotes: 43
Reputation: 19195
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.
Upvotes: 2
Reputation: 29
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.
Upvotes: 1
Reputation: 2563
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:
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.
Upvotes: 2
Reputation: 2883
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
Upvotes: 11
Reputation:
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
Upvotes: 1
Reputation: 439
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.
Upvotes: 3
Reputation: 4951
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.
Upvotes: 1
Reputation: 2400
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.
Upvotes: 12