Struktur des Projekts
.
CMakeLists.txt
external //We simulate that code is provided by an "external" library outside of src
CMakeLists.txt
conversion.cpp
conversion.hpp
README.md
src
CMakeLists.txt
evolution //propagates the system in a time step
CMakeLists.txt
evolution.cpp
evolution.hpp
initial //produces the initial state
CMakeLists.txt
initial.cpp
initial.hpp
io //contains a function to print a row
CMakeLists.txt
io.cpp
io.hpp
main.cpp //the main function
parser //parses the command-line input
CMakeLists.txt
parser.cpp
parser.hpp
tests //contains two unit tests using the Catch2 library
catch.hpp
CMakeLists.txt
test.cpp
Wie man es macht
1. Die oberste CMakeLists.txt ist sehr ähnlich zu Rezept 1, Code-Wiederverwendung mit Funktionen und Makros
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-07 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
# defines targets and sources
add_subdirectory(src)
# contains an "external" library we will link to
add_subdirectory(external)
# enable testing and define tests
enable_testing()
add_subdirectory(tests)
2. die Ziele und Quellen sind in src/CMakeLists.txt definiert (außer dem Konvertierungsziel)
add_executable(automata main.cpp)
add_subdirectory(evolution)
add_subdirectory(initial)
add_subdirectory(io)
add_subdirectory(parser)
target_link_libraries(automata
PRIVATE
conversion
evolution
initial
io
parser
)
Die Konvertierungsbibliothek ist in external/CMakeLists.txt definiert.
add_library(conversion "")
target_sources(conversion
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/conversion.cpp
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/conversion.hpp
)
target_include_directories(conversion
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
Die Datei src/CMakeLists.txt fügt weitere Unterverzeichnisse hinzu, die ihrerseits CMakeLists.txt-Dateien enthalten. Sie sind alle ähnlich aufgebaut; src/evolution/CMakeLists.txt enthält die folgenden:
add_library(evolution "")
target_sources(evolution
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/evolution.cpp
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/evolution.hpp
)
target_include_directories(evolution
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
Die Einheitstests werden in tests/CMakeLists.txt registriert.
add_executable(cpp_test test.cpp)
target_link_libraries(cpp_test evolution)
add_test(
NAME
test_evolution
COMMAND
$<TARGET_FILE:cpp_test>
)
Wie man es ausführt
$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
Siehe dazu: https://github.com/sun1211/cmake_with_add_subdirectory