这是本文档旧的修订版!
CMake
- Official Site:cmake.org
- c++跨平台构建工具,开源[BSD]免费。
Tips
- MSVC中, cmake 默认生成的 release 工程不生成 pdb 文件,可如下添加
if (MSVC) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi") # if build .exe set(CMAKE_EXE_LINKER_FLAGS_RELEASE"${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF" CACHE STRING "" FORCE) # if build .dll set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF" CACHE STRING "" FORCE) # if build static .lib set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF" CACHE STRING "" FORCE) endif (MSVC)
参考 how to generate pdb files..., 或者使用 cmake generator expressions来设置:
target_compile_options(${YourProjName} PRIVATE "$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:/Zi>" ) target_link_options(${YourProjName} PRIVATE "$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:/DEBUG>" PRIVATE "$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:/OPT:REF>" PRIVATE "$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>:/OPT:ICF>" )
- 给dll右键属性的详细信息里添加git version
add_definitions(-DMAIN_PROJECT_NAME=\"${YourProjName}\") find_package(Git QUIET) if(GIT_FOUND) set(COMMIT_HASH "") set(BRANCH_NAME "") execute_process( COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H OUTPUT_VARIABLE COMMIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) execute_process( COMMAND ${GIT_EXECUTABLE} symbolic-ref --short -q HEAD OUTPUT_VARIABLE BRANCH_NAME OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) message(STATUS "Git version is ${BRANCH_NAME}:${COMMIT_HASH}") add_definitions(-DGIT_COMMIT_HASH=\"${COMMIT_HASH}\") add_definitions(-DGIT_BRANCH_NAME=\"${BRANCH_NAME}\") endif()
然后添加VersionInfo.rc
#if (defined GIT_COMMIT_HASH && defined GIT_BRANCH_NAME) #define PRODUCT_VERSION GIT_BRANCH_NAME ":" GIT_COMMIT_HASH #else #define PRODUCT_VERSION "1.0.2" #endif // GIT_COMMIT_HASH && GIT_BRANCH_NAME #ifndef MAIN_PROJECT_NAME #define MAIN_PROJECT_NAME "MyLib" #endif 1 VERSIONINFO FILEVERSION 1,0,0,1 PRODUCTVERSION 1,0,0,1 FILEFLAGSMASK 0x17L #ifdef _DEBUG FILEFLAGS 0x1L #else FILEFLAGS 0x0L #endif FILEOS 0x4L FILETYPE 0x0L FILESUBTYPE 0x0L BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904b0" BEGIN VALUE "FileDescription", MAIN_PROJECT_NAME " Binary" VALUE "FileVersion", "1.0.0.1" VALUE "InternalName", MAIN_PROJECT_NAME VALUE "LegalCopyright", "Copyright (C) 2020 " VALUE "OriginalFilename", MAIN_PROJECT_NAME ".dll" VALUE "ProductName", MAIN_PROJECT_NAME VALUE "ProductVersion", PRODUCT_VERSION END END BLOCK "VarFileInfo" BEGIN VALUE "Translation", 0x409, 1200 END END
- 循环子文件夹来包含子工程,参考 stackoverflow 7787823:
MACRO(SUBDIRLIST result curdir) FILE(GLOB children RELATIVE ${curdir} ${curdir}/*) SET(dirlist "") FOREACH(child ${children}) IF(IS_DIRECTORY ${curdir}/${child} AND EXISTS ${curdir}/${child}/CMakeLists.txt) LIST(APPEND dirlist ${child}) ENDIF() ENDFOREACH() SET(${result} ${dirlist}) ENDMACRO() SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR}) message(STATUS "valid subdirs: ${SUBDIRS}") FOREACH(subdir ${SUBDIRS}) ADD_SUBDIRECTORY(${subdir}) ENDFOREACH()