views:

270

answers:

2

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?

+3  A: 

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)
Reed Copsey
+1  A: 

If the headers have the same names but they reside in different hierarchies, you can just set the -I (include path) flag correctly for your compiler. That's much easier than handling all platform dependent includes separately.

If the headers reside in the same directory and/or you want to customize stuff, you would typically do this in your C/C++ code:

#ifdef _WIN32
  .. include Win headers ..
#endif

#ifdef LINUX
  .. include Linux headers ...
#endif

I wouldn't recommend the cmake specific solution unless you are sure you don't later need to switch the build system.

antti.huima
This doesn't handle including different source files in the build, though. Using CMake typically results in setting this appropriately, but then you can also include platform specific source files. That's one of (many good) reasons to use cmake in the first place.
Reed Copsey