【发布时间】:2020-06-21 13:27:58
【问题描述】:
如何在cmake中制作一个项目,将所有c++文件收集到一个头文件中?
我有这个项目结构。
/
project/
folder1/
file.cpp
file.hpp
folder2/
...etc
CMakeLists.txt
tests/
test.cpp
CMakeLists.txt
CMakeList.txt
根 cmakelists.txt
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject"
LANGUAGES C CXX)
set(CMAKE_EXECUTABLE_SUFFIX ".exe")
include(GNUInstallDirs)
add_subdirectory ("project")
option(ENABLE_TESTING OFF)
if (ENABLE_TESTING)
enable_testing()
add_subdirectory("tests")
endif()
项目中的CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
file(GLOB projectSRC
"*/*.cpp"
"*/*.hpp"
"*.cpp"
"*.hpp"
)
add_library(project INTERFACE)
message(STATUS "CMake inatall directory: " ${CMAKE_INSTALL_INCLUDEDIR})
target_include_directories(project
INTERFACE
$<BUILD_INTERFACE:${PROJECT_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
并测试 cmakelist.txt
cmake_minimum_required (VERSION 3.8)
# install Catch2 testing library
# (https://github.com/catchorg/Catch2/blob/master/docs/cmake-integration.md#installing-catch2-from-git-repository or use packet manager)
find_package(Catch2 REQUIRED)
file(GLOB testSRC
"*.cpp"
)
add_executable(tests ${testSRC})
target_link_libraries(tests
Catch2::Catch2
project)
include(CTest)
include(Catch)
catch_discover_tests(tests)
如何生成一个标头并使用它(在测试或其他项目中)或使该库能够具有模板?第一个更好。
【问题讨论】:
-
如果你想把所有东西都放在头文件里,你为什么要把东西放在 .cpp 文件中??
-
这可能是不可能的。您可以定义单个 cpp 文件的本地变量,如果您已经这样做了,那么如果多个 cpp 文件定义相同的变量,您可能会重新定义某些东西。
-
如果很重要,我可以将所有内容移到标题中,但不能移到一个中。
-
我从未亲自尝试过这样做。但是,您可以在 github 上查看执行此操作的开源项目。你可能会从他们的项目中得到很好的指导。我知道 2,有 pybind11 和 catch2 是仅标头库。
-
可以在这里找到逐行解释的良好参考:dominikberner.ch/cmake-interface-lib
标签: c++ cmake shared-libraries