cmaklist流程控制——调试及发布
cmaklist流程控制
目前只会配置-编译调试-打包发布,并且不会workflow控制
后续学习配置-编译调试-测试-打包发布,workflow控制,理解整个流程,目前对流程控制理解也不够。
1.CMake Presets
先于Cmakelist文件,指导项目配置。
参考CMake之Presets编写
**CMAKE_BUILD_TYPE:**指定了编译类型,debug还是release
**CMAKE_INSTALL_PREFIX:**指定了cmakelist中install的前缀路径,类似于cmake -CMAKE_INSTALL_PREFIX
CMakePresets.json
{
"version": 8,
"configurePresets": [
{
"name": "radar-debug",
"displayName": "radar-debug",
"description": "radar-debug",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/out/debug/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/debug/install"
}
},
{
"name": "radar-release",
"displayName": "radar-release",
"description": "radar-release",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/out/release/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/release/install"
}
}
]
}
2.Cmakelist
通过Presets
配置的CMAKE_BUILD_TYPE
控制编译,配合add_definitions(-DDEBUG_PRINTF)
控制程序表现
cmake_minimum_required(VERSION 3.5.0)
project(radarSerial VERSION 0.1.0 LANGUAGES C CXX)
# Set the default build type to Release if not specified
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
# Configure Debug and Release flags
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") # Debug信息,关闭优化
set(CMAKE_CXX_FLAGS_RELEASE "-O3") # 高级优化
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDEBUG_PRINTF)
add_definitions(-DDEBUG_ENV)
endif()
find_package(MY_LIBRARY_PATH NAMES CppLinuxSerial PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake NO_DEFAULT_PATH REQUIRED)
set(LIB_mqtt3c_PATH /usr/local/lib/)
message(STATUS "CppLinuxSerial_DIR = ${CppLinuxSerial_DIR}")
add_executable(radarSerial main.cpp)
target_include_directories(
radarSerial
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/include/CppLinuxSerial"
)
target_sources(radarSerial
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/comm.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/mqtt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/cJSON.c)
target_link_libraries(radarSerial CppLinuxSerial::CppLinuxSerial pthread paho-mqtt3c)
install(TARGETS radarSerial RUNTIME DESTINATION bin)
install(FILES
${LIB_mqtt3c_PATH}/libpaho-mqtt3c.so
${LIB_mqtt3c_PATH}/libpaho-mqtt3c.so.1
${LIB_mqtt3c_PATH}/libpaho-mqtt3c.so.1.3
${CMAKE_CURRENT_SOURCE_DIR}/lib/libCppLinuxSerial.so
DESTINATION lib)