{"id":853,"date":"2022-06-15T09:31:16","date_gmt":"2022-06-15T01:31:16","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=853"},"modified":"2025-10-09T13:55:34","modified_gmt":"2025-10-09T05:55:34","slug":"cmake-basuc-tutorial","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2022\/06\/15\/cmake-basuc-tutorial\/","title":{"rendered":"CMake\u57fa\u7840\u4ecb\u7ecd"},"content":{"rendered":"<p>&emsp;&emsp;\u672c\u7bc7\u6587\u7ae0\u5c06\u7b80\u8981\u4ecb\u7ecdCMake\u7684\u57fa\u7840\u5165\u95e8\uff0c<a href = \"https:\/\/www.bilibili.com\/video\/BV1vR4y1u77h?p=1\">\u53c2\u8003\u89c6\u9891<\/a>\u3002<\/p>\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001CMkae\u4e00\u4e2aHello World<\/h2>\n<p>\uff081\uff09\u7f16\u5199\u4e00\u4e2aHello World\u6587\u6863<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n\nint main()\n{\n    std::cout &lt;&lt; &quot;Hello world&quot; &lt;&lt; std::endl;\n    return 0;\n}<\/code><\/pre>\n<p>\uff082\uff09\u7f16\u5199\u4e00\u4e2aCMakeLists.txt\uff0c\u6ce8\u610f\uff0c\u6587\u4ef6\u540d\u4e25\u683c\u533a\u5206\u5927\u5c0f\u5199<\/p>\n<pre><code class=\"language-cmake\"># CMakeLists.txt\n\nPROJECT (HELLO)\n\nSET(SRC_LIST main.cpp)\n\nMESSAGE(STATUS &quot;This is BINARY dir &quot; ${HELLO_BINARY_DIR})\n\nMESSAGE(STATUS &quot;This is SOURCE dir &quot;${HELLO_SOURCE_DIR})\n\nADD_EXECUTABLE(hello ${SRC_LIST})<\/code><\/pre>\n<p>(3)\u521b\u5efa\u4e00\u4e2abuild\u6587\u4ef6\u5939\uff0c\u6211\u4eec\u5c06\u5728\u8be5\u6587\u4ef6\u4e0b\u9762\u751f\u4ea7Makefile\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-shell\">\nmkdir build\n\n# \u6b64\u65f6\u6587\u4ef6\u76ee\u5f55\u5982\u4e0b\n.\n\u251c\u2500\u2500 buld\n\u251c\u2500\u2500 CMakeLists.txt\n\u2514\u2500\u2500 main.cpp<\/code><\/pre>\n<p>(4)\u8fdb\u5165build\u6587\u4ef6\u5939\uff0c\u7136\u540e\u8c03\u7528cmake\u547d\u4ee4\u751f\u6210makefile\uff0c\u6700\u540e\u4f7f\u7528make\u751f\u4ea7\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-shell\">cd build\n\n# ..\u8868\u793a\u8c03\u7528\u4e0a\u7ea7\u76ee\u5f55\u7684CMakeLists.txt\ncmake ..\n\nmake\n\nls\n# CMakeCache.txt  CMakeFiles  cmake_install.cmake  hello  Makefile\n\n.\/hello\n# Hello world<\/code><\/pre>\n<h2>\u4e8c\u3001\u8bed\u6cd5\u57fa\u672c\u4ecb\u7ecd<\/h2>\n<p>&emsp;&emsp;\u6211\u4eec\u5c06\u4f7f\u7528\u4e0a\u9762\u7684CMakeLists.txt\u6765\u4ecb\u7ecd\u5176\u57fa\u672c\u8bed\u6cd5\uff0c\u4e0b\u9762\u4ecb\u7ecd\u51e0\u4e2a\u5173\u952e\u5b57\u3002<\/p>\n<h3>2.1 PROJECT<\/h3>\n<pre><code class=\"language-cmake\"># \u6307\u5b9a\u4e86\u5de5\u7a0b\u7684\u540d\u5b57\uff0c\u9ed8\u8ba4\u652f\u6301\u6240\u6709\u8bed\u8a00\nPROJECT (HELLO)   \n\n# \u6307\u5b9a\u4e86\u5de5\u7a0b\u7684\u540d\u5b57\uff0c\u5e76\u4e14\u652f\u6301\u8bed\u8a00\u662fC++\nPROJECT (HELLO CXX)      \n\n# \u6307\u5b9a\u4e86\u5de5\u7a0b\u7684\u540d\u5b57\uff0c\u5e76\u4e14\u652f\u6301\u8bed\u8a00\u662fC\u548cC++\nPROJECT (HELLO C CXX)      <\/code><\/pre>\n<p>\u8be5\u6307\u5b9a\u9690\u5f0f\u5b9a\u4e49\u4e86\u4e24\u4e2aCMAKE\u7684\u53d8\u91cf<\/p>\n<pre><code class=\"language-cmake\">&lt;projectname&gt;_BINARY_DIR\uff0c\u672c\u4f8b\u4e2d\u662f HELLO_BINARY_DIR\n\n&lt;projectname&gt;_SOURCE_DIR\uff0c\u672c\u4f8b\u4e2d\u662f HELLO_SOURCE_DIR<\/code><\/pre>\n<p>&emsp;&emsp;MESSAGE\u5173\u952e\u5b57\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8005\u4e24\u4e2a\u53d8\u91cf\uff0c\u5f53\u524d\u90fd\u6307\u5411\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u540e\u9762\u4f1a\u8bb2\u5916\u90e8\u7f16\u8bd1<\/p>\n<ul>\n<li>\u95ee\u9898\uff1a\u5982\u679c\u6539\u4e86\u5de5\u7a0b\u540d\uff0c\u8fd9\u4e24\u4e2a\u53d8\u91cf\u540d\u4e5f\u4f1a\u6539\u53d8\u3002<\/li>\n<li>\u89e3\u51b3\uff1a\u53c8\u5b9a\u4e49\u4e24\u4e2a\u9884\u5b9a\u4e49\u53d8\u91cf\uff1aPROJECT_BINARY_DIR\u548cPROJECT_SOURCE_DIR\uff0c\u8fd9\u4e24\u4e2a\u53d8\u91cf\u548cHELLO_BINARY_DIR\uff0cHELLO_SOURCE_DIR\u662f\u4e00\u81f4\u7684\u3002\u6240\u4ee5\u6539\u4e86\u5de5\u7a0b\u540d\u4e5f\u6ca1\u6709\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<h3>2.2 SET<\/h3>\n<p>&emsp;&emsp;\u7528\u6765\u663e\u793a\u7684\u6307\u5b9a\u53d8\u91cf\u7684\u3002<\/p>\n<pre><code class=\"language-cmake\"># SRC_LIST\u53d8\u91cf\u5c31\u5305\u542b\u4e86main.cpp\nSET(SRC_LIST main.cpp)\n\n# \u591a\u4e2acpp\u6587\u4ef6\u7684\u60c5\u51b5\nSET(SRC_LIST main.cpp t1.cpp t2.cpp) <\/code><\/pre>\n<h3>2.3 MESSAGE<\/h3>\n<p>&emsp;&emsp;\u5411\u7ec8\u7aef\u8f93\u51fa\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4fe1\u606f\uff0c\u4e3b\u8981\u5305\u542b\u4e00\u4e0b\u4e09\u79cd\u4fe1\u606f\uff1a<\/p>\n<ol>\n<li>SEND_ERROR\uff0c\u4ea7\u751f\u9519\u8bef\uff0c\u751f\u4ea7\u8fc7\u7a0b\u88ab\u8df3\u8fc7\u3002<\/li>\n<li>SATUS\uff0c\u8f93\u51fa\u524d\u7f00\u4e3a\u201c-\u201d\u7684\u4fe1\u606f\u3002<\/li>\n<li>FATAL_ERROR\uff0c\u7acb\u5373\u7ec8\u6b62\u6240\u6709cmake\u8fc7\u7a0b\u3002<\/li>\n<\/ol>\n<h3>2.4 ADD_EXECUTABLE<\/h3>\n<p>&emsp;&emsp;\u751f\u4ea7\u53ef\u6267\u884c\u6587\u4ef6\uff0c<code><code>ADD_EXECUTABLE(hello ${SRC_LIST})<\/code><code>\u751f\u4ea7\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u6267\u884c\u6587\u4ef6\u540d\u4e3ahello\uff0c\u6e90\u6587\u4ef6\u8bfb\u53d6SRC_LIST\u4e2d\u7684\u5185\u5bb9\u3002\u4e5f\u7b49\u540c\u4e8e\uff0c<\/code><code>ADD_EXECUTABLE(hello main.cpp)<\/code><\/code>\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u53ef\u6267\u884c\u6587\u4ef6hello\uff0c\u4e0e\u5de5\u7a0b\u540dHELLO\u65e0\u5173\u3002<\/p>\n<h3>2.4 \u57fa\u672c\u539f\u5219<\/h3>\n<ol>\n<li>\u53d8\u91cf\u4f7f\u7528<code><code>${ }<\/code><\/code>\u7684\u65b9\u5f0f\u53d6\u503c\uff0c\u4f46\u662f\u5728IF\u8bed\u53e5\u4e2d\u76f4\u63a5\u4f7f\u7528\u53d8\u91cf\u540d\u3002<\/li>\n<li><code><code>\u6307\u4ee4(\u53c2\u65701 \u53c2\u65702...)<\/code><\/code>\uff0c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528<strong>\u7a7a\u683c<\/strong>\u6216<strong>\u5206\u53f7<\/strong>\u9694\u5f00\u3002<\/li>\n<li>\u6307\u4ee4\u4e0e\u5927\u5c0f\u5199\u65e0\u5173\uff0c\u53c2\u6570\u548c\u53d8\u91cf\u4e0e\u5927\u5c0f\u5199\u6709\u5173\uff0c\u63a8\u8350\u6307\u4ee4\u4f7f\u7528\u5927\u5199\u3002<\/li>\n<\/ol>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li><code><code>SET(SRC_LIST main.cpp)<\/code><code>Equal2<\/code><code>SET(SRC_LIST &quot;main.cpp&quot;)<\/code><\/code>\uff0c\u5f53\u6e90\u6587\u4ef6\u6709\u7a7a\u683c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5f15\u53f7\u3002<\/li>\n<li><code><code>ADD_EXECUTABLE(hello main.cpp)<\/code><code>\u53ef\u4ee5\u5199\u4f5c<\/code><code>ADD_EXECUTABLE(hello main)<\/code><\/code>\uff0c\u5b83\u4f1a\u81ea\u52a8\u53bb\u5bfb\u627e.c\u6216\u8005\u65f6.cpp\u6587\u4ef6\uff0c\u4f46\u662f\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\uff0c\u53ef\u80fd\u5b58\u5728\u591a\u4e2afilename\u4e3amain\u7684\u6587\u4ef6\uff0c\u5bfc\u81f4\u51b2\u7a81\u3002<\/li>\n<\/ol>\n<h2>\u4e09\u3001\u8ba9Hello\u66f4\u50cf\u4e00\u4e2a\u5de5\u7a0b<\/h2>\n<p>&emsp;&emsp;\u8fd9\u91cc\u5c06\u6e90\u7801\u653e\u5165<code><code>.\/src<\/code><\/code>\u4e0b\uff0c\u6b64\u65f6\u9700\u8981\u518d\u521b\u5efa\u4e24\u4e2aCMakeLists.txt\uff0c\u4e00\u4e2a\u5728\u5916\u5c42\u76ee\u5f55\uff0c\u4e00\u4e2a\u5728src\u76ee\u5f55\u4e0b\u3002\u6b64\u65f6tree\uff1a<\/p>\n<pre><code class=\"language-shell\">.\n\u251c\u2500\u2500 build\n\u251c\u2500\u2500 CMakeLists.txt\n\u2514\u2500\u2500 src\n    \u251c\u2500\u2500 CMakeLists.txt\n    \u2514\u2500\u2500 main.cpp<\/code><\/pre>\n<p>&emsp;&emsp;\u5916\u90e8CMakeLists.txt\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cmake\">PROJECT (HELLO)\n\n# ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])\nADD_SUBDIRECTORY(src bin)<\/code><\/pre>\n<ol>\n<li><code><code>ADD_SUBDIRECTORY()<\/code><\/code>\uff0c\u7528\u4e8e\u6307\u5b9a\u5de5\u7a0b\u6587\u4ef6src\u7684\u5b58\u653e\u76ee\u5f55\uff0c\u540c\u65f6\u53ef\u4ee5\u6307\u5b9a\u4e2d\u95f4\u4e8c\u8fdb\u5236\u548c\u76ee\u6807\u4e8c\u8fdb\u5236\u7684\u5b58\u653e\u4f4d\u7f6e\u3002<\/li>\n<li><code><code>EXLUCE_FROM_ALL<\/code><\/code>\uff0c\u5c06\u5199\u7684\u76ee\u5f55\u4ece\u7f16\u8bd1\u4e2d\u6392\u9664\uff0c\u5982\u7a0b\u5e8f\u4e2d\u7684example\u3002<\/li>\n<li><code><code>ADD_SUBDIRECTORY(src bin)<\/code><code>\uff0c\u5c06src\u5b50\u76ee\u5f55\u52a0\u5165\u5de5\u7a0b\uff0c\u5e76\u6307\u5b9a\u7f16\u8bd1\u8f93\u51fa\uff08\u5305\u542b\u4e2d\u95f4\u7ed3\u679c\uff09\u7684\u8def\u5f84\u4e3abin\u76ee\u5f55\u3002\u5982\u679c\u4e0d\u6307\u5b9abin\u76ee\u5f55\uff0c\u6240\u6709\u8f93\u51fa\uff08\u5305\u62ec\u4e2d\u95f4\u7ed3\u679c\uff09\u90fd\u5c06\u5b58\u653e\u5728<\/code><code>build\/src<\/code><\/code>\u76ee\u5f55\u4e0b\u3002<\/li>\n<\/ol>\n<p>&emsp;&emsp;src\u4e0bCMakeLists.txt\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-cmake\">SET(SRC_LIST main.cpp)\n\n# \u66f4\u6539\u4e8c\u8fdb\u5236\u7684\u4fdd\u5b58\u76ee\u5f55\n# SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR\/bin})\n# SET(LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR\/lib})\n\nADD_EXECUTABLE(hello.exe ${SRC_LIST})<\/code><\/pre>\n<h2>\u56db\u3001\u5b89\u88c5<\/h2>\n<p>&emsp;&emsp;\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528<code><code>make install<\/code><code>\u5b89\u88c5\uff0c\u8fd9\u91cc\u9700\u8981\u4f7f\u7528\u7684cmake\u6307\u4ee4\u6709<\/code><code>INSTALL<\/code><\/code>\u3002\u8fd9\u91cc\u6211\u4eec\u5b89\u88c5\u6587\u4ef6\u3001\u811a\u672c\u548c\u6587\u6863\uff0c\u5206\u522b\u5bf9\u5e94\u4e86COPYRIGHT\u3001README\uff1brunhello.sh\u3001doc|hello.txt\u3002<\/p>\n<pre><code class=\"language-shell\">.\n\u251c\u2500\u2500 build\n\u251c\u2500\u2500 CMakeLists.txt\n\u251c\u2500\u2500 COPYRIGHT\n\u251c\u2500\u2500 doc\n\u2502   \u2514\u2500\u2500 hello.txt\n\u251c\u2500\u2500 README\n\u251c\u2500\u2500 runhello.sh\n\u2514\u2500\u2500 src\n    \u251c\u2500\u2500 CMakeLists.txt\n    \u2514\u2500\u2500 main.cpp<\/code><\/pre>\n<h3>4.1 \u5b89\u88c5\u6587\u4ef6 - FILES<\/h3>\n<p>&emsp;&emsp;\u6211\u4eec\u5728\u5916\u5c42CMakeLists.txt\u4e2d\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-cmake\">INSTALL(FILES COPYRIGHT README DESTINATION myDemo\/doc\/camke)<\/code><\/pre>\n<p>\u5176\u4e2dFILES\u6307\u5b9a\u7c7b\u578b\uff0cDESTINATION\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u9ed8\u8ba4\u6307\u5411usr\/locl\/myDemo\/doc\/camke\uff0c\u5982\u679c\u8981\u6539\u53d8\u7edd\u5bf9\u8def\u5f84\u53ef\u4ee5\u5728cmake\u65f6\u4f7f\u7528<code><code>cmake -DCMAKE_INSTALL_PERFIX=\/home\/<\/code><\/code>\u4e00\u7c7b\u7684\u547d\u4ee4\u3002<\/p>\n<h3>4.2 \u5b89\u88c5\u811a\u672c - PROGRAMS<\/h3>\n<p>&emsp;&emsp;PROGRAMS\u6307\u975e\u76ee\u6807\u6587\u4ef6\u7684\u53ef\u6267\u884c\u5b89\u88c5\u7a0b\u5e8f\uff08\u4f8b\u5982\u811a\u672c\uff09\uff1a<\/p>\n<pre><code class=\"language-cmake\">INSTALL(PROGRAMS runhello.sh DESTINATION bin)<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u65f6\uff0c\u8fd9\u91cc\u811a\u672c\u540c\u6837\u5b89\u88c5\u5728\/usr\/local\u4e0b<\/p>\n<h3>4.3 \u5b89\u88c5\u6587\u672c - doc<\/h3>\n<p>&emsp;&emsp;\u56e0\u4e3a\u8fd9\u91cc\u6211\u4eec\u5c06\u9700\u8981\u5b89\u88c5\u7684hello.txt\u653e\u5728\u4e86\/doc\u4e0b\uff0c\u6240\u4ee5\u8fd9\u91cc\u6709\u4e24\u4e2a\u9009\u62e9\uff0c\u4e00\u662f\u5728doc\u4e0b\u5199\u4e00\u4e2aCMakeLists.txt\uff0c\u4e8c\u662f\u5199\u5728\u5de5\u7a0b\u6587\u4ef6\u91cc\uff0c\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u5199\u5230\u5de5\u7a0b\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-cmake\">INSTALL(DIRECTORY doc DESTINATION myDemo\/doc\/camke)\n# or\nINSTALL(DIRECTORY doc\/ DESTINATION myDemo\/doc\/camke)<\/code><\/pre>\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f<code><code>doc<\/code><code>\u548c<\/code><code>doc\/<\/code><\/code>\u7684\u533a\u522b\uff0c\u524d\u8005\u5b89\u88c5doc\u8fd9\u4e2a\u6587\u4ef6\u5939\u5230\u76ee\u6807\uff0c\u540e\u8005\u5b89\u88c5\u6587\u4ef6\u5939\u91cc\u7684\u6240\u6709\u6587\u4ef6\u5230\u76ee\u6807\u3002<\/p>\n<h3>4.4 \u5c0f\u7ed3<\/h3>\n<p>&emsp;&emsp;\u5916\u5c42cmake\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cmake\">PROJECT (HELLO)\n\nADD_SUBDIRECTORY(src bin)\n\nINSTALL(FILES COPYRIGHT README DESTINATION myDemo\/doc\/camke)\nINSTALL(PROGRAMS runhello.sh DESTINATION bin)\n\nINSTALL(DIRECTORY doc DESTINATION myDemo\/doc\/camke)<\/code><\/pre>\n<p>\u5b89\u88c5\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-shell\">Install the project...\n-- Install configuration: &quot;&quot;\n-- Installing: \/usr\/local\/myDemo\/doc\/camke\/COPYRIGHT\n-- Installing: \/usr\/local\/myDemo\/doc\/camke\/README\n-- Installing: \/usr\/local\/bin\/runhello.sh\n-- Installing: \/usr\/local\/myDemo\/doc\/camke\/doc\n-- Installing: \/usr\/local\/myDemo\/doc\/camke\/doc\/hello.txt<\/code><\/pre>\n<h2>\u4e94\u3001\u9759\u6001\u5e93\u4e0e\u52a8\u6001\u5e93<\/h2>\n<p>&emsp;&emsp;\u73b0\u5728\u6211\u4eec\u6709\u5de5\u7a0b\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-shell\">.\n\u251c\u2500\u2500 build\n\u251c\u2500\u2500 CMakeLists.txt\n\u2514\u2500\u2500 lib\n    \u251c\u2500\u2500 CMakeLists.txt\n    \u251c\u2500\u2500 hello.cpp\n    \u2514\u2500\u2500 hello.h<\/code><\/pre>\n<p>\u5916\u5c42CMakeLists.txt\uff0c\u8fd9\u91cc\u6dfb\u52a0\u4e86\u4e00\u4e2acmake\u7684\u7248\u672c\u9700\u6c42\uff0c\u907f\u514d\u629b\u51fa\u8b66\u544a\u3002<\/p>\n<pre><code class=\"language-cmake\">PROJECT (HELLO)\nCMAKE_MINIMUM_REQUIRED(VERSION 3.5)\n\nADD_SUBDIRECTORY(lib bin)<\/code><\/pre>\n<p>\u5185\u90e8CMakeLists.txt\uff1a<\/p>\n<pre><code class=\"language-cmake\">SET(LIBHELLO_SRC hello.cpp)\n\nADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})\n# \u91cd\u547d\u540d\u9759\u6001\u5e93\nSET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME &quot;hello&quot;)\n# \u6e05\u9664\u91cd\u540d\u7684hello\uff0c\u907f\u514d\u548c\u540e\u9762\u7684\u52a8\u6001\u5e93\u51b2\u7a81\nSET_TARGET_PROPERTIES(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)\n\nADD_LIBRARY(hello_shared SHARED ${LIBHELLO_SRC})\nSET_TARGET_PROPERTIES(hello_shared PROPERTIES OUTPUT_NAME &quot;hello&quot;)\nSET_TARGET_PROPERTIES(hello_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)\n# \u7248\u672c\u53f7\uff0c\u524d\u8005\u662f\u52a8\u6001\u5e93\u672c\u7248\uff0c\u540e\u8005\u662fAPI\u7248\u672c\nSET_TARGET_PROPERTIES(hello_static PROPERTIES VERSION 1.0 SOVERSION 1.0)<\/code><\/pre>\n<p>&emsp;&emsp;\u5728\u6784\u5efa\u5b8c\u6210\u4e4b\u540e\uff0c\u8fdb\u5165\/build\/bin\u76ee\u5f55\uff0c\u53ef\u4ee5\u67e5\u770b<\/p>\n<pre><code class=\"language-shell\">xjt@ubuntu:~\/MyFloder\/myCmakeDemo\/build\/bin$ tree\n.\n\u251c\u2500\u2500 CMakeFiles\n\u2502   \u251c\u2500\u2500 CMakeDirectoryInformation.cmake\n\u2502   \u251c\u2500\u2500 hello_shared.dir\n\u2502   \u2502   \u251c\u2500\u2500 build.make\n\u2502   \u2502   \u251c\u2500\u2500 cmake_clean.cmake\n\u2502   \u2502   \u251c\u2500\u2500 CXX.includecache\n\u2502   \u2502   \u251c\u2500\u2500 DependInfo.cmake\n\u2502   \u2502   \u251c\u2500\u2500 depend.internal\n\u2502   \u2502   \u251c\u2500\u2500 depend.make\n\u2502   \u2502   \u251c\u2500\u2500 flags.make\n\u2502   \u2502   \u251c\u2500\u2500 hello.cpp.o\n\u2502   \u2502   \u251c\u2500\u2500 link.txt\n\u2502   \u2502   \u2514\u2500\u2500 progress.make\n\u2502   \u251c\u2500\u2500 hello_static.dir\n\u2502   \u2502   \u251c\u2500\u2500 build.make\n\u2502   \u2502   \u251c\u2500\u2500 cmake_clean.cmake\n\u2502   \u2502   \u251c\u2500\u2500 cmake_clean_target.cmake\n\u2502   \u2502   \u251c\u2500\u2500 CXX.includecache\n\u2502   \u2502   \u251c\u2500\u2500 DependInfo.cmake\n\u2502   \u2502   \u251c\u2500\u2500 depend.internal\n\u2502   \u2502   \u251c\u2500\u2500 depend.make\n\u2502   \u2502   \u251c\u2500\u2500 flags.make\n\u2502   \u2502   \u251c\u2500\u2500 hello.cpp.o\n\u2502   \u2502   \u251c\u2500\u2500 link.txt\n\u2502   \u2502   \u2514\u2500\u2500 progress.make\n\u2502   \u2514\u2500\u2500 progress.marks\n\u251c\u2500\u2500 cmake_install.cmake\n\u251c\u2500\u2500 libhello.a\n\u251c\u2500\u2500 libhello.so\n\u2514\u2500\u2500 Makefile<\/code><\/pre>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u89c1\uff0c\u5df2\u7ecf\u751f\u6210\u4e86\u9759\u6001\u5e93libhello.a\u548c\u52a8\u6001\u5e93libhello.so\uff0c\u540e\u7f00\u5728windows\u5e73\u53f0\u4e3a.lib\u548c.dll\u3002<\/p>\n<h3>5.1 \u5b89\u88c5\u9759\u6001\u5e93<\/h3>\n<p>&emsp;&emsp;\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u5b89\u88c5\u5934\u6587\u4ef6\u548c\u5e93\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cmake\"># \u6587\u4ef6\u653e\u5230\u8be5\u76ee\u5f55\u4e0b\nINSTALL(FILES hello.h DESTINATION include\/hello)\n\n# \u4e8c\u8fdb\u5236\uff0c\u9759\u6001\u5e93\uff0c\u52a8\u6001\u5e93\u5b89\u88c5\u90fd\u7528TARGETS\n# ARCHIVE \u7279\u6307\u9759\u6001\u5e93\uff0cLIBRARY \u7279\u6307\u52a8\u6001\u5e93\uff0cRUNTIME \u7279\u6307\u53ef\u6267\u884c\u76ee\u6807\u4e8c\u8fdb\u5236\u3002\nINSTALL(TARGETS hello_shared hello_static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)<\/code><\/pre>\n<p>&emsp;&emsp;\u6784\u5efa\u4e4b\u540e\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-shell\">Install the project...\n-- Install configuration: &quot;&quot;\n-- Installing: \/usr\/local\/include\/hello\/hello.h\n-- Installing: \/usr\/local\/lib\/libhello.so\n-- Installing: \/usr\/local\/lib\/libhello.a<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u672c\u7bc7\u6587\u7ae0\u5c06\u7b80\u8981\u4ecb\u7ecdCMake\u7684\u57fa\u7840\u5165\u95e8\uff0c\u53c2\u8003\u89c6\u9891\u3002 \u4e00\u3001CMkae\u4e00\u4e2aHello World \uff081\uff09\u7f16\u5199\u4e00 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/853"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":854,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/853\/revisions\/854"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}