{"id":1994,"date":"2022-12-20T22:00:56","date_gmt":"2022-12-20T14:00:56","guid":{"rendered":"https:\/\/flandre-scarlet.moe\/blog\/?p=1994"},"modified":"2022-12-20T23:46:41","modified_gmt":"2022-12-20T15:46:41","slug":"%e4%bd%bf%e7%94%a8-cmake-%e7%94%9f%e6%88%90%e9%a1%b9%e7%9b%ae%e7%9a%84%e5%b7%a5%e7%a8%8b%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/flandre-scarlet.moe\/blog\/1994\/","title":{"rendered":"\u4f7f\u7528 Cmake \u751f\u6210\u9879\u76ee\u7684\u5de5\u7a0b\u6587\u4ef6"},"content":{"rendered":"<p>\u6784\u5efa\u7cfb\u7edf\u63cf\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528\u6784\u5efa\u5de5\u5177\u4ece\u9879\u76ee\u7684\u6e90\u4ee3\u7801\u6784\u5efa\u9879\u76ee\u7684\u53ef\u6267\u884c\u6587\u4ef6\u548c\u5e93\uff0c\u4ece\u800c\u81ea\u52a8\u6267\u884c\u8be5\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u6784\u5efa\u7cfb\u7edf\u53ef\u4ee5\u662f\u7528\u4e8e\u547d\u4ee4\u884c make \u5de5\u5177\u7684 Makefile \u6216\u7528\u4e8e\u96c6\u6210\u5f00\u53d1\u73af\u5883\u7684\u9879\u76ee\u6587\u4ef6\u3002\u4e3a\u4e86\u907f\u514d\u7ef4\u62a4\u591a\u4e2a\u8fd9\u6837\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u9879\u76ee\u53ef\u4ee5\u4f7f\u7528\u4ee5 CMake \u8bed\u8a00\u7f16\u5199\u7684\u6587\u4ef6\u62bd\u8c61\u5730\u6307\u5b9a\u5176\u6784\u5efa\u7cfb\u7edf\u3002\u4ece\u8fd9\u4e9b\u6587\u4ef6\u4e2d\uff0cCMake \u901a\u8fc7\u79f0\u4e3a\u751f\u6210\u5668\u7684\u540e\u7aef\u4e3a\u6bcf\u4e2a\u4f7f\u7528\u8005\u5728\u672c\u5730\u751f\u6210\u4e00\u4e2a\u9996\u9009\u7684\u6784\u5efa\u7cfb\u7edf\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728 c++ \u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u4f7f\u7528\u4e2d\uff0c\u5305\u62ec\u589e\u51cf\u6587\u4ef6\u5728\u5185\uff0c\u4efb\u4f55\u5176\u4ed6\u6d89\u53ca\u9879\u76ee\u5de5\u7a0b\u7684\u6539\u52a8\u90fd\u8981\u5404\u4e2a\u5e73\u53f0\u7684\u5f00\u53d1\u540c\u6b65\u5904\u7406\u5404\u81ea\u5e73\u53f0\u7684\u5de5\u7a0b\u3002\u800c\u4e00\u65e6\u67d0\u4e2a\u5e73\u53f0\u6ca1\u6709\u53ca\u65f6\u66f4\u65b0\u9879\u76ee\u6587\u4ef6\u5c31\u53ef\u80fd\u4f1a\u5bfc\u81f4 CI\/CD \u81ea\u52a8\u5316\u6d41\u7a0b\u5931\u8d25\uff0c\u975e\u5e38\u5f71\u54cd\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>\u4e3a\u907f\u514d\u8d39\u65f6\u8d39\u529b\u5730\u7ef4\u62a4\u591a\u4e2a\u8fd9\u6837\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u53ef\u4ee5\u9009\u62e9\u7528 CMake \u5bf9\u5176\u8fdb\u884c\u62bd\u8c61\u3002\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a CMakeLists.txt\uff0c\u5b83\u5c31\u53ef\u4ee5\u4e3a\u4f60\u751f\u6210\u6307\u5b9a\u7684 vcxproj \u6587\u4ef6\u6216\u8005 makefile \u6587\u4ef6\u3002\u7136\u540e\u8c03\u7528 MSBuild \u6216\u8005 make \u8fdb\u884c\u7f16\u8bd1\u5c31\u53ef\u4ee5\u3002<br \/>\n<!--more--><\/p>\n<p>\u53e6\u5916\u6211\u4eec\u53ef\u4ee5\u628a\u5e73\u53f0\u7279\u5b9a\u7684\u53c2\u6570\u653e\u5230\u5355\u72ec\u7684\u6a21\u5757\u91cc\uff0c\u7136\u540e include \u5230 CMakeLists.txt \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u6bd4\u8f83\u6e05\u6670\u7684\u6587\u4ef6\u7ed3\u6784\u3002<\/p>\n<h2>\u7f16\u5199\u751f\u6210 vcxproj \u5de5\u7a0b\u7684 cmake \u811a\u672c<\/h2>\n<p>\u811a\u672c\u4e2d\u5e38\u5e38\u4f1a\u5939\u6742\u7740\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u53d8\u91cf\u548c CMake \u7684\u4fdd\u7559\u53d8\u91cf\uff0cCMake \u7684\u4fdd\u7559\u53d8\u91cf\u4e00\u822c\u662f\uff1a<\/p>\n<ol>\n<li>\u4ee5 CMAKE_ \u5f00\u5934\uff0c\u65e0\u8bba\u5927\u5199\u5c0f\u5199\u6216\u662f\u6df7\u5408<\/li>\n<li>\u4ee5 _CMAKE_ \u5f00\u5934\uff0c\u540c\u4e0a<\/li>\n<li>\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u540e\u9762\u8ddf\u968f\u4efb\u4f55 cmake \u547d\u4ee4<\/li>\n<\/ol>\n<h3>CMakeLists.txt<\/h3>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n# \u8bbe\u7f6e cmake \u7684\u6700\u4f4e\u7248\u672c\r\ncmake_minimum_required(VERSION 3.25.0)\r\n\r\n# \uff08\u53ef\u9009\uff09\u8bbe\u7f6e windows SDK \u7248\u672c\uff0c\u8981\u5728 project \u547d\u4ee4\u524d\u8bbe\u7f6e\u5b8c\u6bd5\r\nif (WIN32)\r\n    set(CMAKE_SYSTEM_VERSION 10.0.18362.0 CACHE STRING &quot;&quot; FORCE)\r\nendif()\r\n\r\n# \u8bbe\u7f6e\u5de5\u7a0b\u7684\u540d\u79f0\u4e3a TestProject\uff0c\u6539\u4e3a\u771f\u5b9e\u7684\u5de5\u7a0b\u540d\r\nproject(TestProject)\r\n\r\n# \u63a5\u4e0b\u6765\u8bbe\u7f6e\u6574\u4e2a\u9879\u76ee\u7684\u6839\u76ee\u5f55\uff0c\u540e\u7eed\u5de5\u7a0b\u5185\u4f7f\u7528\u6240\u6709\u6587\u4ef6\u8def\u5f84\u5c06\u4ee5\u8be5\u8def\u5f84\u4e3a\u57fa\u51c6, \r\n# \u540e\u7eed\u7684\u8def\u5f84\u5c31\u53ef\u4ee5\u7528 WORKSPACE_PATH \u6307\u5b9a\uff0c\u4ee3\u66ff\u6666\u6da9\u7684\u76f8\u5bf9\u8def\u5f84\r\n# PROJECT_SOURCE_DIR \u662f cmake \u5185\u7f6e\u7684\u53d8\u91cf\uff0c\r\n# \u5728\u8c03\u7528 project() \u547d\u4ee4\u540e\u88ab\u521d\u59cb\u5316\u4e3a\u5f53\u524d cmake \u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c\r\n# \u901a\u5e38\u4f1a\u7528 -S \u53c2\u6570\u4f20\u7ed9 cmake.exe\r\nset(WORKSPACE_PATH &quot;${PROJECT_SOURCE_DIR}\/..\/..&quot;)\r\n\r\n# \u8bbe\u7f6e\u8be5\u5de5\u7a0b\u4f7f\u7528\u7684\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff0c\u6b64\u5904\u6709 public \u548c private \u4e24\u4e2a\u8def\u5f84\uff0c\r\n# \u8fd9\u91cc\u6d89\u53ca\u5230\u4f5c\u7528\u57df\u7684\u6982\u5ff5\uff0cpublic \u5c5e\u6027\u53ef\u4ee5\u88ab\u4f7f\u7528\u8fd9\u4e2a\u7684\u5bf9\u8c61\u7ee7\u627f\u3002\r\n# \u53ef\u4ee5\u7406\u89e3\u4e3a\u5982\u679c\u67d0\u4e2a\u5c5e\u6027\uff08\u5934\u6587\u4ef6\u3001\u9884\u5b9a\u4e49\u5b8f\u3001\u5bfc\u5165\u5e93\u7b49\uff09\u9700\u8981\u88ab\u5e93\u7684\u4f7f\u7528\u8005\u7ee7\u627f\u5c31\u8bbe\u4e3a public\uff0c\u53cd\u4e4b\u7528 private\u3002\r\n# \u56e0\u4e3a\u6837\u4f8b\u4e2d\u4ecd\u7136\u4fdd\u6301\u4e86 lib\/so \u7ea7\u522b\u7684\u4f9d\u8d56\u800c\u6ca1\u6709\u505a cmake \u811a\u672c\u7ea7\u522b\u7684\u4f9d\u8d56\uff0c\r\n# \u6240\u4ee5\u5728\u8fd9\u91cc\u4e24\u4e2a\u5c5e\u6027\u7684\u610f\u4e49\u4e0d\u662f\u5f88\u5927\u3002\r\n# \u540e\u9762 .cmake \u6587\u4ef6\u4e2d\u53ef\u4ee5\u770b\u5230\u628a\u4e24\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u7ed9 cmake \u751f\u6210\u73af\u5883\u3002\r\n# \u591a\u4e2a\u8def\u5f84\u65f6\u53ef\u4ee5\u6309\u6bcf\u4e2a\u4e00\u884c\u8f93\u5165\uff0c\u5f15\u53f7\u53ef\u4ee5\u7701\u7565\u3002\r\n# PS\uff1a\u867d\u7136\u662f private\uff0c\u4f46\u8fd9\u91cc\u7684\u8def\u5f84\u4ecd\u7136\u662f\u5168\u5e73\u53f0\u901a\u7528\uff0c\u7279\u5b9a\u5e73\u53f0\u7684\u5305\u542b\u8def\u5f84\u4f1a\u5199\u5230\u4e0b\u9762\u7684 .cmake \u6587\u4ef6\u91cc\r\nset(TARGET_COMMON_PUBLIC_INC_PATH &quot;${WORKSPACE_PATH}\/include\/definitions&quot;)\r\nset(TARGET_COMMON_PRIVATE_INC_PATH\r\n${WORKSPACE_PATH}\/src\/internal_1\/include\r\n${WORKSPACE_PATH}\/src\/internal_2\/include\r\n${WORKSPACE_PATH}\/src\/utils\/include\r\n)\r\n\r\n# \u6dfb\u52a0\u9879\u76ee\u4f7f\u7528\u7684 cpp\/h \u6587\u4ef6\uff0c\u8fd9\u91cc\u653e\u6240\u6709\u5e73\u53f0\u90fd\u4f7f\u7528\u7684\u516c\u5171\u6587\u4ef6\uff0c\r\n# \u7279\u5b9a\u5e73\u53f0\u7684\u6587\u4ef6\u6211\u4eec\u53ef\u4ee5\u653e\u5230\u540e\u9762\u7684 .cmake \u5e73\u53f0\u76f8\u5173\u6587\u4ef6\u4e2d\u3002\r\n# \u5934\u6587\u4ef6\u4e5f\u5e94\u8be5\u5217\u8fdb\u6765\u3002\r\nset(TARGET_COMM_SRC_LIST\r\n${WORKSPACE_PATH}\/include\/definitions\/header_1.h\r\n${WORKSPACE_PATH}\/src\/internal_1\/xxx.cpp\r\n${WORKSPACE_PATH}\/src\/internal_1\/xxx.h\r\n...\r\n)\r\n\r\n# \u4f7f\u7528 include \u547d\u4ee4\u5305\u542b\u5e73\u53f0\u76f8\u5173\u6587\u4ef6\r\nif (WIN32)\r\n  include(module CMakeLists.win.cmake)\r\nelseif (&quot;${CMAKE_SYSTEM_NAME}&quot; STREQUAL &quot;Linux&quot;)\r\n    include(CMakeLists.linux.cmake)     \r\nendif()\r\n<\/pre>\n<h3>CMakeLists.win.cmake<\/h3>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n# \u540c\u6837\u8bbe\u7f6e\u6700\u4f4e\u7248\u672c\r\ncmake_minimum_required(VERSION 3.25.0)\r\n\r\n# \u5b9a\u4e49\u751f\u6210\u7684\u6587\u4ef6\u540d\uff0c\u8fd9\u91cc\u53ea\u662f\u5b9a\u4e49\u53d8\u91cf\uff0c\u540e\u9762\u8fd8\u8981\u7528 add_library \u8bbe\u7f6e\r\nset(TARGET_NAME TestProject)\r\n\r\n# \u5bfc\u5165 CMakeLists.txt \u4e2d\u5df2\u7ecf\u5b9a\u4e49\u7684\u53d8\u91cf \r\nset(WORKSPACE_PATH &quot;${WORKSPACE_PATH}&quot;)\r\nset(TARGET_COMM_SRC_LIST &quot;${TARGET_COMM_SRC_LIST}&quot;)\r\nset(TARGET_COMMON_PUBLIC_INC_PATH &quot;${TARGET_COMMON_PUBLIC_INC_PATH}&quot;)\r\nset(TARGET_COMMON_PRIVATE_INC_PATH &quot;${TARGET_COMMON_PRIVATE_INC_PATH}&quot;)\r\n\r\n# CMAKE_BUILD_TYPE \u4e00\u822c\u662f Debug \/ Release \/ RelWithDebInfo \/ MinSizeRel\uff0c\u8d4b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u4ee5\u9632\u672a\u6307\u5b9a\r\n# STRING \u540e\u9762\u7684 &quot;&quot; \u662f\u63cf\u8ff0\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8bbe\u4e3a\u7a7a\r\nif(NOT DEFINED CMAKE_BUILD_TYPE)\r\n    set(CMAKE_BUILD_TYPE Release CACHE STRING &quot;&quot; FORCE)\r\nendif()\r\n\r\n# CMAKE_CONFIGURATION_TYPES \u662f\u7ed9\u50cf VS\u3001xcode \u8fd9\u7c7b\u4e00\u4e2a\u5de5\u7a0b\u91cc\u5305\u542b\u591a\u4e2a\u7f16\u8bd1\u914d\u7f6e\u7684\u5de5\u7a0b\u7528\u7684\uff0c\r\n# \u4e0a\u9762 CMAKE_BUILD_TYPE \u662f\u7ed9\u5355\u914d\u7f6e\u5de5\u7a0b\u5982 Makefile \u7528\r\nset(CMAKE_CONFIGURATION_TYPES &quot;${CMAKE_BUILD_TYPE}&quot; CACHE STRING &quot;&quot; FORCE)\r\n\r\n# \u81ea\u5b9a\u4e49\u4e00\u4e2a\u5c0f\u5199\u7684\u914d\u7f6e\u5b57\u7b26\u4e32\u53d8\u91cf\u201crelease\u201d\uff0c\u8bbe\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\r\n# \u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4f7f\u7528\r\nif(NOT DEFINED TARGET_BUILD_CFG_TYPE)\r\n    set(TARGET_BUILD_CFG_TYPE Release)\r\nendif()\r\nstring(TOLOWER ${TARGET_BUILD_CFG_TYPE} LOWER_TARGET_BUILD_CFG_TYPE)\r\n\r\n# CMAKE_GENERATOR_PLATFORM \u901a\u8fc7 -A \u547d\u4ee4\u884c\u53c2\u6570\u4f20\u7ed9 cmake.exe\r\n# \u751f\u6210\u4e00\u4e2a\u5c0f\u5199\u7684\u5e73\u53f0\u540d\uff0c\u5982 win32\/x64\/arm64\r\n# GEN_PLATFORM \u7528\u6765\u63a7\u5236\u4e0d\u540c\u5e73\u53f0\u7f16\u8bd1\u4ea7\u7269\u7684\u8f93\u51fa\u8def\u5f84\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4f7f\u7528\r\nstring(TOLOWER ${CMAKE_GENERATOR_PLATFORM} LOWER_GENERATOR_PLATFORM)\r\nset(GEN_PLATFORM ${LOWER_GENERATOR_PLATFORM})\r\nif (${GEN_PLATFORM} STREQUAL &quot;win32&quot;)\r\n    set(GEN_PLATFORM &quot;&quot;)\r\nendif()\r\n\r\n# PS\uff1a\u5982\u679c\u5de5\u7a0b\u4e2d\u6709\u591a\u79cd\u914d\u7f6e\uff0c\u8981\u6839\u636e\u914d\u7f6e\u751f\u6210\u52a8\u6001\u5e93\u6216\u9759\u6001\u5e93\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\r\n# \u4f1a\u5c06\u751f\u6210\u7c7b\u578b\u8bb0\u5230 TARGET_LIB_TYPE \u53d8\u91cf\u4e2d\r\n# set(TARGET_BUILD_TYPE_LIST &quot;debug;static_release;release&quot;) \r\n# set(TARGET_LIB_TYPES SHARED STATIC SHARED) \r\n# string(TOLOWER ${TARGET_BUILD_CFG_TYPE} LOWER_TARGET_BUILD_CFG_TYPE) \r\n# list(FIND TARGET_BUILD_TYPE_LIST ${LOWER_TARGET_BUILD_CFG_TYPE} bt_idx) \r\n# if(bt_idx EQUAL -1) \r\n#   message(FATAL_ERROR &quot;Unknown build configuration&quot;) \r\n# endif() \r\n# list(GET TARGET_LIB_TYPES ${bt_idx} TARGET_LIB_TYPE)\r\n\r\n# \u6307\u660e\u751f\u6210\u7684\u662f\u5e93\u6587\u4ef6\u800c\u4e0d\u662f exe\uff08exe \u8981\u7528 add_executable\uff09\uff0c\u7c7b\u578b\u6709 SHARED\u3001STATIC\u3001MODULE\uff0c\u9ed8\u8ba4\u662f STATIC\u3002\r\n# \u5982\u679c\u662f\u751f\u6210 windows \u7684\u8d44\u6e90 dll \u6216\u8005\u662f\u6258\u7ba1 dll \u65f6\u8981\u7528 MODULE\uff0c\r\n# \u56e0\u4e3a cmake \u4f7f\u7528 SHARED \u7c7b\u578b\u65f6\u4f1a\u5047\u5b9a\u5b83\u4e00\u5b9a\u5bfc\u51fa\u7b26\u53f7\u3002\r\n# \u8fd9\u91cc\u53ea\u4f1a\u751f\u6210\u52a8\u6001 dll \u6240\u4ee5\u76f4\u63a5\u586b SHARED\r\nadd_library(${TARGET_NAME} SHARED)\r\n\r\n# \u8bbe\u7f6e\u7f16\u8bd1\u5668\u7279\u6027\uff0c\u4f7f\u7528\u4ec0\u4e48 c++ \u6807\u51c6\uff0c\u8fd8\u53ef\u4ee5\u5f00\u542f\/\u5173\u95ed\u91cc\u9762\u67d0\u4e9b\u7279\u5b9a\u7279\u6027\r\n# TARGET_NAME \u4e00\u5b9a\u662f\u7528 add_executable() \u6216 add_library() \u8bbe\u7f6e\u8fc7\u7684\u540d\u79f0\r\ntarget_compile_features(${TARGET_NAME} PRIVATE cxx_std_14)\r\n\r\n# \u8bbe\u5b9a\u76ee\u6807\u7684\u7279\u5b9a\u5c5e\u6027\uff0c\u8fd9\u91cc\u662f\u5f00\u542f\u5168\u7a0b\u5e8f\u4f18\u5316\r\nset_target_properties(${TARGET_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)\r\n\r\n# \u6dfb\u52a0\u5e73\u53f0\u7279\u5b9a\u7684\u6587\u4ef6\uff0c\u7528\u4e00\u4e2a\u53d8\u91cf\u4fdd\u5b58\r\nset(TARGET_PLATFORM_SRC_FILES \r\n${WORKSPACE_PATH}\/src\/internal_1\/file_for_win.cpp \r\n${WORKSPACE_PATH}\/src\/internal_1\/file_for_win.rc\r\n${WORKSPACE_PATH}\/src\/internal_1\/win_resource.h\r\n)\r\n\r\n# \u5c06\u524d\u9762\u7684\u516c\u5171\u6587\u4ef6\u4ee5\u53ca\u5e73\u53f0\u7279\u5b9a\u6587\u4ef6\u6dfb\u52a0\u5230\u7f16\u8bd1\u5217\u8868\u4e2d\r\ntarget_sources(${TARGET_NAME} PRIVATE &quot;${TARGET_COMM_SRC_LIST}&quot; &quot;${TARGET_PLATFORM_SRC_FILES}&quot;)\r\n\r\n# \u8bbe\u7f6e\u5934\u6587\u4ef6\u7684\u5305\u542b\u8def\u5f84\uff0c\u8fd9\u91cc TARGET_INC_PATH_LIST \u662f\u5e73\u53f0\u7279\u5b9a\u7684\u5305\u542b\u8def\u5f84\uff0c\u663e\u7136\u662f private \u7684\r\nset(TARGET_INC_PATH_LIST &quot;&quot;) \r\ntarget_include_directories(${TARGET_NAME} \r\nPUBLIC &quot;${TARGET_COMMON_PUBLIC_INC_PATH}&quot;\r\nPRIVATE &quot;${TARGET_COMMON_PRIVATE_INC_PATH}&quot; &quot;${TARGET_INC_PATH_LIST}&quot;)\r\n\r\n# \u8bbe\u7f6e\u5bfc\u5165\u5e93\u7684\u5305\u542b\u8def\u5f84\uff0c\u5176\u5b9e\u66f4\u63a8\u8350\u7ed9\u6bcf\u4e2a\u5bfc\u5165\u5e93\u6307\u5b9a\u5168\u8def\u5f84\uff0c\u9632\u6b62\u9519\u8bef\u5f15\u7528\u5230\u5176\u4ed6\u7684\u540c\u540d\u5e93\u4e0a\u3002\r\n# \u5728\u4e0b\u9762 TARGET_LINK_LIBS_LIST \u90e8\u5206\u5c31\u53ef\u4ee5\u770b\u5230\u6bcf\u4e2a\u5bfc\u5165\u5e93\u90fd\u5e26\u6709\u8def\u5f84\u3002\r\n#if (${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;release&quot;) \r\n#    set(TARGET_LINK_PATH_LIST &quot;${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${TARGET_BUILD_CFG_TYPE}\/lib;${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${TARGET_BUILD_CFG_TYPE}&quot;) \r\n#endif()\r\n#target_link_directories(${TARGET_NAME} PRIVATE &quot;${TARGET_LINK_PATH_LIST}&quot;)\r\n\r\n# \u6dfb\u52a0\u5bfc\u5165\u5e93\uff0c\u901a\u8fc7\u4e0d\u540c\u7f16\u8bd1\u914d\u7f6e\u63a7\u5236\u751f\u6210\u8def\u5f84\r\nset(TARGET_LINK_LIBS_LIST\r\n${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${TARGET_BUILD_CFG_TYPE}\/jsoncpp.lib;\r\n${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${TARGET_BUILD_CFG_TYPE}\/libprotobuf-lite.lib\r\n)\r\ntarget_link_libraries(${TARGET_NAME} PRIVATE &quot;${TARGET_LINK_LIBS_LIST}&quot;)\r\n\r\n# \u8bbe\u7f6e\u9884\u5b9a\u4e49\u5b8f \r\nif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;debug&quot;)\r\n    set(TARGET_PREDEFINE_MACRO_LIST &quot;WIN32;_DEBUG;_WINDOWS;_USRDLL;_UNICODE;UNICODE&quot;) \r\nelseif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;release&quot;)    \r\n    set(TARGET_PREDEFINE_MACRO_LIST &quot;WIN32;NDEBUG;_WINDOWS;_USRDLL;_UNICODE;UNICODE&quot;) \r\n    if (${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;arm64&quot;)\r\n        list(APPEND TARGET_PREDEFINE_MACRO_LIST _ATL_NO_OPENGL)\r\n    endif()\r\nendif()\r\ntarget_compile_definitions(${TARGET_NAME} PRIVATE &quot;${TARGET_PREDEFINE_MACRO_LIST}&quot;)\r\n\r\n# \u8bbe\u7f6e\u989d\u5916\u7684\u7f16\u8bd1\u9009\u9879\uff0c\r\n# \u5bf9\u4e8e vs \u5de5\u7a0b\u53ef\u4ee5\u6253\u5f00\u5de5\u7a0b\u5c5e\u6027 - &quot;C\/C++&quot; - \u547d\u4ee4\u884c\uff0c\u5c06\u6240\u6709\u53c2\u6570\u590d\u5236\u8fc7\u6765\uff0c\u5220\u6389\u542b\u6709\u8def\u5f84\u7684\u547d\u4ee4\u53c2\u6570\uff0c\r\n# \u518d\u5220\u6389 \/D \u53c2\u6570\uff08\u9884\u5b9a\u4e49\u5b8f\uff0c\u4e0a\u9762\u521a\u521a\u5b9a\u4e49\u8fc7\uff09\u5c31\u53ef\u4ee5\u4e86\r\n# \u5f53\u7136\u7a33\u59a5\u8d77\u89c1\uff0c\u53ef\u4ee5\u5728\u7528 cmake \u751f\u6210\u51fa vcxproj \u540e\u518d\u548c\u539f\u6765\u7684\u5de5\u7a0b\u5bf9\u6bd4\u4e00\u4e0b\r\nif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;debug&quot;)\r\n    if(${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;win32&quot;)\r\n        set(TARGET_BUILD_CFG_LIST \/MP \/GS \/analyze- \/W3 \/Zc:wchar_t \/ZI \/Gm- \/Od \/Zc:inline \/fp:precise \/errorReport:prompt \/WX- \/Zc:forScope \/RTC1 \/Gd \/Oy- \/MDd \/FC \/EHsc \/nologo \/diagnostics:column)\r\n    endif()\r\nelseif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;release&quot;)\r\n    if (${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;arm64&quot;)\r\n        set(TARGET_BUILD_CFG_LIST \/MP \/GS \/GL \/analyze- \/W3 \/Gy \/Zc:wchar_t \/guard:cf \/Zi \/Gm- \/O1 \/Zc:inline \/fp:precise \/errorReport:prompt \/WX- \/Zc:forScope \/Gd \/Oy- \/Oi \/MD \/FC \/EHsc \/nologo \/Os \/diagnostics:column \/Oy- \/bigobj)\r\n    elseif(${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;win32&quot;)\r\n        set(TARGET_BUILD_CFG_LIST \/MP \/GS \/GL \/analyze- \/W3 \/Gy \/Zc:wchar_t \/guard:cf \/Zi \/Gm- \/O1 \/Zc:inline \/fp:precise \/errorReport:prompt \/WX- \/Zc:forScope \/Gd \/Oy- \/Oi \/MD \/FC \/EHsc \/nologo \/Os \/diagnostics:column \/Oy-)\r\n    else() # x64\r\n        set(TARGET_BUILD_CFG_LIST \/MP \/GS \/GL \/W3 \/Gy- \/Zc:wchar_t \/guard:cf \/Zi \/Gm- \/O1 \/Zc:inline \/fp:precise \/errorReport:prompt \/WX- \/Zc:forScope \/Gd \/Oi \/MD \/FC \/EHsc \/nologo \/Os \/diagnostics:column \/Oy-)\r\n    endif()\r\nendif()\r\ntarget_compile_options(${TARGET_NAME} PRIVATE ${TARGET_BUILD_CFG_LIST})\r\n\r\n# \u8bbe\u7f6e\u989d\u5916\u7684\u94fe\u63a5\u9009\u9879\r\n# \u540c\u4e0a\uff0c\u6253\u5f00\u5de5\u7a0b\u5c5e\u6027 - \u94fe\u63a5\u5668 - \u547d\u4ee4\u884c\uff0c\u590d\u5236\u6240\u6709\u5e76\u5220\u9664\u5e26\u6709\u8def\u5f84\u7684\u53c2\u6570\uff0c\r\n# \u751f\u6210\u540e\u518d\u548c\u539f\u5de5\u7a0b\u7684\u53c2\u6570\u505a\u5bf9\u6bd4\r\nif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;debug&quot;)\r\n    if(${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;win32&quot;)\r\n        set(TARGET_INK_OPT_LIST \/DEBUG \/SAFESEH \/INCREMENTAL \/SUBSYSTEM:WINDOWS)\r\n    else()\r\n        set(TARGET_INK_OPT_LIST \/DEBUG \/INCREMENTAL \/SUBSYSTEM:WINDOWS)\r\n    endif()\r\nelseif(${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;release&quot;)\r\n    if (${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;arm64&quot;)\r\n        set(TARGET_INK_OPT_LIST \/LTCG \/DEBUG \/OPT:REF \/INCREMENTAL:NO \/SUBSYSTEM:WINDOWS \/OPT:ICF \/guard:cf)\r\n    elseif(${LOWER_GENERATOR_PLATFORM} STREQUAL &quot;win32&quot;)\r\n        set(TARGET_INK_OPT_LIST \/DEBUG \/OPT:NOREF \/SAFESEH \/INCREMENTAL:NO \/SUBSYSTEM:WINDOWS \/OPT:NOICF \/guard:cf \/LTCG)\r\n    else() # x64\r\n        set(TARGET_INK_OPT_LIST \/LTCG:incremental \/DEBUG \/OPT:REF \/INCREMENTAL:NO \/SUBSYSTEM:WINDOWS \/OPT:ICF \/guard:cf)\r\n    endif()\r\nendif()\r\ntarget_link_options(${TARGET_NAME} PRIVATE ${TARGET_INK_OPT_LIST})\r\n\r\n# \u8bbe\u7f6e\u7f16\u8bd1\u4ea7\u7269\u7684\u8f93\u51fa\u76ee\u5f55\uff0c\u4e00\u822c\u6709\r\n#           Windows      Linux\r\n# RUNTIME  .exe\u3001.dll  \u53ef\u6267\u884c\u6587\u4ef6\r\n# LIBRARY      -         .so\r\n# ARCHIVE    .lib        .a\r\nif (${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;debug&quot; OR ${LOWER_TARGET_BUILD_CFG_TYPE} STREQUAL &quot;release&quot;)\r\n    set(TARGET_RUNTIME_OUTPUT ${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${CONF_TYPE}\/)\r\n    set(TARGET_LIBRARY_OUTPUT ${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${CONF_TYPE}\/lib)\r\n    set(TARGET_ARCHIVE_OUTPUT ${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${CONF_TYPE}\/lib)\r\nendif()\r\nset_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TARGET_RUNTIME_OUTPUT})\r\nset_target_properties(${TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TARGET_LIBRARY_OUTPUT})\r\nset_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${TARGET_ARCHIVE_OUTPUT})\r\n\r\n# \u8bbe\u7f6e pdb \u8def\u5f84 \r\nset_target_properties(${TARGET_NAME} PROPERTIES PDB_OUTPUT_DIRECTORY ${WORKSPACE_PATH}\/bin\/${GEN_PLATFORM}\/${CONF_TYPE}\/)\r\n\r\n# \u8bbe\u7f6e\u6e90\u4ee3\u7801\u6587\u4ef6\u5982\u4f55\u6446\u653e\u5728\u89e3\u51b3\u65b9\u6848\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u7684\u903b\u8f91\u6587\u4ef6\u5939\uff0c\u5373 .filter \u6587\u4ef6\u7684\u6548\u679c\r\nsource_group(&quot;source_file&quot; FILES \r\n${WORKSPACE_PATH}\/src\/app1\/sample.h \r\n${WORKSPACE_PATH}\/src\/app2\/sample.h \r\n)\r\n\r\nsource_group(&quot;source_file\/subs&quot; FILES \r\n${WORKSPACE_PATH}\/src\/app1\/sub1\/sample2.h\r\n${WORKSPACE_PATH}\/src\/app1\/sub1\/sample2.cpp\r\n)\r\n\r\nsource_group(&quot;&quot; FILES\r\n${WORKSPACE_PATH}\/src\/res\/resource.h \r\n${WORKSPACE_PATH}\/src\/res\/resource.rc \r\n)\r\n\r\nsource_group(&quot;header_files&quot; FILES \r\n${WORKSPACE_PATH}\/inc\/sample1.h \r\n${WORKSPACE_PATH}\/inc\/sample2.h \r\n)\r\n\r\nsource_group(&quot;pch&quot; FILES\r\n${WORKSPACE_PATH}\/src\/stdafx.cpp\r\n)\r\n<\/pre>\n<h2>\u751f\u6210&#038;\u7f16\u8bd1<\/h2>\n<p>\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u8c03\u7528 cmake.exe \u8fdb\u884c\u751f\u6210\uff0c\u5047\u8bbe\u5f53\u524d\u76ee\u5f55\u5c31\u662f CMakeLists.txt \u6240\u5728\u76ee\u5f55\u3002<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nset blt_arch_type=x64\r\nset blt_cfg_type=release\r\ncmake -B .\\cmake\\%blt_arch_type%\\%blt_cfg_type% -S . -A %blt_arch_type% -D TARGET_BUILD_CFG_TYPE=%blt_cfg_type%\r\n<\/pre>\n<p>\u6210\u529f\u751f\u6210\u5de5\u7a0b\u6587\u4ef6\u540e\u518d\u8c03\u7528 MSBuild.exe \u8fdb\u884c\u7f16\u8bd1<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nif &quot;%VSCMD_VER%&quot; EQU &quot;&quot; (\r\n    echo &quot;init msvc env&quot;\r\n    call &quot;C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\Common7\\Tools\\VsDevCmd.bat&quot;\r\n)\r\nMSBUILD %~dp0\\cmake\\%blt_arch_type%\\%blt_cfg_type%\\TestProject.vcxproj \/t:rebuild \/p:Configuration=&quot;Release&quot;;Platform=%blt_arch_type%;BuildProjectReferences=false \/m\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6784\u5efa\u7cfb\u7edf\u63cf\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528\u6784\u5efa\u5de5\u5177\u4ece\u9879\u76ee\u7684\u6e90\u4ee3\u7801\u6784\u5efa\u9879\u76ee\u7684\u53ef\u6267\u884c\u6587\u4ef6\u548c\u5e93\uff0c\u4ece\u800c\u81ea\u52a8\u6267\u884c\u8be5\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u6784\u5efa\u7cfb\u7edf\u53ef\u4ee5\u662f&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/flandre-scarlet.moe\/blog\/1994\/\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[99],"class_list":["post-1994","post","type-post","status-publish","format-standard","hentry","category-programs","tag-cmake"],"_links":{"self":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/1994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/comments?post=1994"}],"version-history":[{"count":15,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":2009,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions\/2009"}],"wp:attachment":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}