【发布时间】:2019-06-27 02:11:19
【问题描述】:
我正在学习 CMake,但我有点挣扎。我的“项目”正在使用作为一个 .cpp 文件和两个 .h 文件提供的 JsonCpp“库”。结构如下:
myProject
build/
json/
CMakeLists.txt
jsoncpp.cpp
include/
json.h
json-forward.h
CMakeLists.txt
main.cpp
构建/CMakeLists.txt:
cmake_minimum_required(VERSION 3.6.0)
project(myProject)
add_subdirectory(json)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE json)
# add_executable(app main.cpp json/jsoncpp.cpp json/include/json.h json/include/json-forwards.h)
json/CMakeLists.txt:
cmake_minimum_required(VERSION 3.6.0)
add_library(
json
jsoncpp.cpp
include/json.h
include/json-forwards.h
)
target_include_directories(json PUBLIC '${CMAKE_CURRENT_SOURCE_DIR}/include')
仅对所有 .cpp 文件使用 add_executable() 与使用将 jsoncpp 转换为静态库然后链接它的 target_link_libraries 有什么区别?我应该选择什么方法?
接下来让我感到困惑的是 target_include_directories()。使用此功能有什么好处?如果我评论它,并运行 cmake(然后 makefile 并启动应用程序)一切仍然正常。如果我从 add_library() 中删除 "include/json.h" "include/json-forward.h",一切仍然有效。
【问题讨论】: