tags:

views:

60

answers:

1

I would like to write a cmake script that will compile the same source against multiple versions of header files. The aim is to be able to easily create shared libraries that are backwards compatible with earlier versions of an API.

I am looking for examples and pointers on the best way to do this.

I am new to both c++ and cmake so any help will be greatly appreciated.

+1  A: 

This answer is a bit off the top of my head, so take it with a grain of salt.

In your source code do this:

#ifdef Version_1_0
#include "Header_1_0.h"
#endif

#ifdef Version_2_0
#include "Header_2_0.h"
#endif

In the CMakeLists.txt file do this:

add_library ( Foo_Version_1_0 SHARED Foo.cxx Header_1_0.h )
# When you compile Foo_Version_1_0, define "Version_1_0"
set_target_properties ( Foo_Version_1_0 PROPERTIES COMPILE_FLAGS -DVersion1_0 )

# Likewise for Version_2_0
...

When you are finished building, you should have two libraries called libFoo_Version_1_0.so and libFoo_Version_2_0.so.

Cheers,
-dan

Daniel Blezek