我正在构建一个中等大小的 C++ 库并拼凑了我的 CMakeLists.txt
来自一堆不同示例等的文件。我试图理解 include_directories
之间的区别。与 target_link_libraries
相比说明。
我在下面列出了我的一些代码,但只是想在开头加上注释。我用 Boost
库来构建我的一些代码。所以我有一个指令 INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
在构建过程中包含 Boost 源目录。所以我假设 Cmake 将在构建任何可执行文件时包含这些 Boost 源文件——无需任何额外的显式指令。
但后来我有一个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} )
最佳答案
是的,两者都需要。
include_directories
将告诉编译器在哪里寻找头文件,在本例中是 boost 库的头文件。
target_link_libraries
将告诉链接器您要链接到可执行文件的库。
虽然 header 将(大部分时间)仅提供访问库的接口(interface),但库本身已预编译并链接到您的应用程序。
关于c++ - CMake: `include_directories` 与 `target_link_libraries` 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43456982/