Multiplatform C++ Project: Inclusion of platform specific sources

build, c++, cmake, multiplatform

Solution

This is fairly easy in CMake.

Just have your CMakeLists.txt file check the platform, and add in the appropriate files or include the appropriate subdirectory as needed.

The basic syntax is:

IF(WIN32)
    # Do windows specific includes
ELSEIF(UNIX)
    # Do linux specific includes
ENDIF(WIN32)

Problem

I have for some of my classes different implementations per OS. My source structure is like this: - include/AExample.h - include/windows/WindowsExample.h - include/linux/LinuxExample.h - src/AExample.cpp - src/windows/WindowsExample.cpp - src/linux/LinuxExample.cpp the A* classes are the interfaces for the specific implementations My current buildsystem is cmake - but at the moment its only capable of building the linux version. In a windows build i only need to include the windows/* file and on linux only the linux/* files I need to - include only the files that are relevant for my current build - choose the right implementation when i need an instance of AExample What techniques can you recommend to realize this in a professional way?

Original source