cmake:相对链接目录

时间:2014-08-15 09:43:37

标签: cmake

我正在尝试使用cmake构建我的项目,并且我在指定带有要链接的库的目录时遇到问题。

我的项目具有以下目录层次结构:

文件夹/ my_libs / LIBA /包括
文件夹/ my_libs / LIBA / SRC
文件夹/ my_libs /力霸/项目
folder / my_libs / libA / unit_tests

文件夹/ other_libs / devlib /包括
folder / other_libs / devlib / lib

3RD_PARTY_LIB / GTEST-1.5.0 /包括
3RD_PARTY_LIB / GTEST-1.5.0 / lib中
3RD_PARTY_LIB / PCRE-8.02 /包括
3RD_PARTY_LIB / pcre-8.02 / lib

3RD_PARTY_LIB是环境变量,包含我使用的某些第三方库的路径。

问题在于unit_tests / CMakeLists.txt - 当我为 include_directories 指定包含的相对路径时,它们被正确处理,但使用 link_directories 指定的相对路径处理错误。

这是我的makefile:

=================================
#folite / 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 个答案:

答案 0 :(得分:13)

问题解决了。事实证明,link_directories()处理相对路径是由CMP0015策略定义的。

cmake_policy(SET CMP0015 NEW)

诀窍。

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