{"id":4849,"date":"2020-11-25T15:07:44","date_gmt":"2020-11-25T07:07:44","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4849"},"modified":"2020-11-25T15:07:44","modified_gmt":"2020-11-25T07:07:44","slug":"cmake%e4%b8%ad%e6%9e%84%e5%bb%ba%e9%9d%99%e6%80%81%e5%ba%93%e4%b8%8e%e5%8a%a8%e6%80%81%e5%ba%93%e5%8f%8a%e5%85%b6%e4%bd%bf%e7%94%a8%ef%bc%88cmakelists%e7%9a%84%e4%b8%80%e4%ba%9b%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4849","title":{"rendered":"CMake\u4e2d\u6784\u5efa\u9759\u6001\u5e93\u4e0e\u52a8\u6001\u5e93\u53ca\u5176\u4f7f\u7528\uff08CMakeLists\u7684\u4e00\u4e9b\u89e3\u91ca\uff09"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><a href=\"https:\/\/blog.csdn.net\/weixin_41245919\/article\/details\/82793643\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"htmledit_views\">\n<p>\u672c\u8282\u7684\u4efb\u52a1\uff1a<\/p>\n<p>\uff11\uff0c\u5efa\u7acb\u4e00\u4e2a\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\uff0c\u63d0\u4f9bHelloFunc\u51fd\u6570\u4f9b\u5176\u4ed6\u7a0b\u5e8f\u7f16\u7a0b\u4f7f\u7528\uff0cHelloFunc<\/p>\n<p>\u5411\u7ec8\u7aef\u8f93\u51faHello World\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\uff12\uff0c\u5b89\u88c5\u5934\u6587\u4ef6\u4e0e\u5171\u4eab\u5e93\u3002<\/p>\n<p>3\uff0c \u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u521b\u5efa\u7684\u5171\u4eab\u5e93\uff08\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>cd \/home\/ccj\/CMakeDemo<\/strong><\/p>\n<p><strong>mkdir t3<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>cd \/backup\/cmake\/t3<\/strong><\/p>\n<p><strong>mkdir lib<\/strong><\/p>\n<p><strong><img decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/31e0b46e9b628f74489ab9313cb22587.png\" alt=\"\" \/><\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5728t3\u76ee\u5f55\u4e0b\u5efa\u7acbCMakeLists.txt\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>PROJECT(HELLOLIB)<\/strong><\/p>\n<p><strong># \u901a\u8fc7\u5728\u4e3b\u5de5\u7a0b\u6587\u4ef6CMakeLists.txt\u4e2d\u4fee\u6539ADD_SUBDIRECTORY (lib) \u6307\u4ee4\u6765\u6307\u5b9a\u4e00\u4e2a\u7f16\u8bd1\u8f93\u51fa\u4f4d\u7f6e;<\/strong><\/p>\n<p><strong># \u6307\u5b9a\u672c\u5de5\u7a0b\u4e2d\u9759\u6001\u5e93libhello.so\u751f\u6210\u7684\u4f4d\u7f6e\uff0c\u5373 build\/lib;<\/strong><\/p>\n<p><strong>ADD_SUBDIRECTORY(lib)<\/strong><\/p>\n<p><strong># \u4e5f\u53ef\u4ee5\u901a\u8fc7\u53d8\u66f4\u4e3a\u5176\u4ed6\u7684\u4f4d\u7f6e\uff0c\u5982<\/strong><\/p>\n<p><strong># ADD_SUBDIRECTORY(lib lib_new)<\/strong><\/p>\n<p><strong># \u5219\uff0c\u9759\u6001\u5e93libhello.so\u751f\u6210\u7684\u4f4d\u7f6e\u53d8\u4e3a build\/lib_new;<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5728lib\u76ee\u5f55\u4e0b\u5efa\u7acb\u4e24\u4e2a\u6e90\u6587\u4ef6hello.cpp\u4e0e hello.h<\/strong><\/p>\n<p><strong>hello.cpp\u5185\u5bb9\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>#include &#8220;hello.h&#8221;<\/strong><\/p>\n<p><strong>using\u00a0namespace\u00a0std;<\/strong><\/p>\n<p><strong>void HelloFunc(){<\/strong><\/p>\n<p><strong>cout\u00a0&lt;&lt;\u00a0&#8220;Hello World\/n&#8221;;<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>hello.h\u5185\u5bb9\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>#ifndef HELLO_H<\/strong><\/p>\n<p><strong>#define HELLO_H<\/strong><\/p>\n<p><strong>#include &lt;stdio.h&gt;<\/strong><\/p>\n<p><strong>void HelloFunc();<\/strong><\/p>\n<p><strong>#endif<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5728lib\u76ee\u5f55\u4e0b\u5efa\u7acbCMakeLists.txt\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>\u6ce8\u610f\uff1a \u8fd9\u91cc\u6211\u4eec\u5c06Section 7\u4e4b\u524d\u7684CMakeLists.txt\u4e2d\u5e94\u6709\u7684\u5185\u5bb9\u63d0\u4f9b\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>SET\u00a0(LIBHELLO_SRC hello.cpp)<\/strong><\/p>\n<p><strong># SET (LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}\/lib)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong># three kinds of libraries:<\/strong><\/p>\n<p><strong># 1. shared, i.e., dynamic library\uff0c\u52a8\u6001\u5e93\u6587\u4ef6\u6269\u5c55\u540d\u5e38\u4e3a &#8220;*.so&#8221;\uff1b<\/strong><\/p>\n<p><strong># 2. static, i.e., static library\uff0c \u9759\u6001\u5e93\u6587\u4ef6\u6269\u5c55\u540d\u5e38\u4e3a &#8220;*.a&#8221;;<\/strong><\/p>\n<p><strong># \u800c\u4e14\uff0c\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\u6587\u4ef6\u540d\u4fdd\u6301\u4e00\u81f4\uff0c\u53ea\u662f\u6269\u5c55\u540d\u4e0d\u540c\uff1b<\/strong><\/p>\n<p><strong># 3. module, this parameter is valid only when dyld is supported;<\/strong><\/p>\n<p><strong># otherwise, will be considered as shared<\/strong><\/p>\n<p><strong># \u6dfb\u52a0\u52a8\u6001\u5e93\uff0c\u5173\u952e\u8bcd\u4e3ashared\uff0c\u4f60\u4e0d\u9700\u8981\u5199\u5168libhello.so\uff0c<\/strong><\/p>\n<p><strong># \u53ea\u9700\u8981\u586b\u5199hello\u5373\u53ef\uff0ccmake\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u4f60\u751f\u6210 libhello.X<\/strong><\/p>\n<p><strong>ADD_LIBRARY\u00a0(hello\u00a0SHARED\u00a0${LIBHELLO_SRC})<\/strong><\/p>\n<p><strong># \u6dfb\u52a0\u9759\u6001\u5e93\uff0c\u5173\u952e\u8bcd\u4e3astatic\uff0c<\/strong><\/p>\n<p><strong># ADD_LIBRARY (hello STATIC ${LIBHELLO_SRC})<\/strong><\/p>\n<p><strong># \u4ecd\u7136\u7528hello\u4f5c\u4e3atarget\u540d\u65f6\uff0c\u662f\u4e0d\u80fd\u6210\u529f\u521b\u5efa\u6240\u9700\u7684\u9759\u6001\u5e93\u7684\uff0c<\/strong><\/p>\n<p><strong># \u56e0\u4e3ahello\u4f5c\u4e3a\u4e00\u4e2atarget\u662f\u4e0d\u80fd\u91cd\u540d\u7684\uff0c \u6545\u628a\u4e0a\u9762\u7684hello\u4fee\u6539\u4e3ahello_static<\/strong><\/p>\n<p><strong># \u540c\u7406\uff0c\u4f60\u4e0d\u9700\u8981\u5199\u5168libhello_static.a<\/strong><\/p>\n<p><strong># \u53ea\u9700\u8981\u586b\u5199hello\u5373\u53ef\uff0ccmake\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u4f60\u751f\u6210 libhello_static.X<\/strong><\/p>\n<p><strong>ADD_LIBRARY\u00a0(hello_static\u00a0STATIC\u00a0${LIBHELLO_SRC})<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong># \u6309\u7167\u4e00\u822c\u7684\u4e60\u60ef\uff0c\u9759\u6001\u5e93\u540d\u5b57\u8ddf\u52a8\u6001\u5e93\u540d\u5b57\u5e94\u8be5\u662f\u4e00\u81f4\u7684\uff0c\u53ea\u662f\u6269\u5c55\u540d\u4e0d\u540c\uff1b<\/strong><\/p>\n<p><strong># \u5373\uff1a\u9759\u6001\u5e93\u540d\u4e3a libhello.a\uff1b \u52a8\u6001\u5e93\u540d\u4e3alibhello.so \uff1b<\/strong><\/p>\n<p><strong># \u6240\u4ee5\uff0c\u5e0c\u671b &#8220;hello_static&#8221; \u5728\u8f93\u51fa\u65f6\uff0c\u4e0d\u662f&#8221;hello_static&#8221;\uff0c\u800c\u662f\u4ee5&#8221;hello&#8221;\u7684\u540d\u5b57\u663e\u793a\uff0c\u6545\u8bbe\u7f6e\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES\u00a0(hello_static\u00a0PROPERTIES\u00a0OUTPUT_NAME &#8220;hello&#8221;)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>GET_TARGET_PROPERTY\u00a0(OUTPUT_VALUE hello_static OUTPUT_NAME)<\/strong><\/p>\n<p><strong>MESSAGE\u00a0(STATUS\u00a0&#8220;This is the hello_static OUTPUT_NAME: &#8221; ${OUTPUT_VALUE})<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong># cmake\u5728\u6784\u5efa\u4e00\u4e2a\u65b0\u7684target\u65f6\uff0c\u4f1a\u5c1d\u8bd5\u6e05\u7406\u6389\u5176\u4ed6\u4f7f\u7528\u8fd9\u4e2a\u540d\u5b57\u7684\u5e93\uff0c<\/strong><\/p>\n<p><strong># \u56e0\u6b64\uff0c\u5728\u6784\u5efalibhello.a\u65f6\uff0c\u5c31\u4f1a\u6e05\u7406\u6389libhello.so.<\/strong><\/p>\n<p><strong># \u4e3a\u4e86\u56de\u907f\u8fd9\u4e2a\u95ee\u9898\uff0c\u6bd4\u5982\u518d\u6b21\u4f7f\u7528SET_TARGET_PROPERTIES\u5b9a\u4e49 CLEAN_DIRECT_OUTPUT\u5c5e\u6027\u3002<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES\u00a0(hello_static\u00a0PROPERTIES\u00a0CLEAN_DIRECT_OUTPUT\u00a01)<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES\u00a0(hello\u00a0PROPERTIES\u00a0CLEAN_DIRECT_OUTPUT\u00a01)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong># \u6309\u7167\u89c4\u5219\uff0c\u52a8\u6001\u5e93\u662f\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u7248\u672c\u53f7\u7684\uff0c<\/strong><\/p>\n<p><strong># VERSION\u6307\u4ee3\u52a8\u6001\u5e93\u7248\u672c\uff0cSOVERSION\u6307\u4ee3API\u7248\u672c\u3002<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES\u00a0(hello\u00a0PROPERTIES\u00a0VERSION\u00a01.2 SOVERSION\u00a01)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong># \u6211\u4eec\u9700\u8981\u5c06libhello.a, libhello.so.x\u4ee5\u53cahello.h\u5b89\u88c5\u5230\u7cfb\u7edf\u76ee\u5f55\uff0c\u624d\u80fd\u771f\u6b63\u8ba9\u5176\u4ed6\u4eba\u5f00\u53d1\u4f7f\u7528\uff0c<\/strong><\/p>\n<p><strong># \u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u5c06hello\u7684\u5171\u4eab\u5e93\u5b89\u88c5\u5230&lt;prefix&gt;\/lib\u76ee\u5f55\uff1b<\/strong><\/p>\n<p><strong># \u5c06hello.h\u5b89\u88c5&lt;prefix&gt;\/include\/hello\u76ee\u5f55\u3002<\/strong><\/p>\n<p><strong>INSTALL\u00a0(TARGETS hello hello_static LIBRARY DESTINATION lib<\/strong><\/p>\n<p><strong>ARCHIVE DESTINATION lib)<\/strong><\/p>\n<p><strong>INSTALL\u00a0(FILES\u00a0hello.h DESTINATION\u00a0include\/hello)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>:<\/strong><\/p>\n<p><strong>\u4ecd\u7136\u91c7\u7528out-of-source\u7f16\u8bd1\u7684\u65b9\u5f0f\uff0c\u6309\u7167\u4e60\u60ef\uff0c\u6211\u4eec\u5efa\u7acb\u4e00\u4e2abuild\u76ee\u5f55\uff0c\u5728build<\/strong><\/p>\n<p><strong>\u76ee\u5f55\u4e2d<\/strong><\/p>\n<p><strong>cmake ..<\/strong><\/p>\n<p><strong>make<\/strong><\/p>\n<p><strong>\u8fd9\u65f6\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728lib\u76ee\u5f55\u5f97\u5230\u4e00\u4e2alibhello.so\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u671f\u671b\u7684\u5171\u4eab\u5e93\u3002<\/strong><\/p>\n<p><strong>\u5982\u679c\u4f60\u8981\u6307\u5b9alibhello.so\u751f\u6210\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u901a\u8fc7\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u5728\u4e3b\u5de5\u7a0b\u6587\u4ef6CMakeLists.txt\u4e2d\u4fee\u6539ADD_SUBDIRECTORY (lib) \u6307\u4ee4\u6765\u6307\u5b9a\u4e00\u4e2a\u7f16\u8bd1\u8f93\u51fa\u4f4d\u7f6e\uff1b<\/strong><\/li>\n<li><strong>\u6216\u8005\u5728 lib\/CMakeLists.txt\u4e2d\u6dfb\u52a0SET (LIBRARY_OUTPUT_PATH &lt;\u8def\u5f84&gt;) \u6765\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\u3002<\/strong><\/li>\n<\/ul>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u8fd9\u4e24\u8005\u7684\u533a\u522b\u6211\u4eec\u4e0a\u4e00\u8282\u5df2\u7ecf\u63d0\u5230\u4e86\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\uff0c\u4e0b\u9762\uff0c\u6211\u4eec\u89e3\u91ca\u4e00\u4e0b\u4e00\u4e2a\u65b0\u7684<\/strong><\/p>\n<p><strong>\u6307\u4ee4ADD_LIBRARY\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>ADD_LIBRARY(libname [SHARED|STATIC|MODULE]<\/strong><\/p>\n<p><strong>[EXCLUDE_FROM_ALL] source1 source2 &#8230; sourceN)<\/strong><\/p>\n<p><strong>\u4f60\u4e0d\u9700\u8981\u5199\u5168 libhello.so\uff0c\u53ea\u9700\u8981\u586b\u5199hello\u5373\u53ef\uff0ccmake\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e3a\u4f60\u751f\u6210 libhello.X<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u7c7b\u578b\u6709\u4e09\u79cd:<\/strong><\/p>\n<ul>\n<li><strong>SHARED\uff0c\u52a8\u6001\u5e93\uff1b<\/strong><\/li>\n<li><strong>STATIC\uff0c\u9759\u6001\u5e93<\/strong><\/li>\n<li><strong>MODULE\uff0c\u5728\u4f7f\u7528dyld\u7684\u7cfb\u7edf\u6709\u6548\uff0c\u5982\u679c\u4e0d\u652f\u6301dyld\uff0c\u5219\u88ab\u5f53\u4f5cSHARED\u5bf9\u5f85\u3002<\/strong><\/li>\n<\/ul>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>EXCLUDE_FROM_ALL\u53c2\u6570\u7684\u610f\u601d\u662f\u8fd9\u4e2a\u5e93\u4e0d\u4f1a\u88ab\u9ed8\u8ba4\u6784\u5efa\uff0c\u9664\u975e\u6709\u5176\u4ed6\u7684\u7ec4\u4ef6\u4f9d\u8d56\u6216\u8005\u624b\u52a8\u6784\u5efa\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u540c\u6837\u4f7f\u7528\u4e0a\u9762\u7684\u6307\u4ee4\uff0c\u6211\u4eec\u5728\u652f\u6301\u52a8\u6001\u5e93\u7684\u57fa\u7840\u4e0a\u518d\u4e3a\u5de5\u7a0b\u6dfb\u52a0\u4e00\u4e2a\u9759\u6001\u5e93\uff0c\u6309\u7167\u4e00\u822c\u7684\u4e60<\/strong><\/p>\n<p><strong>\u60ef\uff0c\u9759\u6001\u5e93\u540d\u5b57\u8ddf\u52a8\u6001\u5e93\u540d\u5b57\u5e94\u8be5\u662f\u4e00\u81f4\u7684\uff0c\u53ea\u4e0d\u8fc7\u540e\u7f00\u662f.a\u7f62\u4e86\u3002<\/strong><\/p>\n<p><strong>\u4e0b\u9762\u6211\u4eec\u7528\u8fd9\u4e2a\u6307\u4ee4\u518d\u6765\u6dfb\u52a0\u9759\u6001\u5e93\uff1a<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u7136\u540e\u518d\u5728build\u76ee\u5f55\u8fdb\u884c\u5916\u90e8\u7f16\u8bd1\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u9759\u6001\u5e93\u6839\u672c\u6ca1\u6709\u88ab\u6784\u5efa\uff0c\u4ecd\u7136\u53ea\u751f\u6210\u4e86<\/strong><\/p>\n<p><strong>\u4e00\u4e2a\u52a8\u6001\u5e93\u3002\u56e0\u4e3ahello\u4f5c\u4e3a\u4e00\u4e2aADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})<\/strong><\/p>\n<p><strong>\u5c31\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2alibhello_static.a\u7684\u9759\u6001\u5e93\u4e86\u3002<\/strong><\/p>\n<p><strong>\u8fd9\u79cd\u7ed3\u679c\u663e\u793a\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u6211\u4eec\u9700\u8981\u7684\u662f\u540d\u5b57\u76f8\u540c\u7684\u9759\u6001\u5e93\u548c\u52a8\u6001\u5e93\uff0c\u56e0\u4e3atarget\u540d<\/strong><\/p>\n<p><strong>\u79f0\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u80af\u5b9a\u4e0d\u80fd\u901a\u8fc7ADD_LIBRARY\u6307\u4ee4\u6765\u5b9e\u73b0\u4e86\u3002\u8fd9\u65f6\u5019\u6211\u4eec\u9700\u8981\u7528\u5230<\/strong><\/p>\n<p><strong>\u53e6\u5916\u4e00\u4e2a\u6307\u4ee4\uff1a<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES\uff0c<\/strong><\/p>\n<p><strong>\u5176\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES (target1 target2 &#8230;PROPERTIES prop1 value1 prop2 value2 &#8230;)<\/strong><\/p>\n<p><strong>\u8fd9\u6761\u6307\u4ee4\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u8f93\u51fa\u7684\u540d\u79f0\uff0c\u5bf9\u4e8e\u52a8\u6001\u5e93\uff0c\u8fd8\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u52a8\u6001\u5e93\u7248\u672c\u548cAPI\u7248\u672c\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4f5c\u7684\u662f\u5411lib\/CMakeLists.txt\u4e2d\u6dfb\u52a0\u4e00\u6761\uff1a<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME &#8220;hello&#8221;)<\/strong><\/p>\n<p><strong>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u540c\u65f6\u5f97\u5230libhello.so \u548c libhello.a\u4e24\u4e2a\u5e93\u4e86\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u4e0eSET_TARGET_PROPERTIES\u5bf9\u5e94\u7684\u6307\u4ee4\u662f\uff1a<\/strong><\/p>\n<p><strong>GET_TARGET_PROPERTY (VAR target property)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\u4f8b\uff0c\u6211\u4eec\u5411lib\/CMakeListst.txt\u4e2d\u6dfb\u52a0\uff1a<\/strong><\/p>\n<p><strong>GET_TARGET_PROPERTY (OUTPUT_VALUE hello_static OUTPUT_NAME)<\/strong><\/p>\n<p><strong>MESSAGE(STATUS &#8220;This is the hello_static OUTPUT_NAME: &#8220;${OUTPUT_VALUE})<\/strong><\/p>\n<p><strong>\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\u5b9a\u4e49\uff0c\u5219\u8fd4\u56deNOTFOUND.<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u8ba9\u6211\u4eec\u6765\u68c0\u67e5\u4e00\u4e0b\u6700\u7ec8\u7684\u6784\u5efa\u7ed3\u679c\uff0c\u6211\u4eec\u53d1\u73b0\uff0clibhello.a\u5df2\u7ecf\u6784\u5efa\u5b8c\u6210\uff0c\u4f4d\u4e8e<\/strong><\/p>\n<p><strong>build\/lib\u76ee\u5f55\u4e2d\uff0c\u4f46\u662flibhello.so\u53bb\u6d88\u5931\u4e86\u3002\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u662f\uff1acmake\u5728\u6784\u5efa\u4e00<\/strong><\/p>\n<p><strong>\u4e2a\u65b0\u7684target\u65f6\uff0c\u4f1a\u5c1d\u8bd5\u6e05\u7406\u6389\u5176\u4ed6\u4f7f\u7528\u8fd9\u4e2a\u540d\u5b57\u7684\u5e93\uff0c\u56e0\u4e3a\uff0c\u5728\u6784\u5efalibhello.a\u65f6\uff0c<\/strong><\/p>\n<p><strong>\u5c31\u4f1a\u6e05\u7406\u6389libhello.so. \u4e3a\u4e86\u56de\u907f\u8fd9\u4e2a\u95ee\u9898\uff0c\u6bd4\u5982\u518d\u6b21\u4f7f\u7528SET_TARGET_PROPERTIES\u5b9a\u4e49<\/strong><\/p>\n<p><strong>CLEAN_DIRECT_OUTPUT\u5c5e\u6027\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5411lib\/CMakeLists.txt\u4e2d\u6dfb\u52a0:<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES (hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES (hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)<\/strong><\/p>\n<p><strong>\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u518d\u6b21\u8fdb\u884c\u6784\u5efa\uff0c\u4f1a\u53d1\u73b0build\/lib\u76ee\u5f55\u4e2d\u540c\u65f6\u751f\u6210libhello.so\u548clibhello.a\u4e86\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6309\u7167\u89c4\u5219\uff0c\u52a8\u6001\u5e93\u662f\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u7248\u672c\u53f7\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b\u7cfb\u7edf\u7684\u52a8\u6001\u5e93\uff0c\u4e00\u822c\u60c5\u51b5\u662f<\/strong><\/p>\n<p><strong>libhello.so.1.2<\/strong><\/p>\n<p><strong>libhello.so -&gt;libhello.so.1<\/strong><\/p>\n<p><strong>libhello.so.1-&gt;libhello.so.1.2<\/strong><\/p>\n<p><strong>\u4e3a\u4e86\u5b9e\u73b0\u52a8\u6001\u5e93\u7248\u672c\u53f7\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4f7f\u7528SET_TARGET_PROPERTIES\u6307\u4ee4\u3002<\/strong><\/p>\n<p><strong>\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>SET_TARGET_PROPERTIES (hello PROPERTIES VERSION 1.2 SOVERSION 1)<\/strong><\/p>\n<p><strong>VERSION\u6307\u4ee3\u52a8\u6001\u5e93\u7248\u672c\uff0cSOVERSION\u6307\u4ee3API\u7248\u672c\u3002<\/strong><\/p>\n<p><strong>\u5c06\u4e0a\u8ff0\u6307\u4ee4\u52a0\u5165lib\/CMakeLists.txt\u4e2d\uff0c\u91cd\u65b0\u6784\u5efa\u770b\u770b\u7ed3\u679c\u3002<\/strong><\/p>\n<p><strong>\u5728build\/lib\u76ee\u5f55\u4f1a\u751f\u6210\uff1a<\/strong><\/p>\n<p><strong>libhello.so.1.2<\/strong><\/p>\n<p><strong>libhello.so.1-&gt;libhello.so.1.2<\/strong><\/p>\n<p><strong>libhello.so -&gt;libhello.so.1<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/5d8476443d58a199daab45d4c02bd22d.png\" alt=\"\" width=\"570\" height=\"77\" \/><\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u76ee\u5f55\uff0c\u5c06hello.h\u5b89\u88c5&lt;prefix&gt;\/include\/hello\u76ee\u5f55\u3002<\/strong><\/p>\n<p><strong>\u5229\u7528\u4e0a\u4e00\u8282\u4e86\u89e3\u5230\u7684INSTALL\u6307\u4ee4\uff0c\u6211\u4eec\u5411lib\/CMakeLists.txt\u4e2d\u6dfb\u52a0\u5982\u4e0b\u6307\u4ee4\uff1a<\/strong><\/p>\n<p><strong>INSTALL\u00a0(TARGETS hello hello_static LIBRARY DESTINATION lib<\/strong><\/p>\n<p><strong>ARCHIVE DESTINATION lib)<\/strong><\/p>\n<p><strong>INSTALL\u00a0(FILES\u00a0hello.h DESTINATION\u00a0include\/hello)<\/strong><\/p>\n<p><strong>\u6ce8\u610f\uff0c\u9759\u6001\u5e93\u8981\u4f7f\u7528ARCHIVE\u5173\u952e\u5b57<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u901a\u8fc7:<\/strong><\/p>\n<p><strong>cmake -DCMAKE_INSTALL_PREFIX=\/usr\/loal ..<\/strong><\/p>\n<p><strong>make<\/strong><\/p>\n<p><strong>sudo make install<\/strong><\/p>\n<p><strong>\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5934\u6587\u4ef6\u548c\u5171\u4eab\u5e93\u5b89\u88c5\u5230\u7cfb\u7edf\u76ee\u5f55\/usr\/local\/lib\u548c\/usr\/local\/include\/hello\u4e2d\u4e86\u3002<\/strong><\/p>\n<p><strong>\u5982\u4e0b\u56fe\uff1a<\/strong><\/p>\n<p><strong><img decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/24136cccb0b9bfa022d0fcd101cad830.png\" alt=\"\" \/><\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u524d\u51e0\u4e2asections\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86libhello\u9759\u3001\u52a8\u6001\u5e93\u7684\u6784\u5efa\u4ee5\u53ca\u5b89\u88c5\uff0c\u672csection\u4e2d\u6211\u4eec\u9700\u8981<\/strong><\/p>\n<p><strong>\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u4f7f\u7528\u6784\u5efa\u597d\u7684\u5171\u4eab\u5e93\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\uff09\u5728\u76ee\u5f55\u4e0b\u5efa\u7acb\u76ee\uff1a#include &#8220;hello.h&#8221;<\/strong><\/p>\n<p><strong>int main(){<\/strong><\/p>\n<p><strong>HelloFunc();<\/strong><\/p>\n<p><strong>return\u00a00;<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u7f16\u5199\u5de5\u7a0b\u4e3b\u6587\u4ef6CMakeLists.txt<\/strong><\/p>\n<p><strong>PROJECT(NEWHELLO)<\/strong><\/p>\n<p><strong>ADD_SUBDIRECTORY(src)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u7f16\u5199src\/CMakeLists.txt<\/strong><\/p>\n<p><strong>ADD_EXECUTABLE(main main.cpp)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\uff09\u5916\u90e8\u6784\u5efacmake ..<\/strong><\/p>\n<p><strong>make<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u6784\u5efa\u5931\u8d25\uff0c\u5982\u679c\u9700\u8981\u67e5\u770b\u7ec6\u8282\uff0c\u53ef\u4ee5\u4f7f\u7528 make VERBOSE=1\u6765\u6784\u5efa\u3002<\/strong><\/p>\n<p><strong>\u9519\u8bef\u7ed3\u679c\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\uff09\u5f15\u5165\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\u3002<\/strong><\/p>\n<p><strong>hello.h\u4f4d\u4e8e\/usr\/local\/include\/hello\u76ee\u5f55\u4e2d\uff0c\u5e76\u6ca1\u6709\u4f4d\u4e8e\uff0c\u540c\u5fd7\uff0c\u8981\u8fd9\u4e48\u5e72\uff0c\u6211\u8fd9<\/strong><\/p>\n<p><strong>\u4e00\u8282\u5c31\u6ca1\u4ec0\u4e48\u53ef\u5199\u4e86\uff0c\u53ea\u80fd\u9009\u62e9\u4e00\u4e2aglib\u6216\u8005libX11\u6765\u5199\u4e86\uff0c\u8fd9\u4e9b\u4ee3\u7801\u5199\u51fa\u6765\u5f88\u591a\u540c\u5fd7<\/strong><\/p>\n<p><strong>\u662f\u770b\u4e0d\u61c2\u7684)<\/strong><\/p>\n<p><strong>\u4e3a\u4e86\u8ba9\u6211\u4eec\u7684\u5de5\u7a0b\u80fd\u591f\u627e\u5230hello.h\u5934\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u65b0\u7684\u6307\u4ee4<\/strong><\/p>\n<p><strong>INCLUDE_DIRECTORIES\uff0c\u5176\u5b8c\u6574\u8bed\u6cd5\u4e3a\uff1a<\/strong><\/p>\n<p><strong>INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 &#8230;)<\/strong><\/p>\n<p><strong>\u8fd9\u6761\u6307\u4ee4\u53ef\u4ee5\u7528\u6765\u5411\u5de5\u7a0b\u6dfb\u52a0\u591a\u4e2a\u7279\u5b9a\u7684\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff0c\u8def\u5f84\u4e4b\u95f4\u7528\u73b0\u5728\u6211\u4eec\u5728src\/CMakeLists.txt\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84\uff0c\u65b9\u5f0f\u5f88\u7b80\u5355\uff0c\u52a0\u5165\uff1a<\/strong><\/p>\n<p><strong>INCLUDE_DIRECTORIES(\/usr\/local\/include\/hello)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u8fdb\u5165build\u76ee\u5f55\uff0c\u91cd\u65b0\u8fdb\u884c\u6784\u5efa\uff0c\u8fd9\u65f6\u627e\u4e0d\u5230hello.h\u7684\u9519\u8bef\u5df2\u7ecf\u6d88\u5931\uff0c\u4f46\u662f\u51fa\u73b0\u4e86\u4e00\u4e2a\u65b0\u7684\u9519\u8bef\uff1a<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>2<\/strong><\/p>\n<p><strong>main.cpp:(.text+0x5): undefined reference to `HelloFunc()&#8217;<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><img decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/ab34060e40703f7f0d37ad0e62ba21ee.png\" alt=\"\" \/><\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u56e0\u4e3a\u6211\u4eec\u5e76\u6ca1\u6709link\u5230\u5171\u4eab\u5e93libhello\u4e0a\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\uff09target\u8fd9\u4e2a\u6307\u4ee4\u53ef\u4ee5\u7528\u6765\u4e3atarget\u6dfb\u52a0\u9700\u8981\u94fe\u63a5\u7684\u5171\u4eab\u5e93\uff0c\u672c\u4f8b\u4e2d\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f46\u662f\u540c\u6837\u53ef\u4ee5\u7528\u4e8e\u4e3a\u81ea\u5df1\u7f16\u5199\u7684\u5171\u4eab\u5e93\u6dfb\u52a0\u5171\u4eab\u5e93\u94fe\u63a5\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u4e3a\u4e86\u89e3\u51b3\u6211\u4eec\u524d\u9762\u9047\u5230\u7684&#8221;HelloFunc()&#8221;\u672a\u5b9a\u4e49\u9519\u8bef\uff0c\u6211\u4eec\u9700\u8981\u4f5c\u7684\u662f\u5411<\/strong><\/p>\n<p><strong>src\/CMakeLists.txt\u4e2d\u6dfb\u52a0\u5982\u4e0b\u6307\u4ee4\uff1a<\/strong><\/p>\n<p><strong>\u4e5f\u53ef\u4ee5\u5199\u6210<\/strong><\/p>\n<p><strong>\u8fdb\u5165build\u76ee\u5f55\u91cd\u65b0\u8fdb\u884c\u6784\u5efa\u3002<\/strong><\/p>\n<p><strong>cmake ..<\/strong><\/p>\n<p><strong>make<\/strong><\/p>\n<p><strong>\u8fd9\u65f6\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u8fde\u63a5\u5230libhello\u7684\u53ef\u6267\u884c\u7a0b\u5e8fmain\uff0c\u4f4d\u4e8ebuild\/src\u76ee\u5f55\uff0c<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/43175884f51c606e1c521a3fc263f3dd.png\" alt=\"\" width=\"570\" height=\"118\" \/><\/strong><\/p>\n<p><strong>\u8fd0\u884cmain\u7684\u7ed3\u679c\u662f<\/strong><\/p>\n<p><strong><img decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/453f1e962c912c9604c66147bed9e2e3.png\" alt=\"\" \/><\/strong><\/p>\n<p><strong>\u8ba9\u6211\u4eec\u6765\u68c0\u67e5\u4e00\u4e0bmain\u7684\u94fe\u63a5\u60c5\u51b5\uff1a<\/strong><\/p>\n<p><strong>$ ldd src\/main<\/strong><\/p>\n<p><strong>\u867d\u7136\u4e86\u8fd9\u4e2a\u7ed3\u679c\uff0c\u4f46\u662f\u6ca1\u6709\u7406\u89e3\u660e\u767d\uff0c\u5230\u5e95\u94fe\u63a5\u4ec0\u4e48\u5e93<img loading=\"lazy\" decoding=\"async\" class=\"has\" src=\"http:\/\/www.th7.cn\/d\/file\/p\/2014\/10\/02\/d9b5c7a73aee6439fd93d937ddd5857b.png\" alt=\"\" width=\"570\" height=\"152\" \/><\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u53ef\u4ee5\u6e05\u695a\u7684\u770b\u5230main\u786e\u5b9e\u94fe\u63a5\u4e86\u5171\u4eab\u5e93libhello\uff0c\u800c\u4e14\u94fe\u63a5\u7684\u662f\u52a8\u6001\u5e93libhello.so.1<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>$ ldd src\/main<\/strong><\/p>\n<p><strong>\u867d\u7136\u4e86\u8fd9\u4e2a\u7ed3\u679c\uff0c\u4f46\u662f\u6ca1\u6709\u7406\u89e3\u660e\u767d\uff0c\u5230\u5e95\u94fe\u63a5\u4ec0\u4e48\u5e93<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u8bf4\u660e\uff0cmain\u786e\u5b9e\u94fe\u63a5\u5230\u4e86\u9759\u6001\u5e93libhello.a<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\uff09\u7279\u6b8a\u7684\u73af\u5883\u53d8\u91cf\uff1a\u548c\u52a1\u5fc5\u6ce8\u610fCMAKE_INCLUDE_PATH \u548c CMAKE_LIBRARY_PATH\u8fd9\u4e24\u4e2a\u662f\u73af\u5883\u53d8\u91cf\u6765\u8fdb\u884c\uff0c\u4f7f\u7528bash\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<p><strong>\u7136\u540e\u5728\u5934\u6587\u4ef6\u4e2d\u5c06INCLUDE_DIRECTORIES(\/usr\/local\/include\/hello)\u66ff\u6362\u4e3a\uff1a<\/strong><\/p>\n<p><strong>FIND_PATH(myHeader hello.h)<\/strong><\/p>\n<p><strong>IF(myHeader)<\/strong><\/p>\n<p><strong>INCLUDE_DIRECTORIES(${myHeader})<\/strong><\/p>\n<p><strong>ENDIF(myHeader)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u4e0a\u8ff0\u7684\u4e00\u4e9b\u6307\u4ee4\u6211\u4eec\u5728\u540e\u9762\u4f1a\u4ecb\u7ecd\u3002\u8fd9\u91cc\u7b80\u5355\u8bf4\u660e\u4e00\u4e0b\uff0cFIND_PATH\u7528\u6765\u5728\u6307\u5b9a\u8def\u5f84\u4e2d\u641c\u7d22\u6587\u4ef6\u540d\uff0c\u6bd4\u5982\uff1aFIND_PATH (myHeader NAMES hello.h PATHS \/usr\/local\/include \/usr\/local\/include\/hello)<\/strong><\/p>\n<p><strong>\u8fd9\u91cc\u6211\u4eec\u6ca1\u6709\u6307\u5b9a\u8def\u5f84\uff0c\u4f46\u662f\uff0ccmake\u4ecd\u7136\u53ef\u4ee5\u5e2e\u6211\u4eec\u627e\u5230hello.h\u5b58\u653e\u7684\u8def\u5f84\uff0c\u5c31\u662f\u56e0<\/strong><\/p>\n<p><strong>\u4e3a\u6211\u4eec\u8bbe\u7f6e\u4e86\u73af\u5883\u53d8\u91cfCMAKE_INCLUDE_PATH\u3002<\/strong><\/p>\n<p><strong>\u5982\u679c\u4f60\u4e0d\u4f7f\u7528FIND_PATH\uff0cCMAKE_INCLUDE_PATH\u53d8\u91cf\u7684\u8bbe\u7f6e\u662f\u4e0d\u4f1a\u8d77\u4f5c\u7528\u7684\uff0c\u4f60\u4e0d\u80fd\u6307<\/strong><\/p>\n<p><strong>\u671b\u5b83\u4f1a\u76f4\u63a5\u4e3a\u7f16\u8bd1\u5668\u547d\u4ee4\u6dfb\u52a0\u53c2\u6570-I&lt;CMAKE_INCLUDE_PATH&gt;\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u540c\u7406\uff0cCMAKE_LIBRARY_PATH\u53ef\u4ee5\u7528\u5728FIND_LIBRARY\u4e2d\u3002\u540c\u6837\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u76f4\u63a5\u4e3aFIND_\u6307\u4ee4\u6240\u4f7f\u7528\uff0c\u6240\u4ee5\u6240\u6709\u4f7f\u7528FIND_\u6307\u4ee4\u7684cmake\u6a21\u5757\u90fd\u4f1a\u53d7\u76ca\u3002<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u5230\u8fd9\u91cc\u4e3a\u6b62\uff0c\u60a8\u5e94\u8be5\u57fa\u672c\u53ef\u4ee5\u4f7f\u7528cmake\u5de5\u4f5c\u4e86\uff0c\u4f46\u662f\u8fd8\u6709\u5f88\u591a\u9ad8\u7ea7\u7684\u8bdd\u9898\u6ca1\u6709\u63a2\u8ba8\uff0c\u6bd4<\/strong><\/p>\n<p><strong>\u5982\u7f16\u8bd1\u6761\u4ef6\u68c0\u67e5\u3001\u7f16\u8bd1\u5668\u5b9a\u4e49\u3001\u5e73\u53f0\u5224\u65ad\u3001\u5982\u4f55\u8ddfpkgconfig\u914d\u5408\u4f7f\u7528\u7b49\u7b49\u3002<\/strong><\/p>\n<p><strong>\u5230\u8fd9\u91cc\uff0c\u6216\u8bb8\u4f60\u53ef\u4ee5\u7406\u89e3\u524d\u9762\u8bb2\u5230\u7684&#8221;cmake\u7684\u4f7f\u7528\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662f\u5b66\u4e60cmake\u8bed\u8a00\uff0c\u5e76\u7f16\u5199<\/strong><\/p>\n<p><strong>cmake\u7a0b\u5e8f\u7684\u8fc7\u7a0b&#8221;\uff0c\u65e2\u7136\u662f&#8221;cmake\u8bed\u8a00&#8221;\uff0c\u81ea\u7136\u6d89\u53ca\u5230\u53d8\u91cf\u3001\u8bed\u6cd5\u7b49. \u4e0b\u4e00\u8282\uff0c\u6211\u4eec\u5c06\u629b\u5f00\u7a0b\u5e8f\u7684\u8bdd\u9898\uff0c\u770b\u770b\u5e38\u7528\u7684CMAKE\u53d8\u91cf\u4ee5\u53ca\u4e00\u4e9b\u57fa\u672c\u7684\u63a7\u5236\u8bed\u6cd5\u89c4\u5219\u3002<\/strong><\/p>\n<p>***************************************************************************************************************************************<\/p>\n<p>\u6240\u4ee5\u8fd9\u91cc\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<pre class=\"has\"><code class=\"hljs apache\"><span class=\"hljs-attribute\">add_executable<\/span>(\u53ef\u6267\u884c\u6587\u4ef6,src<span class=\"hljs-number\">1<\/span>,src<span class=\"hljs-number\">2<\/span>,src<span class=\"hljs-number\">3<\/span>...);<\/code><\/pre>\n<ul>\n<li>1<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u4e00\u5b9a\u8981\u628a\u6784\u6210\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6240\u6709\u6e90\u6587\u4ef6\u90fd\u6dfb\u52a0\u8fdb\u53bb\uff0c\u5426\u5219\u5c31\u4f1a\u72af\u9519\uff01\uff01\uff01<\/p>\n<p>\u5343\u91cc\u4e4b\u884c\uff0c\u59cb\u4e8e\u8db3\u4e0b\uff01<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>*********************************************************************************************************************************************<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0binclude_directories\uff0clink_directories\uff0clink_libraries\u548ctarget_link_libraries\u7684\u4f5c\u7528\u3002\u5c24\u5176\u662f\u540e\u9762\u4e09\u4e2a\u53c2\u6570\uff0c\u6bd4\u8f83\u76f8\u4f3c\uff0c\u5bb9\u6613\u5f04\u6df7\u3002<\/p>\n<p><strong>INCLUDE_DIRECTORIES\uff08\u6dfb\u52a0\u5934\u6587\u4ef6\u76ee\u5f55\uff09<\/strong><br \/>\n\u5b83\u76f8\u5f53\u4e8eg++\u9009\u9879\u4e2d\u7684-I\u53c2\u6570\u7684\u4f5c\u7528\uff0c\u4e5f\u76f8\u5f53\u4e8e\u73af\u5883\u53d8\u91cf\u4e2d\u589e\u52a0\u8def\u5f84\u5230CPLUS_INCLUDE_PATH\u53d8\u91cf\u7684\u4f5c\u7528\uff08\u8fd9\u91cc\u7279\u6307c++\u3002c\u548cJava\u4e2d\u7528\u6cd5\u7c7b\u4f3c\uff09\u3002<\/p>\n<p>\u6bd4\u5982\uff1a<br \/>\ninclude_directories(&#8220;\/opt\/MATLAB\/R2012a\/extern\/include&#8221;)<\/p>\n<p>export CPLUS_INCLUDE_PATH=CPLUS_INCLUDE_PATH:$MATLAB\/extern\/include<\/p>\n<p><strong>LINK_DIRECTORIES\uff08\u6dfb\u52a0\u9700\u8981\u94fe\u63a5\u7684\u5e93\u6587\u4ef6\u76ee\u5f55\uff09<\/strong><br \/>\n\u8bed\u6cd5\uff1a<br \/>\nlink_directories(directory1 directory2 &#8230;)<\/p>\n<p>\u5b83\u76f8\u5f53\u4e8eg++\u547d\u4ee4\u7684-L\u9009\u9879\u7684\u4f5c\u7528\uff0c\u4e5f\u76f8\u5f53\u4e8e\u73af\u5883\u53d8\u91cf\u4e2d\u589e\u52a0LD_LIBRARY_PATH\u7684\u8def\u5f84\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u6bd4\u5982\uff1a<br \/>\nLINK_DIRECTORIES(&#8220;\/opt\/MATLAB\/R2012a\/bin\/glnxa64&#8221;)<\/p>\n<p>export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MATLAB\/bin\/glnxa64<\/p>\n<p><strong>LINK_LIBRARIES\u3000\uff08\u6dfb\u52a0\u9700\u8981\u94fe\u63a5\u7684\u5e93\u6587\u4ef6\u8def\u5f84\uff0c\u6ce8\u610f\u8fd9\u91cc\u662f\u5168\u8def\u5f84\uff09<\/strong><br \/>\nList of direct link dependencies.<\/p>\n<p>\u6bd4\u5982\uff1a<br \/>\nLINK_LIBRARIES(&#8220;\/opt\/MATLAB\/R2012a\/bin\/glnxa64\/libeng.so&#8221;)<br \/>\nLINK_LIBRARIES(&#8220;\/opt\/MATLAB\/R2012a\/bin\/glnxa64\/libmx.so&#8221;)<\/p>\n<p>\u4e5f\u53ef\u4ee5\u5199\u6210\uff1a<br \/>\nLINK_LIBRARIES(&#8220;\/opt\/MATLAB\/R2012a\/bin\/glnxa64\/libeng.so&#8221;\u3000&#8221;\/opt\/MATLAB\/R2012a\/bin\/glnxa64\/libmx.so&#8221;)<\/p>\n<p><strong>TARGET_LINK_LIBRARIES \uff08\u8bbe\u7f6e\u8981\u94fe\u63a5\u7684\u5e93\u6587\u4ef6\u7684\u540d\u79f0\uff09<\/strong><br \/>\n\u8bed\u6cd5\uff1aTARGET_LINK_LIBRARIES(targetlibrary1 &lt;debug | optimized&gt; library2 ..)<\/p>\n<p>\u6bd4\u5982\uff08\u4ee5\u4e0b\u5199\u6cd5\uff08\u5305\u62ec\u5907\u6ce8\u4e2d\u7684\uff09\u90fd\u53ef\u4ee5\uff09\uff1a<br \/>\nTARGET_LINK_LIBRARIES(myProject hello)\uff0c\u8fde\u63a5libhello.so\u5e93<br \/>\nTARGET_LINK_LIBRARIES(myProject libhello.a)<br \/>\nTARGET_LINK_LIBRARIES(myProject libhello.so)<\/p>\n<p>\u518d\u5982\uff1a<br \/>\nTARGET_LINK_LIBRARIES(myProject\u00a0libeng.so)\u3000\u3000#\u8fd9\u4e9b\u5e93\u540d\u5199\u6cd5\u90fd\u53ef\u4ee5\u3002<br \/>\nTARGET_LINK_LIBRARIES(myProject\u00a0eng)<br \/>\nTARGET_LINK_LIBRARIES(myProject\u00a0-leng)<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u672c\u8282\u7684\u4efb\u52a1\uff1a \uff11\uff0c\u5efa\u7acb\u4e00\u4e2a\u9759\u6001<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4849","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4849"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4849\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}