xxxxxxxxxx- project/ - include/ - src/ - CMakeLists.txtcmake_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)xxxxxxxxxxcmake -S . -B cmake-build-Ssource识别CMake源文件
-B生成目录
xxxxxxxxxxcmake -S . -B cmake-build -G "Ninja"x
cmake --build ./cmake-build/--build指定输出文件夹
xxxxxxxxxxset(EXPORT_COMPILE_COMMANDS ON) # 启用LSP支持target_compile_features(main PRIVATE cxx_std_17) # 指定用C++17版本编译xxxxxxxxxx- test/ - test.cpp - CMakeLists.txtx
# ./test/CMakeLists.txtadd_executable(cgmath ./cgmath.cpp)add_test(NAME cgmath COMMAND $<TARGET_FILE:cgmath>)xxxxxxxxxxcmake_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.txtxxxxxxxxxx# ./3rdlibs/CMakeLists.txtadd_subdirectory(glfw) # 导入子目录xxxxxxxxxx#./CMakeLists.txt...add_subdirectory(3rdlibs)
target_link_library(engine PUBLIC glfw)xxxxxxxxxx- 3rdlibs/ - tomlplusplus/ - ... - CMakeList.txt - CMakeLists.txtx
# ./3rdlibs/tomlplusplus/CMakeLists.txtadd_libray(toml++ INTERFACE)target_include(toml++ INTERFACE ./)target_compile_features(toml++ INTERFACE cxx_std_17)
xxxxxxxxxxtarget_precompile_headers(cgmath REUSE_FROM engine)
xxxxxxxxxxmacro(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()
xxxxxxxxxxoption(ENGINE_BUILD_TEST "Should build unit test" OFF)
if (ENGINE_BUILD_TEST) include(CTest) enable_testing() add_subdirectory(test)endif()xxxxxxxxxxcmake --build ./cmake-build/ -ENGINE_BUILD_TEST=ON
xxxxxxxxxxset(engine_name engine)xxxxxxxxxx${engine_name}
自动搜索目录中的源文件
xxxxxxxxxxaux_source_directory(./src engine_src)输出
xxxxxxxxxxmessage("source file: ${enginge_src}")xxxxxxxxxxfile(GLOB engine_header_files ./include/core/*.hpp)xxxxxxxxxxadd_library(engine STATIC ${engine_src} ${engine_header_files})
xxxxxxxxxxfile(GLOB_RECURESE engine_header_file ./include/*.hpp)