CMake:“ include_directories”和“ target_link_libraries”之间有什么区别 [英] CMake: What is the difference between `include_directories` versus `target_link_libraries`

查看:577
本文介绍了CMake:“ include_directories”和“ target_link_libraries”之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个中等大小的C ++库,并从许多不同的示例中将我的 CMakeLists.txt 文件拼凑在一起。我试图理解两者之间的区别 include_directories target_link_libraries 指令进行比较。

I am building a moderately sized C++ library and have cobbled together my CMakeLists.txt file from a bunch of different examples, etc. I was trying to understand the difference between include_directories versus the target_link_libraries instructions.

我列出了一些下面的代码,但只想在前面加上注释。我使用 Boost 库构建一些代码。因此,我有一条指令 INCLUDE_DIRECTORIES($ {Boost_INCLUDE_DIRS})将Boost源目录包括在构建过程中。因此,我假定Cmake在构建任何可执行文件时将包含这些Boost Source文件-无需任何其他显式指令。

I list some of my code below, but just wanted to preface with a comment. I use the Boost library to build some of my code. So I have an instruction to INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) to include the Boost source directories in the build process. So I assume that Cmake will include these Boost Source files when building any executable--without any additional explicit instruction.

但是后来我有了 TARGET_LINK_LIBRARIES (gd_validator $ {Boost_LIBRARIES})。因此,这表明我不仅需要包含Boost目录,还需要显式地将其与可执行文件链接。

But later I have a TARGET_LINK_LIBRARIES( gd_validator ${Boost_LIBRARIES} ) when building an executable. So that suggests that I need to not only include the Boost directory, but then also explicitly link it with the executable.

所以我不确定我是否真的需要两个步骤,还是只需要 INCLUDE_DIRECTORIES 指令,是吗

So I was not sure if I actually needed both steps, or if I just needed the INCLUDE_DIRECTORIES instruction and that was it.

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} )


推荐答案

是的,您需要同时使用。

Yes, you need both.

include_directories 会告诉编译器在哪里寻找头文件,在这种情况下, Boost库。

include_directories will tell to the compiler where to look for the header files, in this case, the header files for the boost library.

target_link_libraries 会告诉链接器您要针对可执行文件链接的库。

target_link_libraries will tell to the linker which libraries you want to link against your executable.

尽管头文件(大部分时间)仅提供访问库的接口,但库本身已预先编译并链接到您的应用程序。

While headers will provide (most of the time) just the interface to access the library, the library itself is precompiled and linked to your application.

这篇关于CMake:“ include_directories”和“ target_link_libraries”之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆