Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplatform C++ Project: Inclusion of platform specific sources

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?

like image 634
Christoph Avatar asked Jul 09 '09 18:07

Christoph


1 Answers

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)
like image 170
Reed Copsey Avatar answered Nov 15 '22 02:11

Reed Copsey