CMake常用命令
一、常用命令
- 用于在生成VS工程时对文件进行分组(类似于VS中的过滤筛选器)
set(SRC_FILE
../shared/renderer/client_app_renderer.cc
../shared/renderer/client_app_renderer.h
)
source_group(shared\\\\renderer FILES SRC_FILE)
- 添加宏定义
add_definitions("-DGTK_DISABLE_DEPRECATED")
- 添加工程项目依赖的第三方库,例如QCustomPlot
project( QCustomPlot )
add_library( ${PROJECT_NAME} STATIC )
target_sources( ${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/qcustomplot.cpp
${CMAKE_CURRENT_LIST_DIR}/qcustomplot.h
)
target_include_directories( ${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries( ${PROJECT_NAME}
Qt6::Gui
)
- 配置基于Qt的GUI可执行程序
cmake_minimum_required(VERSION 3.17)
set(target_name SteelSurfaceMeasure)
if(MSVC)
set(VERSIONINFO_RC "${CMAKE_BINARY_DIR}/versionInfo.rc")
configure_file("versionInfo.rc" ${VERSIONINFO_RC})
endif()
find_package(VTK REQUIRED)
find_package(Qt6AdvancedDocking REQUIRED)
if (NOT VTK_FOUND)
message("VTK NOT FOUND: ${VTK_NOT_FOUND_MESSAGE}")
return()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
# translations
set(TS_FILES translations/rail_measure_zh_CN.ts)
#set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION "l10n")
qt6_add_translation(QM_FILES ${TS_FILES})
qt6_create_translation(QM_FILES ${PROJECT_SOURCE_DIR} ${TS_FILES})
file(GLOB_RECURSE PROJECT_SOURCES *.h *.hpp *.cpp *.ui *.qrc)
qt_add_executable(${target_name} MANUAL_FINALIZATION ${PROJECT_SOURCES} ${TS_FILES} ${VERSIONINFO_RC})
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
else()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
# vtk_module_autoinit is needed
vtk_module_autoinit(TARGETS ${target_name} MODULES ${VTK_LIBRARIES})
endif()
target_include_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src/Extern
${CMAKE_SOURCE_DIR}/thirdparty
)
target_link_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/lib
${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}
#${CMAKE_SOURCE_DIR}/lib/QtDock/${CMAKE_BUILD_TYPE}
)
target_link_libraries(${target_name}
PRIVATE
Qt6::Core
Qt6::Widgets
Qt6::PrintSupport
Qt6::OpenGL
Qt6::Concurrent
Qt6::AxContainer
ads::qt6advanceddocking
OpenGL32
QCustomPlot
${VTK_LIBRARIES}
)
#add_definitions(-DQCUSTOMPLOT_USE_OPENGL)
set_target_properties(${target_name} PROPERTIES
LINK_FLAGS "/INCLUDE:__tcmalloc /MANIFEST:NO"
MACOSX_BUNDLE_GUI_IDENTIFIER glasssix.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
)
if(${USE_SIMULATION_DATA} OR (CMAKE_BUILD_TYPE STREQUAL "Debug"))
set_target_properties(${target_name} PROPERTIES WIN32_EXECUTABLE FALSE)
else()
set_target_properties(${target_name} PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
if( WIN32 )
DeployQt(
TARGET
${target_name}
DEPLOY_PATH
${STEELMEASURE_DEST_FOLDER}
)
else()
DeployQt(
TARGET
${target_name}
DEPLOY_PATH
${target_name}
)
endif()
if (NOT APPLE)
install_ext( TARGETS ${target_name} ${STEELMEASURE_DEST_FOLDER} "" )
endif()
qt_finalize_executable(${target_name})
add_subdirectory( PlotCharts )
add_subdirectory( Visualization3D )
- 配置工程的生成目标为动态库
cmake_minimum_required(VERSION 3.17)
find_package(Qt6
COMPONENTS
Core
REQUIRED
)
set(target_name MeasureDetect)
file(GLOB PROJECT_SOURCES *.h *.hpp *.cpp)
add_library(${target_name} SHARED ${PROJECT_SOURCES})
target_include_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/include/FusionMeasure
${CMAKE_SOURCE_DIR}/include/RailCP/Measure
${CMAKE_SOURCE_DIR}/include/RailCP/Calibration
${CMAKE_SOURCE_DIR}/thirdparty
${CMAKE_SOURCE_DIR}/src/Gui
)
target_link_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/lib/RailCP/Measure/${CMAKE_BUILD_TYPE}
${CMAKE_SOURCE_DIR}/lib/RailCP/Calibration/${CMAKE_BUILD_TYPE}
)
target_link_libraries(${target_name}
PRIVATE
RMDataStorage
RMDataCompress
logger
RailCP
GXMiscellaneous::gx-miscellaneous
)
target_compile_definitions(${target_name} PRIVATE GX_IS_MEASURE_DETECT_IMPL=1)
InstallSharedLibrary( TARGET ${target_name} )
- 顶层CMake文件的配置
cmake_minimum_required(VERSION 3.17)
project(SteelSurfaceMeasure VERSION 0.9.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
message("GX_MISCELLANEOUS_ROOT:${GX_MISCELLANEOUS_ROOT}")
find_package(GXMiscellaneous REQUIRED NO_DEFAULT_PATH HINTS ${GX_MISCELLANEOUS_ROOT})
# find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck)
if (CMAKE_CXX_CPPCHECK)
list(APPEND CMAKE_CXX_CPPCHECK
"--enable=all"
"--force"
#"--inline-suppr"
"--suppressions-list=${CMAKE_SOURCE_DIR}/CppCheckSuppressions.txt"
)
endif()
message("CMAKE_CXX_CPPCHECK:${CMAKE_CXX_CPPCHECK}")
# One shouldn't generate the BUILD project directly in the SOURCES folder!
if ( ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} )
if ( NOT SAME_BUILD_AND_SOURCE_FOLDER_WARNING_ALREADY_ISSUED )
message(FATAL_ERROR "It is not advised to BUILD the binaries directly in the SOURCE folder!\n If you want to proceed with this option, just CONFIGURE the project once again" )
set( SAME_BUILD_AND_SOURCE_FOLDER_WARNING_ALREADY_ISSUED TRUE )
endif()
endif()
# Add our cmake module path so we don't need relative paths for these
list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" )
include( CMakePolicies )
include( CMakeSetCompilerOptions )
# Load advanced scripts
include( CMakeInclude )
include( Install )
if( WIN32 )
include( DeployQt )
include( CMakeExternalLibs )
endif()
# 获取 Git Commit ID
execute_process(COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_REV_VAL
OUTPUT_STRIP_TRAILING_WHITESPACE)
if ( NOT "${GIT_REV_VAL}" STREQUAL "")
add_definitions(-DGIT_REV=\"${GIT_REV_VAL}\")
endif()
# option
option( OPTION_BUILD_CAMERA2DSERVER "Check to compile Camera2DServer project" OFF )
option( OPTION_BUILD_TESTING "Check to compile testing project" OFF )
option( OPTION_BUILD_TOOLS "Check to compile tools project" OFF )
option( USE_SIMULATION_DATA "Simulate in-line measurements using simulated data" OFF )
message("OPTION_BUILD_CAMERA2DSERVER:${OPTION_BUILD_CAMERA2DSERVER}")
message("OPTION_BUILD_TESTING:${OPTION_BUILD_TESTING}")
message("OPTION_BUILD_TOOLS:${OPTION_BUILD_TOOLS}")
message("USE_SIMULATION_DATA:${USE_SIMULATION_DATA}")
# Define target folders
# (now that ccViewer can have its own plugins, qCC and ccViewer must fall in separate folders!
if(WIN32 OR APPLE)
set( STEELMEASURE_DEST_FOLDER SteelSurfaceMeasure )
set( TESTING_DEST_FOLDER testing )
set( TOOLS_DEST_FOLDER tools )
else()
set( STEELMEASURE_DEST_FOLDER bin )
set( TESTING_DEST_FOLDER bin )
set( TOOLS_DEST_FOLDER bin )
endif()
if( WIN32 )
set( INSTALL_DESTINATIONS ${STEELMEASURE_DEST_FOLDER} )
if( ${OPTION_BUILD_CAMERA2DSERVER} )
list( APPEND INSTALL_DESTINATIONS ${CAMERA2DSERVER_DEST_FOLDER} )
endif()
if( ${OPTION_BUILD_TESTING} )
list( APPEND INSTALL_DESTINATIONS ${TESTING_DEST_FOLDER} )
endif()
if( ${OPTION_BUILD_TOOLS})
list( APPEND INSTALL_DESTINATIONS ${TOOLS_DEST_FOLDER} )
endif()
elseif( UNIX AND NOT APPLE )
# RPATH Linux/Unix: (dynamic) libs are put in $prefix/$lib/SteelSurfaceMeasure,
include( GNUInstallDirs )
set( LINUX_INSTALL_SHARED_DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/SteelSurfaceMeasure" )
set( CMAKE_INSTALL_RPATH ${LINUX_INSTALL_SHARED_DESTINATION} )
set( INSTALL_DESTINATIONS ${CMAKE_INSTALL_PREFIX})
endif()
# 设置多核编译
if (WIN32 AND MSVC)
OPTION(USE_MP "use multiple" ON)
OPTION(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP "Set The Global Option COMPILE_FLAGS /MP to target." ON)
if(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP OR USE_MP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()
set(VS_STARTUP_PROJECT ${PROJECT_NAME})
endif()
if(MSVC)
add_compile_options("/utf-8")
endif()
if( NOT ((CMAKE_BUILD_TYPE STREQUAL "Debug") OR
(CMAKE_BUILD_TYPE STREQUAL "Release") OR
(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") OR
(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) )
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "CMAKE_VERSION: ${CMAKE_VERSION}")
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMAKE_CXX_COMPILER_VERSION: ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
if ( WIN32 )
add_subdirectory(src/logger)
add_subdirectory(src/alarm)
add_subdirectory(src/RMDataCache)
add_subdirectory(src/RMDataCompress)
add_subdirectory(src/RMDataStorage)
add_subdirectory(src/FusionMeasure)
if(${USE_SIMULATION_DATA})
add_subdirectory(src/PLCController-Simu)
add_subdirectory(src/RMDataCollection-Simu)
else()
add_subdirectory(src/PLCController)
add_subdirectory(src/RMDataCollection)
endif()
if(${OPTION_BUILD_TESTING})
add_subdirectory(testing)
endif()
elseif( UNIX AND NOT APPLE )
add_subdirectory(src/RMDataCompress)
add_subdirectory(src/RMDataStorage)
endif()
- 使用CMake配置单元测试工程
cmake_minimum_required(VERSION 3.17)
file(GLOB test_srcs *.h *.hpp *.cpp)
set(target_name ${PROJECT_NAME}_test)
add_executable(${target_name} ${test_srcs})
target_include_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/thirdparty
${CMAKE_SOURCE_DIR}/thirdparty/asio
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/PLCController
)
target_link_directories(${target_name}
PRIVATE
${CMAKE_SOURCE_DIR}/lib
)
target_link_libraries(${target_name}
PRIVATE
logger
RMDataCompress
RMDataCache
)
if (NOT APPLE)
install_ext( TARGETS ${target_name} ${TESTING_DEST_FOLDER} "" )
endif()
enable_testing()
add_test(NAME ${target_name} COMMAND ${target_name})