xxxxxxxxxx
- project/
- include/
- src/
- CMakeLists.txt
cmake_minimum_required(VERSION 3.20) # 指定CMake版本号
project(
myproject # 工程名称 必填
LANGUAGE CXX C # 语言 可多选 非必填
DESCRIPTION "my project" # 描述 非必填
VERSION 0.1.0 # 版本 非必填
)
# 生成程序名称 源文件
add_executable(main src/main.cpp)
# 头文件包含目录
target_include_directories(main PRIVATE include)
xxxxxxxxxx
cmake -S . -B cmake-build
-S
source识别CMake源文件
-B
生成目录
xxxxxxxxxx
cmake -S . -B cmake-build -G "Ninja"
x
cmake --build ./cmake-build/
--build
指定输出文件夹
xxxxxxxxxx
set(EXPORT_COMPILE_COMMANDS ON) # 启用LSP支持
target_compile_features(main PRIVATE cxx_std_17) # 指定用C++17版本编译
xxxxxxxxxx
- test/
- test.cpp
- CMakeLists.txt
x
# ./test/CMakeLists.txt
add_executable(cgmath ./cgmath.cpp)
add_test(NAME cgmath COMMAND $<TARGET_FILE:cgmath>)
xxxxxxxxxx
cmake_minimum_required(VERSION 3.20) # 指定CMake版本号
project(myproject)
add_library(engine STATIC src/main.cpp core/...) # 编译静态库
target_include_directories(main PUBLIC include)
target_compile_features(main PRIVATE cxx_std_17) # 指定用C++17版本编译
target_link_libraries(cgmath RPIVATE engine)
include(CTest)
enable_testing()
add_subdirectory(test)
添加3rdlibs文件夹并创建CMake文件
x
- 3rdlibs/
- glfw/
- CMakeLists.txt
xxxxxxxxxx
# ./3rdlibs/CMakeLists.txt
add_subdirectory(glfw) # 导入子目录
xxxxxxxxxx
#./CMakeLists.txt
...
add_subdirectory(3rdlibs)
target_link_library(engine PUBLIC glfw)
xxxxxxxxxx
- 3rdlibs/
- tomlplusplus/
- ...
- CMakeList.txt
- CMakeLists.txt
x
# ./3rdlibs/tomlplusplus/CMakeLists.txt
add_libray(toml++ INTERFACE)
target_include(toml++ INTERFACE ./)
target_compile_features(toml++ INTERFACE cxx_std_17)
xxxxxxxxxx
target_precompile_headers(cgmath REUSE_FROM engine)
xxxxxxxxxx
macro(AddTest target_name)
add_executable(${taget_name} )
add_test(NAME ${taget_name} PRIVATE $<TARGET_FILE:cgmath>)
target_link_libraries(${taget_name} PRIVATE engine)
target_precompile_headers(${taget_name} REUSE_FROM engine)
endmacro()
xxxxxxxxxx
option(ENGINE_BUILD_TEST "Should build unit test" OFF)
if (ENGINE_BUILD_TEST)
include(CTest)
enable_testing()
add_subdirectory(test)
endif()
xxxxxxxxxx
cmake --build ./cmake-build/ -ENGINE_BUILD_TEST=ON
xxxxxxxxxx
set(engine_name engine)
xxxxxxxxxx
${engine_name}
自动搜索目录中的源文件
xxxxxxxxxx
aux_source_directory(./src engine_src)
输出
xxxxxxxxxx
message("source file: ${enginge_src}")
xxxxxxxxxx
file(GLOB engine_header_files ./include/core/*.hpp)
xxxxxxxxxx
add_library(engine STATIC ${engine_src} ${engine_header_files})
xxxxxxxxxx
file(GLOB_RECURESE engine_header_file ./include/*.hpp)