【问题标题】:CMake splits the generated files between build and src directoryCMake 在 build 和 src 目录之间拆分生成的文件
【发布时间】:2016-02-02 20:31:39
【问题描述】:

我的问题是当我运行$ 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 的:

根目录/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() 命令应该每个目录使用一个。它的几个参数并不意味着将此命令应用于多个源目录。
  • 谢谢 Tsyvarev,为我修好了。

标签: c++ build cmake


【解决方案1】:

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

郑重声明,使用file(GLOB ...)就是not recommended收集源文件进行编译。如果添加源文件,则不会更改任何 CMake 文件,并且不会重新生成构建系统。

最后,头文件不需要file(GLOB ...),但include 文件夹可能需要include_directories() 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多