【发布时间】:2017-04-17 18:11:10
【问题描述】:
我正在构建一个中等大小的 C++ 库,并从一堆不同的示例等中拼凑出我的 CMakeLists.txt 文件。我试图了解 include_directories 与 target_link_libraries 指令之间的区别。
我在下面列出了我的一些代码,但只是想在前面加上评论。我使用Boost 库来构建我的一些代码。所以我有一个指示INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) 在构建过程中包含 Boost 源目录。所以我假设 Cmake 将在构建任何可执行文件时包含这些 Boost Source 文件——无需任何额外的显式指令。
但后来我在构建可执行文件时有一个TARGET_LINK_LIBRARIES( gd_validator ${Boost_LIBRARIES} )。所以这表明我不仅需要包含 Boost 目录,还需要明确地将其与可执行文件链接。
所以我不确定我是否真的需要这两个步骤,或者我是否只需要 INCLUDE_DIRECTORIES 指令,仅此而已。
cmake_minimum_required(VERSION 3.7)
project(XXX)
find_package(Boost 1.58.0 REQUIRED COMPONENTS system filesystem program_options chrono timer date_time REQUIRED)
if(NOT Boost_FOUND)
message(FATAL_ERROR "NOTICE: This demo requires Boost and will not be compiled.")
endif()
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
file(GLOB lib_SRC RELATIVE "lib/" "*.h" "*.cpp")
file(GLOB test_SRC RELATIVE "tests/" "*.h" "*.cpp")
# need to fix the instruction below to reference library
set(SOURCE_FILES ${lib_SRC} tests/testComplexCreator.cpp tests/testDataFormatter.cpp tests/testComplexAnalysis.cpp tests/testFascadeClass.cpp)
add_library(libXXX SHARED ${SOURCE_FILES})
add_executable(${PROJECT_NAME} main.cpp random_mat_vector_generator.h random_mat_vector_generator.cpp)
add_executable(gd_validator gudhi_validator.cpp)
TARGET_LINK_LIBRARIES( gd_validator ${Boost_LIBRARIES} )
【问题讨论】: