我正在尝试使用 cmake 构建我的项目,但在指定带有要链接的库的目录时遇到问题。
我的项目具有以下目录层次结构:
文件夹/my_libs/libA/include
文件夹/my_libs/libA/src
文件夹/my_libs/libA/projects
文件夹/my_libs/libA/unit_tests
文件夹/other_libs/devlib/include
文件夹/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/lib3RD_PARTY_LIB
是环境变量,其中包含我使用的某些 3rd 方库的路径。
问题出在 unit_tests/CMakeLists.txt - 当我使用 include_directories 指定包含的相对路径时,它们被正确处理,但使用 link_directories 指定的相对路径处理错误。
这是我的makefile:
==================================
# 文件夹/my_libs/libA/CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project("libA")
add_subdirectory(src)
add_subdirectory(unit_tests)
==================================
# 文件夹/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} )
==================================
# 文件夹/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)
最佳答案
问题解决了。事实证明,通过 link_directories() 处理相对路径是由 CMP0015 策略定义的。
cmake_policy(SET CMP0015 NEW)
搞定了把戏。
看
http://www.cmake.org/cmake/help/v3.0/policy/CMP0015.html
关于cmake:相对链接目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25324357/