用法
cmake -B build // 配置,在源码目录用 -B 直接创建 build 目录并生成 build/Makefile
cmake --build build -j4 // 构建,Linux会调用make,Windows会调用devenv.exe
cmake --version
定义
-DCMAKE_INSTALL_PREFIX=/opt/openvdb-8.0 //设置安装路径
-DCMAKE_BUILD_TYPE=Release // 设置发布模式
-DBUILD_SHARED_LIBS:BOOL=ON
变量
PROJECT_SOURCE_DIR:当前项目源码路径(存放main.cpp的地方)
PROJECT_BINARY_DIR:当前项目输出路径(存放main.exe的地方)
CMAKE_SOURCE_DIR:根项目源码路径(存放main.cpp的地方)
CMAKE_BINARY_DIR:根项目输出路径(存放main.exe的地方)
PROJECT_IS_TOP_LEVEL:BOOL类型,表示当前项目是否是(最顶层的)根项目
PROJECT_NAME:当前项目名
CMAKE_PROJECT_NAME:根项目的项目名
PROJECT_VERSION_MAJOR // x
PROJECT_VERSION_MINOR // y
PROJECT_VERSION_PATCH // z
${${PROJECT_NAME}_VERSION}
BUILD_SHARED_LIBS
WIN32, APPLE, Linux, UNIX, ANDROID, IOS // 简写变量,NOT非
CMAKE_CXX_COMPILER_ID
CMAKE_GENERATOR // Ninja
CMAKE_CXX_COMPILER
CMAKE_C_COMPILER
$ENV{PATH} //环境变量
脚本用法
project(项目名 LANGUAGES 使用的语言列表(C/C++ CUDA ASM) VERSION x.y.z)
project(项目名 VERSION x.y.z)
cmake_minimum_required(VERSION 3.15)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) // 允许动态库链接
set_property(TARGET otherlib CMAKE_POSITION_INDEPENDENT_CODE ON) // 允许指定动态库链接
set_property(TARGET main PROPERTY WIN32_EXECUTABLE ON) // win32不启用控制台窗口
set_property(TARGET main PROPERTY LIBRARY_OUT_DIRECTORY ./) // 动态链接库输出路径
set_property(TARGET main PROPERTY ARCHIVE_OUT_DIRECTORY ./) // 静态链接库输出路径
set_property(TARGET main PROPERTY RUNTIME_OUT_DIRECTORY ./) // 可执行文件输出路径
set_properties(main PROPERTYS
CXX_STANDARD 17
LINK_WHAT_YOU_USE ON
)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} C:/cmake)
target_compile_definitions(main PUBLIC MY_MACRO=233 $<$<PLATFORM_ID:Windows,Linux>:MY_NAME=”Bill Gates”>) // $<$<类型:值>:为真时的表达式>
if(MYVAR MATCHER "HELLO") else() endif() //if 的特点:不需要加 ${},会自动尝试作为变量名求值
set(MYVAR ON PARENT_SCOPE) // 子模块变量传递到父模块
if(DEFINED ENV{xx}) // 判断环境变量是否存在
set(ENV{x} ...) // 设置环境变量
自定义FindXXX.cmake
set_target_properties(TBB::tbb PROPERTIES
INTERFACE_COMPILE_DEFINITIONS ""
INTERFACE_INCLUDE_DIRECTORIES "/include"
)
案例exe
cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_BUILD_TYPE Release)
project(zeno LANGUAGES C CXX VERSION 0.2.3)
if (PROJECT_BINARY_DIR STREQUAL PROJECT_SOURCE_DIR)
message(WARNING "The binary directory of CMake cannot be the same as source directory!")
endif()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
if (WIN32)
add_definitions(-DNOMINMAX -D_USE_MATH_DEFINES)
endif()
if (NOT MSVC)
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
message(STATUS "Found CCache: ${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE_PROGRAM})
endif()
endif()
message("PROJECT_NAME: ${PROJECT_NAME}")
message("PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message("PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}")
message("CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
message("CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
add_subdirectory(mylib)
find_package(TBB)
if (TBB_FOUND)
message(STATUS "TBB found at: ${TBB_DIR}")
target_link_libraries(main PUBLIC TBB::tbb)
target_compile_definitions(main PUBLIC WITH_TBB)
else()
message(WARNING "TBB not found! using serial for")
endif()
add_executable(main)
file(GLOB sources CONFIGURE_DEPENDS *.cpp *.h)
aux_source_directory(. sources)
aux_source_directory(mylib sources)
target_sources(main PUBLIC ${sources})
target_link_libraries(main PUBLIC mylib)
target_compile_definitions(main PUBLIC MY_MACRO=233)
target_compile_definitions(main PUBLIC
$<$<PLATFORM_ID:Windows>:MY_NAME="Bill Gates">
$<$<PLATFORM_ID:Linux>:MY_NAME="Linus Torvalds">
$<$<PLATFORM_ID:Darwin>:MY_NAME="Steve Jobs">
)
target_compile_definitions(main PUBLIC
$<$<CXX_COMPILER_ID:GNU,Clang>:MY_NAME="Open-source">
$<$<CXX_COMPILER_ID:MSVC,NVIDIA>:MY_NAME="Commercial">
)
target_compile_definitions(main PUBLIC
$<$<AND:$<CXX_COMPILER_ID:GNU,Clang>,$<PLATFORM_ID:Linux,FreeBSD>>:MY_NAME="Open-source">
)
案例lib
project(mylib)
if (NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
endif()
add_library(mylib STATIC mylib.cpp)
set_property(TARGET otherlib PROPERTY POSITION_INDEPENDENT_CODE ON)
message("mylib got PROJECT_NAME: ${PROJECT_NAME}")
message("mylib got CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message("mylib got CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message("mylib got PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message("mylib got PROJECT_BINARY_DIR: ${PROJECT_BINARY_DIR}")
message("mylib got CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
message("mylib got CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}")
project — CMake 3.30.0-rc3 Documentation
CMake常见变量——Project和CMake相关信息_cmake 变量后缀-CSDN博客
cmake-generator-expressions(7) — CMake 3.30.0-rc3 Documentation
CMake: use of true/false or on/off or yes/no in generator expressions - Stack Overflow
if — CMake 3.7.2 Documentation
set — CMake 3.16.9 Documentation
Cmake入门之——Set方法(六)_cmake set-CSDN博客
Ccache — Compiler cache