我应该在哪个CMakeLists.txt文件中定义一个目标,该目标的源代码位于具有其自己的CMakeLists.txt文件的子目录中?

chhkpiq4  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(149)

我有一个C++项目,根目录下有srcbuildtests文件夹,src文件夹看起来像这样(简化)

├── CMakeLists.txt
├── data_generation
│   ├── CMakeLists.txt
│   ├── include
│   │   └── data_generation
│   │       └── main_data_generation.h
│   └── main_data_generation.cc
└── settings
    ├── CMakeLists.txt
    ├── constants.cc
    └── include
        └── settings
            └── constants.h

build$ cmake --build .运行无误。
main_data_generation.cc包含一个main函数,我想让它成为可执行的,这样我就可以用命令运行它。

./build/data_geneneration/main_data_generation

我猜我应该在CMakeLists.txt中放入add_executable(data_generation src/main_data_generation.cc),但是放在哪一个中呢?

  • 源代码/CMakeLists.txt*
add_subdirectory(data_generation)
add_subdirectory(settings)
# more subdirecories...
  • 源代码/数据生成/CMakeLists.txt*
file(GLOB data_generation_SRC
     "*.cc"
)

add_library(data_generation ${data_generation_SRC})

target_include_directories(data_generation PUBLIC include)

根目录中的CMakeLists.txt文件

cmake_minimum_required(VERSION 3.16)
project(MultiplyTest LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

enable_testing()

add_subdirectory(src)
add_subdirectory(tests)
h5qlskok

h5qlskok1#

不管你把东西放在哪里,它都应该是可行的。

如果您的目标是共享或静态库而不是可执行文件,请参阅类似命名的变量和目标属性,如ARCHIVE_OUTPUT_DIRECTORYLIBRARY_OUTPUT_DIRECTORY

相关问题