我有一个C++项目,根目录下有src
、build
和tests
文件夹,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)
1条答案
按热度按时间h5qlskok1#
不管你把东西放在哪里,它都应该是可行的。
src/main_data_generation.cc
)的路径,因为add_executable
的相对路径是相对于current source directory的。RUNTIME_OUTPUT_DIRECTORY
target proeprty或theCMAKE_RUNTIME_OUTPUT_DIRECTORY
variable覆盖它。如果您的目标是共享或静态库而不是可执行文件,请参阅类似命名的变量和目标属性,如
ARCHIVE_OUTPUT_DIRECTORY
和LIBRARY_OUTPUT_DIRECTORY
。