自用,备忘
一、集成vcpkg到cmake工程
VS新建cmake项目后,如果想要引入用vcpkg安装好的第三方库,可以打开项目里的CMakePresets.json,找到如下代码段
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
在里面添加上vcpkg的路径
"CMAKE_TOOLCHAIN_FILE": "D:/Vcpkg/scripts/buildsystems/vcpkg.cmake"
二、判断当前平台是windows还是linux
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
message ("Platform: Windows")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
message ("Platform: Linux")
endif()
三、判断Debug、Release
if (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
message ("Build type: Release")
else()
message ("Build type: Debug")
endif()
四、文件命令
创建文件夹
execute_process (COMMAND ${CMAKE_COMMAND} -E make_directory ${path})
拷贝文件夹
execute_process (COMMAND ${CMAKE_COMMAND} -E copy_directory ${path1} ${path2})
拷贝文件
execute_process( COMMAND ${CMAKE_COMMAND} -E copy ${path1}/xxx.cpp ${path2})
当前CMakeLists所在文件夹目录
${CMAKE_CURRENT_SOURCE_DIR}
设置编译文件输出目录(windows)
set (publish_debug ${CMAKE_CURRENT_SOURCE_DIR}/Publish/Debug)
set (publish_release ${CMAKE_CURRENT_SOURCE_DIR}/Publish/Release)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${publish_debug})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${publish_release})
获取编译文件输出目录(windows)
即 ${publish_debug} 或 ${publish_release}
设置编译文件输出目录(linux)
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
获取编译文件输出目录(linux)
${EXECUTABLE_OUTPUT_PATH}
五、常用库的查找,引入(动态链接)
boost
add_compile_definitions(BOOST_ALL_NO_LIB)
add_compile_definitions(BOOST_ALL_DYN_LINK)
find_package (Boost 1.82.0 REQUIRED COMPONENTS filesystem json)
include_directories (${Boost_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PRIVATE Boost::boost Boost::filesystem Boost::json)
fmt
find_package (fmt REQUIRED)
target_link_libraries (${PROJECT_NAME} PRIVATE fmt::fmt)
cgal
set (CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE ON)
find_package (CGAL REQUIRED COMPONENTS Core)
include_directories (${CGAL_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PRIVATE CGAL::CGAL CGAL::CGAL_Core)
freetype
find_package (Freetype REQUIRED)
include_directories (${FREETYPE_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PRIVATE ${FREETYPE_LIBRARIES})
opencv
find_package (OpenCV REQUIRED)
include_directories (${OpenCV_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PRIVATE ${OpenCV_LIBRARIES})
spdlog
find_package (spdlog REQUIRED)
target_link_libraries (${PROJECT_NAME} PRIVATE spdlog::spdlog_header_only)
poco
find_package (Poco REQUIRED Net Util Foundation)
target_link_libraries (${PROJECT_NAME} PRIVATE Poco::Net Poco::Util Poco::Foundation)
jsoncpp
find_package (jsoncpp REQUIRED)
target_link_libraries (${PROJECT_NAME} PRIVATE jsoncpp_lib)
六、其他
指定cmake最小版本
cmake_minimum_required(VERSION 3.2)
设置项目名称
project(demo)
生成一个可执行文件
add_executable(${PROJECT_NAME} demo.cpp demo.h)
生成一个库
add_library(${PROJECT_NAME} SHARED demo.cpp demo.h) 可选:STATIC/SHARED/MODULE
声明本地变量
set(LIBRARY_LIST Boost::boost CGAL::CGAL)
设置属性
set_property(TARGET TargetName
PROPERTY CXX_STANDARD 11)
set_target_properties(TargetName PROPERTIES
CXX_STANDARD 11)
包含子目录
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
七、设置cmake项目在VS中调试时传递的参数
https://learn.microsoft.com/zh-cn/cpp/build/configure-cmake-debugging-sessions?view=msvc-170
调试->调试和启动XXX的设置
会在.vs目录下生成launch.vs.json
在原有的配置上增加args字段,如下
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "CMakeDebugParam.exe (CMakeDebugParam\\CMakeDebugParam.exe)",
"name": "CMakeDebugParam.exe (CMakeDebugParam\\CMakeDebugParam.exe)",
"args": [ "Hello", "World" ]
}
]