CMake与通常的项目结构

huangapple go评论56阅读模式
英文:

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将确保utilPUBLIC包含路径可以被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.

huangapple
  • 本文由 发表于 2023年2月23日 20:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定