[CMake] How can I set different include_directories values for multiple C targets in a single CMakeLists.txt file?

Michael Hertling mhertling at online.de
Tue Jun 1 20:52:23 EDT 2010


On 06/01/2010 10:34 PM, Hickel, Kelly wrote:
> I'm trying to build two different versions of a library from a single CMakeLists.txt file (akin to what the FAQ section 7.1 spells out for building shared and static libraries).  However, the include_directories command doesn't accept a target the way that, for instance, target_link_libraries does (linking doesn't seem to be any problem at all).
> 
> I've tried using set_target_properties to set the COMPILE_FLAG property for each target to include the correct path, but that /I command doesn't appear in the project settings in Visual Studio (I'm starting on windows but will be branching out to other platforms eventually).
> 
> This is with CMake 2.8.1 on Windows 7 with VS 2005.
> 
> So, something like this, but I need to be able to build the v5 version with the v5 include path, not the v4 path.
> cmake_minimum_required (VERSION 2.8)
> project (test_with_ver_4)
> project (test_with_ver_5)
> add_library(test_with_ver_4 SHARED test.c )
> add_library(test_with_ver_5 SHARED test.c )
> include_directories ( "c:\\src\\pkg_v4\\include" )
> target_link_libraries(test_with_ver_4 "c:\\src\\pkg_v4\\pkg_v4.lib")
> target_link_libraries(test_with_ver_5 "c:\\src\\pkg_v5\\pkg_v5.lib")

IMO, the cleanest way is to build the v4 and v5 version each in a
subdirectory of its own and to use INCLUDE_DIRECTORIES() as usual:

CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(test_with_ver_4_and_ver_5)
add_subdirectory(test_4)
add_subdirectory(test_5)

test_4/CMakeLists.txt:
add_library(test_with_ver_4 SHARED ../test.c)
include_directories("c:\\src\\pkg_v4\\include")
target_link_libraries(test_with_ver_4 "c:\\src\\pkg_v4\\pkg_v4.lib")

test_5/CMakeLists.txt:
add_library(test_with_ver_5 SHARED ../test.c)
include_directories("c:\\src\\pkg_v5\\include")
target_link_libraries(test_with_ver_5 "c:\\src\\pkg_v5\\pkg_v5.lib")

Indeed, INCLUDE_DIRECTORIES() does not associate its arguments with
targets but with directories, and usually, the COMPILE_DEFINITIONS and
COMPILE_FLAGS properties aren't appropriate to pass include directories
to the preprocessor. Btw, one shouldn't have more than one PROJECT() in
a CMakeLists.txt as, among other things, this command defines variables
like <PROJECT>_SOURCE_DIR and <PROJECT>_BINARY_DIR.

Regards,

Michael


More information about the CMake mailing list