{"id":334,"date":"2022-02-09T16:33:09","date_gmt":"2022-02-09T08:33:09","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=334"},"modified":"2022-02-09T22:35:58","modified_gmt":"2022-02-09T14:35:58","slug":"cmake-%e7%ae%80%e6%98%8e%e6%89%8b%e5%86%8c","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=334","title":{"rendered":"cmake \u7b80\u660e\u624b\u518c"},"content":{"rendered":"<h1>1. \u6982\u8ff0<\/h1>\n<p>\u7528 CMakeLists.txt \u6765\u63cf\u8ff0\u6784\u5efa\u4f9d\u8d56\u5173\u7cfb\u548c\u6784\u5efa\u76ee\u6807\uff0c \u7528 <code>cmake &lt;options&gt; &lt;folder&gt;<\/code> \u6765\u751f\u6210 Makefile.<br \/>\n\u8fd9\u4e2a <code>&lt;folder&gt;<\/code> \u5c31\u662fCMakeLists.txt \u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55.<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u5165\u5b8f\u53d8\u91cf\u5b9a\u4e49\uff0c\u4f8b\u5982<\/p>\n<pre><code>cmake -DABSL_BUILD_TESTING=ON -DABSL_USE_GOOGLETEST_HEAD=ON -DCMAKE_CXX_STANDARD=11 ..\ncmake --build . --target all<\/code><\/pre>\n<p>\u8fd9\u4e2a\u9879\u76ee <a href=\"https:\/\/github.com\/abseil\/abseil-cpp\">https:\/\/github.com\/abseil\/abseil-cpp<\/a> \u4e2d\u7684 cmake \u7528\u6cd5\u53ef\u4f5c\u53c2\u8003<\/p>\n<h1>2. \u5feb\u901f\u4e0a\u624b<\/h1>\n<pre><code>cmake_minimum_required(VERSION 3.0.0)\nproject(webrtc_primer VERSION 0.1.0)\n\ninclude(CTest)\nenable_testing()\n\nadd_executable(webrtc_primer main.cpp)\n\nset(CPACK_PROJECT_NAME ${PROJECT_NAME})\nset(CPACK_PROJECT_VERSION ${PROJECT_VERSION})\ninclude(CPack)<\/code><\/pre>\n<p>main.cpp<\/p>\n<pre><code>#include &lt;iostream&gt;\n\nint main(int, char**) {\n    std::cout &lt;&lt; &quot;Hello, world!\\n&quot;;\n}<\/code><\/pre>\n<p>\u6267\u884c\u547d\u4ee4 <\/p>\n<pre><code>mkdir bld\ncd bld\ncmake ..\nmake<\/code><\/pre>\n<h1>3.  CMakeLists.txt \u8bed\u6cd5<\/h1>\n<ul>\n<li>\u7528 # \u5f00\u5934\u6765\u6dfb\u52a0\u6ce8\u91ca<\/li>\n<li>\u7528 project \u6765\u6307\u5b9a\u9879\u76ee\u540d\u79f0<\/li>\n<li>\u7528 add_subdirectory \u6765\u6dfb\u52a0\u5b50\u76ee\u5f55<\/li>\n<li>\u7528 set \u547d\u4ee4\u6765\u5b9a\u4e49\u53d8\u91cf\u548c\u8d4b\u503c\uff0c \u5728\u975e if \u8bed\u53e5\u4e2d\u7528 ${var} \u7684\u683c\u5f0f\u6765\u5f15\u7528\u53d8\u91cf<\/li>\n<li>\u5206\u652f\u8bed\u53e5<\/li>\n<li>\u5faa\u73af\u8bed\u53e5<\/li>\n<\/ul>\n<h1>4. CMake \u5185\u90e8\u53d8\u91cf<\/h1>\n<p>CMAKE_C_COMPILER<br \/>\nCMAKE_CXX_COMPILER<br \/>\nCMAKE_C_FLAGS<br \/>\nCMAKE_BUILD_TYPE<br \/>\nEXECUTABLE_OUTPUT_PATH<br \/>\nLIBRARY_OUTPUT_PATH<\/p>\n<h1>5. CMake \u547d\u4ee4<\/h1>\n<ul>\n<li>project<\/li>\n<li>message: \u6253\u5370\u6d88\u606f<\/li>\n<li>include_directories<\/li>\n<li>link_directories<\/li>\n<li>add_subdirectory<\/li>\n<li>add_executable<\/li>\n<li>add_library<\/li>\n<li>add_definitions\uff1a \u6dfb\u52a0\u7f16\u8bd1\u53c2\u6570<\/li>\n<li>target_link_libraries: \u76f8\u5f53\u4e8e\u6307\u5b9a -l \u53c2\u6570<\/li>\n<li>add_custom_target<\/li>\n<li>set_target_properties<\/li>\n<li>link_libraries<\/li>\n<\/ul>\n<h1>6. CMake FAQ<\/h1>\n<h2>1\uff09  \u600e\u6837\u83b7\u5f97\u4e00\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6e90\u6587\u4ef6<\/h2>\n<blockquote>\n<blockquote>\n<p>aux_source_directory(<dir> <variable>)<\/p>\n<p>\u5c06dir\u4e2d\u6240\u6709\u6e90\u6587\u4ef6\uff08\u4e0d\u5305\u62ec\u5934\u6587\u4ef6\uff09\u4fdd\u5b58\u5230\u53d8\u91cfvariable\u4e2d\uff0c\u7136\u540e\u53ef\u4ee5add_executable (ss7gw ${variable})\u8fd9\u6837\u4f7f\u7528\u3002<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>2\uff09  \u600e\u6837\u6307\u5b9a\u9879\u76ee\u7f16\u8bd1\u76ee\u6807<\/h2>\n<blockquote>\n<blockquote>\n<p>project\u547d\u4ee4\u6307\u5b9a<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>3\uff09  \u600e\u6837\u6dfb\u52a0\u52a8\u6001\u5e93\u548c\u9759\u6001\u5e93<\/h2>\n<blockquote>\n<blockquote>\n<p>target_link_libraries\u547d\u4ee4\u6dfb\u52a0\u5373\u53ef<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>4\uff09  \u600e\u6837\u5728\u6267\u884cCMAKE\u65f6\u6253\u5370\u6d88\u606f<\/h2>\n<blockquote>\n<blockquote>\n<p>message([SEND_ERROR | STATUS | FATAL_ERROR] &quot;message to display&quot; ...)<\/p>\n<p>\u6ce8\u610f\u5927\u5c0f\u5199<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>5\uff09  \u600e\u6837\u6307\u5b9a\u5934\u6587\u4ef6\u4e0e\u5e93\u6587\u4ef6\u8def\u5f84<\/h2>\n<blockquote>\n<blockquote>\n<p>include_directories\u4e0elink_directories<\/p>\n<p>\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u8def\u5f84<\/p>\n<p>link_directories\u4ec5\u5bf9\u5176\u540e\u9762\u7684targets\u8d77\u4f5c\u7528<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>6\uff09  \u600e\u6837\u533a\u5206debug\u3001release\u7248\u672c<\/h2>\n<blockquote>\n<blockquote>\n<p>\u5efa\u7acbdebug\/release\u4e24\u76ee\u5f55\uff0c\u5206\u522b\u5728\u5176\u4e2d\u6267\u884ccmake -DCMAKE_BUILD_TYPE=Debug\uff08\u6216Release\uff09\uff0c\u9700\u8981\u7f16\u8bd1\u4e0d\u540c\u7248\u672c\u65f6\u8fdb\u5165\u4e0d\u540c\u76ee\u5f55\u6267\u884cmake\u5373\u53ef\uff1b<\/p>\n<\/blockquote>\n<\/blockquote>\n<p>Debug\u7248\u4f1a\u4f7f\u7528\u53c2\u6570-g\uff1bRelease\u7248\u4f7f\u7528-O3 \u2013DNDEBUG<\/p>\n<blockquote>\n<blockquote>\n<p>\u53e6\u4e00\u79cd\u8bbe\u7f6e\u65b9\u6cd5\u2014\u2014\u4f8b\u5982DEBUG\u7248\u8bbe\u7f6e\u7f16\u8bd1\u53c2\u6570DDEBUG<\/p>\n<\/blockquote>\n<\/blockquote>\n<pre><code>IF(DEBUG_mode)\n\n    add_definitions(-DDEBUG)\n\nENDIF()<\/code><\/pre>\n<p>\u5728\u6267\u884ccmake\u65f6\u589e\u52a0\u53c2\u6570\u5373\u53ef\uff0c\u4f8b\u5982cmake -D DEBUG_mode=ON<\/p>\n<h2>7) \u600e\u6837\u8bbe\u7f6e\u6761\u4ef6\u7f16\u8bd1<\/h2>\n<p>\u4f8b\u5982debug\u7248\u8bbe\u7f6e\u7f16\u8bd1\u9009\u9879DEBUG\uff0c\u5e76\u4e14\u66f4\u6539\u4e0d\u5e94\u6539\u53d8CMakelist.txt<\/p>\n<blockquote>\n<blockquote>\n<p>\u4f7f\u7528option command\uff0ceg\uff1a<\/p>\n<\/blockquote>\n<\/blockquote>\n<pre><code>option(DEBUG_mode &quot;ON for debug or OFF for release&quot; ON)\n\nIF(DEBUG_mode)\n\n    add_definitions(-DDEBUG)\n\nENDIF()<\/code><\/pre>\n<blockquote>\n<blockquote>\n<p>\u4f7f\u5176\u751f\u6548\u7684\u65b9\u6cd5\uff1a\u9996\u5148cmake\u751f\u6210makefile\uff0c\u7136\u540emake edit_cache\u7f16\u8f91\u7f16\u8bd1\u9009\u9879\uff1bLinux\u4e0b\u4f1a\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u6846\uff0c\u53ef\u4ee5\u66f4\u6539\uff0c\u8be5\u5b8c\u540e\u518dmake\u751f\u6210\u76ee\u6807\u6587\u4ef6\u2014\u2014emacs\u4e0d\u652f\u6301make edit_cache\uff1b<\/p>\n<p>\u5c40\u9650\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u8bbe\u7f6e\u751f\u6210\u7684makefile\uff0c\u800c\u662f\u5fc5\u987b\u4f7f\u7528\u547d\u4ee4\u5728make\u524d\u8bbe\u7f6e\u53c2\u6570\uff1b\u5bf9\u4e8edebug\u3001release\u7248\u672c\uff0c\u76f8\u5f53\u4e8e\u9700\u8981\u4e24\u4e2a\u76ee\u5f55\uff0c\u5206\u522b\u5148cmake\u4e00\u6b21\uff0c\u7136\u540e\u5206\u522bmake edit_cache\u4e00\u6b21\uff1b<\/p>\n<p>\u671f\u671b\u7684\u6548\u679c\uff1a\u5728\u6267\u884ccmake\u65f6\u76f4\u63a5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u5f00\u5173\u9879\uff0c\u751f\u6210\u76f8\u5e94\u7684makefile\u2014\u2014\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u4f8b\u5982cmake \u2013DDEBUGVERSION=ON<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>8\uff09  \u600e\u6837\u6dfb\u52a0\u7f16\u8bd1\u5b8f\u5b9a\u4e49<\/h2>\n<blockquote>\n<blockquote>\n<p>\u4f7f\u7528add_definitions\u547d\u4ee4\uff0c\u89c1\u547d\u4ee4\u90e8\u5206\u8bf4\u660e<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>9\uff09  \u600e\u6837\u6dfb\u52a0\u7f16\u8bd1\u4f9d\u8d56\u9879<\/h2>\n<p>\u7528\u4e8e\u786e\u4fdd\u7f16\u8bd1\u76ee\u6807\u9879\u76ee\u524d\u4f9d\u8d56\u9879\u5fc5\u987b\u5148\u6784\u5efa\u597d<\/p>\n<blockquote>\n<blockquote>\n<p>add_dependencies<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>10\uff09        \u600e\u6837\u6307\u5b9a\u76ee\u6807\u6587\u4ef6\u76ee\u5f55<\/h2>\n<blockquote>\n<blockquote>\n<p>\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\uff0c\u5728\u8be5\u76ee\u5f55\u4e2d\u6267\u884ccmake\u751f\u6210Makefile\u6587\u4ef6\uff0c\u8fd9\u6837\u7f16\u8bd1\u7ed3\u679c\u4f1a\u4fdd\u5b58\u5728\u8be5\u76ee\u5f55\u2014\u2014\u7c7b\u4f3c<\/p>\n<pre><code>SET_TARGET_PROPERTIES(ss7gw PROPERTIES\nRUNTIME_OUTPUT_DIRECTORY &quot;${BIN_DIR}&quot;)<\/code><\/pre>\n<\/blockquote>\n<\/blockquote>\n<h2>11\uff09        \u5f88\u591a\u6587\u4ef6\u5939\uff0c\u96be\u9053\u9700\u8981\u628a\u6bcf\u4e2a\u6587\u4ef6\u5939\u7f16\u8bd1\u6210\u4e00\u4e2a\u5e93\u6587\u4ef6\uff1f<\/h2>\n<blockquote>\n<blockquote>\n<p>\u53ef\u4ee5\u4e0d\u5728\u5b50\u76ee\u5f55\u4e2d\u4f7f\u7528CMakeList.txt\uff0c\u76f4\u63a5\u5728\u4e0a\u5c42\u76ee\u5f55\u4e2d\u6307\u5b9a\u5b50\u76ee\u5f55<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>12\uff09        \u600e\u6837\u8bbe\u5b9a\u4f9d\u8d56\u7684cmake\u7248\u672c<\/h2>\n<blockquote>\n<blockquote>\n<p>cmake_minimum_required(VERSION 2.6)<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>13\uff09        \u76f8\u5bf9\u8def\u5f84\u600e\u4e48\u6307\u5b9a<\/h2>\n<blockquote>\n<blockquote>\n<p>${projectname_SOURCE_DIR}\u8868\u793a\u6839\u6e90\u6587\u4ef6\u76ee\u5f55\uff0c${ projectname _BINARY_DIR}\u8868\u793a\u6839\u4e8c\u8fdb\u5236\u6587\u4ef6\u76ee\u5f55\uff1f<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>14\uff09        \u600e\u6837\u8bbe\u7f6e\u7f16\u8bd1\u4e2d\u95f4\u6587\u4ef6\u7684\u76ee\u5f55<\/h2>\n<blockquote>\n<blockquote>\n<p>TBD<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>15\uff09        \u600e\u6837\u5728IF\u8bed\u53e5\u4e2d\u4f7f\u7528\u5b57\u4e32\u6216\u6570\u5b57\u6bd4\u8f83<\/h2>\n<blockquote>\n<blockquote>\n<p>\u6570\u5b57\u6bd4\u8f83LESS\u3001GREATER\u3001EQUAL\uff0c\u5b57\u4e32\u6bd4STRLESS\u3001STRGREATER\u3001STREQUAL\uff0c<\/p>\n<p>Eg\uff1a<\/p>\n<\/blockquote>\n<\/blockquote>\n<pre><code>\nset(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)\n\nset(AAA abc)\n\nIF(AAA STREQUAL abc)\n\n    message(STATUS &quot;true&quot;)   #\u5e94\u8be5\u6253\u5370true\n\nENDIF()<\/code><\/pre>\n<h2>16\uff09        \u66f4\u6539h\u6587\u4ef6\u65f6\u662f\u5426\u53ea\u7f16\u8bd1\u5fc5\u987b\u7684cpp\u6587\u4ef6<\/h2>\n<blockquote>\n<blockquote>\n<p>\u662f<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>17\uff09        \u673a\u5668\u4e0a\u5b89\u88c5\u4e86VC7\u548cVC8\uff0cCMAKE\u4f1a\u81ea\u52a8\u641c\u7d22\u7f16\u8bd1\u5668\uff0c\u4f46\u662f\u600e\u6837\u6307\u5b9a\u67d0\u4e2a\u7248\u672c\uff1f<\/h2>\n<blockquote>\n<blockquote>\n<p>TBD<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>18\uff09        \u600e\u6837\u6839\u636eOS\u6307\u5b9a\u7f16\u8bd1\u9009\u9879<\/h2>\n<blockquote>\n<blockquote>\n<p>IF( APPLE ); IF( UNIX ); IF( WIN32 )<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>19\uff09        \u80fd\u5426\u81ea\u52a8\u6267\u884c\u67d0\u4e9b\u7f16\u8bd1\u524d\u3001\u540e\u547d\u4ee4\uff1f<\/h2>\n<blockquote>\n<blockquote>\n<p>\u53ef\u4ee5\uff0cTBD<\/p>\n<\/blockquote>\n<\/blockquote>\n<h2>20\uff09        \u600e\u6837\u6253\u5370make\u7684\u8f93\u51fa<\/h2>\n<p>make VERBOSE=1<\/p>\n<h1>\u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/lidabo\/p\/7359422.html\">https:\/\/www.cnblogs.com\/lidabo\/p\/7359422.html<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/zjutzz\/p\/6815342.html\">https:\/\/www.cnblogs.com\/zjutzz\/p\/6815342.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6982\u8ff0 \u7528 CMakeLists.txt \u6765\u63cf\u8ff0\u6784\u5efa\u4f9d\u8d56\u5173\u7cfb\u548c\u6784\u5efa\u76ee\u6807\uff0c \u7528 cmake &lt;options&gt; &lt;folder&gt; \u6765\u751f\u6210 Makefile. \u8fd9\u4e2a &lt;folder&gt; \u5c31\u662fCMakeLists.txt \u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55. \u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u5165\u5b8f\u53d8\u91cf\u5b9a\u4e49\uff0c\u4f8b\u5982 cmake -DABSL_BUILD_TESTING=ON -DABSL_USE_GOOGLETEST_HEAD=ON -DCMAKE_CXX_STANDARD=11 .. cmake &#8211;build . &#8211;target all \u8fd9\u4e2a\u9879\u76ee https:\/\/github.com\/abseil\/abseil-cpp \u4e2d\u7684 cmake \u7528\u6cd5\u53ef\u4f5c\u53c2\u8003 2. \u5feb\u901f\u4e0a\u624b cmake_minimum_required(VERSION 3.0.0) project(webrtc_primer VERSION 0.1.0) include(CTest) enable_testing() add_executable(webrtc_primer main.cpp) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) include(CPack) main.cpp #include &lt;iostream&gt; int main(int, char**) { std::cout [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=334\" title=\"Permanent Link to: cmake \u7b80\u660e\u624b\u518c\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=334"}],"version-history":[{"count":8,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}