英文:
CMake with usual project structure
问题
The error is: "CMake Error at src/CMakeLists.txt:2 (target_include_directories):Cannot specify include directories for target "hello" which is not built by this project."
The project structure is:
-build
-src
-CMakeLists.txt
-include
-util
-util.h
-util
-CMakeLists.txt
-util.cpp
main.cpp
CMakeLists.txt
I want to build the target hello in build, and here's my code (incomplete):
#project/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(hello VERSION 1.0)
add_subdirectory(src)
#project/src/CMakeLists.txt
add_subdirectory(util)
target_include_directories(hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(hello main.cpp)
target_link_libraries(hello PUBLIC util)
#project/src/util/CMakeLists.txt
add_library(util STATIC util.cpp)
I'm new in CMake and I wonder what should I write in these 3 CMakeLists.txt. I will be thankful if anyone can tell me!
英文:
The error is:"CMake Error at src/CMakeLists.txt:2 (target_include_directories):Cannot specify include directories for target "hello" which is not built by this project."
The project structure is:
-build
-src
-CMakeLists.txt
-include
-util
-util.h
-util
-CMakeLists.txt
-util.cpp
main.cpp
CMakeLists.txt
I want to build the target hello in build, and here's my code(incomplete):
#project/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(hello VERSION 1.0)
add_subdirectory(src)
#project/src/CMakeLists.txt
add_subdirectory(util)
target_include_directories(hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(hello main.cpp)
target_link_libraries(hello PUBLIC util)
#project/src/util/CMakeLists.txt
add_library(util STATIC util.cpp)
I'm new in CMake and I wonder what should I write in these 3 CMakeLists.txt. I will be thankful if anyone can tell me!
答案1
得分: 0
以下是翻译好的部分:
这些注释中描述的错误是由于在创建目标hello
之前访问它导致的。add_*
系列的命令创建目标,而target_*
系列的命令修改现有目标。首先创建,然后再执行操作。
在这种情况下,我们可以进行更多的清理。文件project/src/CMakeLists.txt
应该如下所示:
add_subdirectory(util)
add_executable(hello main.cpp)
target_link_libraries(hello PUBLIC util)
以及project/src/util/CMakeLists.txt
应该如下所示:
add_library(util STATIC util.cpp)
target_include_directories(util PUBLIC ../include)
这样,您将创建一个名为util
的库,其包含路径为"include"。在处理target_link_libraries
时,CMake将确保util
的PUBLIC
包含路径可以被hello
访问。
设置util
的(而不是hello
的)包含路径的好处在于,链接util
的任何目标都将获得这些包含文件。否则,使用util
的每个目标都必须自行设置包含路径。
英文:
The error described in the comments results from target hello
being accessed before it was created. The add_*
family of commands creates targets while the target_*
family of commands modifies existing targets. Create first, then do something with it.
In this case, we can clean up some more. The file project/src/CMakeLists.txt
should look like this:
add_subdirectory(util)
add_executable(hello main.cpp)
target_link_libraries(hello PUBLIC util)
And project/src/util/CMakeLists.txt
like this:
add_library(util STATIC util.cpp)
target_include_directories(util PUBLIC ../include)
This way, you will create a library util
with its include path as "include". When processing target_link_libraries
, CMake will make sure util
's PUBLIC
include path will be accessible by hello
.
The benefit of setting util
's (and not hello
's) include path is that any target linking util
will get the includes. Otherwise, each and every target using util
would have to set the include path itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论