gpt4 book ai didi

c++ - CMake 在 build 和 src 目录之间拆分生成的文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:36 25 4
gpt4 key购买 nike

我的问题是,当我运行 $ cmake/path/to/source/ 时,生成的文件和目录在我调用的目录和/path/to/source/include 之间拆分。

这是我的 CMake 项目:

文件结构:

root:
|-CMakeLists.txt
|-src
| |-CMakeLists.txt
| |-"source files"
|-include # This is where part of the generated files are ending up.
| |-CMakeLists.txt
| |-"include files"

这是我的 CMakeLists.txt:

root/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(DUCKSIM)

# Add the root directory for the CMakeLists.txt being called. This is necessary for
# out-of-tree builds.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add the directories containing source and header files.
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/include)

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY $(pwd)) # This is an attempt to fix my problem

src/CMakeLists.txt:

# set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set DUCKSIM_SOURCES as all .cpp files
file(GLOB DUCKSIM_SOURCES *.cpp)

# Set the name of the executable as "ducksim" and link it with main.cpp
# and every thing in the DUCKSIM_SOURCES variable.
add_executable(ducksim main.cpp ${DUCKSIM_SOURCES})

包含/CMakeLists.txt:

# set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Set DUCKSIM_SOURCES as all .h files
file(GLOB DUCKSIM_SOURCES *.h)

最佳答案

命令add_subdirectory() 将选择性地接受第二个路径作为参数,但这将指示二进制 目录,如documentation 中所示.通过将 include 文件夹作为第二个参数,CMake 假定您希望将二进制文件放在那里。这就是为什么您最终会在顶层得到一些文件(CMakeCache.txt 等),而在 include 文件夹中得到一些文件。

根据记录,使用 file(GLOB ...)not recommended用于收集要编译的源文件。如果添加源文件,则不会更改任何 CMake 文件,构建系统也不会重新生成。

最后,您不需要 file(GLOB ...) 作为头文件,但您可能需要 include_directories() 调用 包括文件夹。

关于c++ - CMake 在 build 和 src 目录之间拆分生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com