I want to create a project in C++ that can work in Windows, Linux and Embedded Linux.
How are projects created when they have to work across many OS’es? Is it first created on one OS and then the code slowly modified to be ported to another OS? Eg: to me, the Linux version of Firefox appears to be created as a Windows project and a separate Linux project with a different code base, since Firefox behaves a bit different in Windows and Linux. Although the source code download is surprisingly a single link.
If QT is used for UI, Boost threads for threading, Build Bot for CI and NetBeans/Eclipse/QT Creator for an IDE, would a person be able to minimise the amount of code re-write required to get the project onto another OS? Is this the right way to do it, or are such projects meant to be created as two entirely separate projects for two separate OS’es?
1
The key is to isolate non-portable parts of the code from the main code base.
For example, the directory structure could include a directory called shared/
which would then include all the portable and shared code and content. Then whatever non-portable code would be located in specific directories which group the behaviour. For example GUI/
.
When it comes to actual code, you can do multiple implementations and use conditional compiling either via separate source files for separate platforms(e.g. GUI/Qt/Linux/<linux_specific_files>
) and then use some build system to define what kind of build you want to create(say a generic Linux build with Qt or GTK, granted that you have both implementations). Another way would be to use preprocessor directives to include/exclude parts of the code upon compilation. Simple example would be as follows:
#ifdef XX_LINUX_BUILD
// Linux specific code here
#endif
#ifdef XX_WINDOWS_BUILD
// Windows specific code here
#endif
…where XX
denotes some unique identifier for the project to avoid collisions. Then, upon compiling, you’d just define either XX_LINUX_BUILD
or XX_WINDOWS_BUILD
via your build script which sets the required compiler command line arguments(e.g. -DXX_LINUX_BUILD
with GCC/g++).
4