3
votes

I'm trying to build my project with cmake and I'm having problem with specifying directories with libraries to link.

My project have the following directory hierarchy:

folder/my_libs/libA/include
folder/my_libs/libA/src
folder/my_libs/libA/projects
folder/my_libs/libA/unit_tests

folder/other_libs/devlib/include
folder/other_libs/devlib/lib

3RD_PARTY_LIB/gtest-1.5.0/include
3RD_PARTY_LIB/gtest-1.5.0/lib
3RD_PARTY_LIB/pcre-8.02/include
3RD_PARTY_LIB/pcre-8.02/lib

3RD_PARTY_LIB is environment variable that contains path of some 3rd party libs that I use.

The problem is in unit_tests/CMakeLists.txt - when I specify relative paths for includes with include_directories they are handled correctly, but relative paths specified with link_directories are handled wrong.

Here are my makefiles:

=================================
# folder/my_libs/libA/CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("libA")        
add_subdirectory(src)
add_subdirectory(unit_tests)

=================================
# folder/my_libs/libA/src/CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)    
project("libA")

set(HDR_LIST ../include/AuxRoutines.h          
             ...
             ../include/utility.h)

set(SRC_LIST ModuleImpl.cpp 
             ..
             Version.cpp)

# add include dirs
include_directories("../include")
include_directories("../../../other_libs/devlib/include")

# disable some warnings
add_compile_options("/wd 4996")
...

# this command creates a new library from specified sources
add_library(libA STATIC ${SRC_LIST} ${HDR_LIST} )

=================================
# folder/my_libs/libA/unit_tests/CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)    
project("sys_utils_tests")

set(SRC_LIST AuxiliaryRoutinesTests.cpp 
             ...
             UnitTests.h)

# add include dir
include_directories("../include")
include_directories("../../../other_libs/devlib/include")
include_directories($ENV{3RD_PARTY_LIB}/gtest-1.5.0/include)
include_directories($ENV{3RD_PARTY_LIB}/pcre-8.02/include)

# where to look for libs
link_directories(../../../other_libs/devlib/lib/)  
link_directories($ENV{3RD_PARTY_LIB}/gtest-1.5.0/lib/)  

# disable some warnings
add_compile_options("/wd 4996")
...

# make an executable from specified sources
add_executable(libATest ${SRC_LIST})

# libs to link with
target_link_libraries(libATest libA)
target_link_libraries(libATest gtestd.lib)
target_link_libraries(libATest devlibd.lib)
1

1 Answers

15
votes

Problem solved. It turned out that handling of relative paths by link_directories() is defined by CMP0015 policy.

cmake_policy(SET CMP0015 NEW)

Made the trick.

see http://www.cmake.org/cmake/help/v3.0/policy/CMP0015.html