title : CMake tags : c/c++, C#, python mathjax : true published : true toc : true —
1. 指定源文件
2. 指定库文件
3. 指定编译器
4. 指定编译器参数
设置Debug/Release
target_compile_options([target] PRIVATE "-Wdeprecated-copy")
cmake --build out/multi --config Debug
cmake --build out/multi --config Release
# 检查构建类型并设置编译器标志
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Building in Debug mode")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -Wextra")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
message(STATUS "Building in Release mode")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall -Wextra -DNDEBUG")
else()
message(WARNING "Unknown build type: ${CMAKE_BUILD_TYPE}")
endif()
5. 中间文件输出位置
CMake 只能通过命令行来指定中间文件的输出位置,避免污染源代码目录。
cmake [<options>] -S <path-to-source> -B <path-to-build>
$ cmake -S /path/to/source-dir -B /path/to/build-dir
$ cmake --build /path/to/build-dir --config Release
6. 最终文件输出位置
set_target_properties(demo5
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
LIBRARY_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
RUNTIME_OUTPUT_DIRECTORY "/home/xy/cmake_practice/bin/"
)