{"id":2305,"date":"2017-04-01T09:23:05","date_gmt":"2017-04-01T01:23:05","guid":{"rendered":"https:\/\/www.csslayer.info\/wordpress\/?p=2305"},"modified":"2017-04-01T09:23:05","modified_gmt":"2017-04-01T01:23:05","slug":"cmake-find-package","status":"publish","type":"post","link":"https:\/\/www.csslayer.info\/wordpress\/cmake\/cmake-find-package\/","title":{"rendered":"\u7528 CMake target \u6765\u7ba1\u7406\u4f9d\u8d56\u5e93"},"content":{"rendered":"<p>\u867d\u7136\u6709\u8d8a\u6765\u8d8a\u591a\u7684\u5305\u63d0\u4f9b CMake config \u4e86\uff0c\u65f6\u5e38\u8fd8\u662f\u9700\u8981\u624b\u5199\u4e00\u4e2a\u3002\u4f46\u662f\u5982\u679c\u4f60\u8fd8\u6d3b\u5728 cmake 2.8 \u7684\u65f6\u4ee3\uff0c \u4f60\u53ef\u80fd\u6d6a\u8d39\u4e86\u5f88\u591a\u7684\u7cbe\u529b\u5728\u4e0d\u5fc5\u8981\u7684\u5730\u65b9\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u8fd8\u5728\u5199 include_directories\uff0clink_directories \u6765\u7ed9\u4f60\u7684\u4f9d\u8d56\u6dfb\u52a0\u627e\u5934\u6587\u4ef6\u548c\u5e93\u7684\u76ee\u5f55\uff0c\u90a3\u4e48\u4f60\u5927\u6982\u5df2\u7ecf OUT \u4e86\u3002<\/p>\n<p>\u6bd4\u8f83\u73b0\u4ee3\u7684\u4e00\u79cd\u65b9\u5f0f\u662f\uff0c\u7528 Target \u6765\u7ba1\u7406\u4f9d\u8d56\u7684\u5e93\u7684\u4e00\u5207\u3002\u6700\u65e9\u6211\u662f\u4ece Qt5 \u7684 cmake \u90a3\u91cc\u5b66\u5230\u7684\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u901a\u8fc7 add_library([Library] [TYPE] IMPORTED]) \u6765\u628a\u4f60\u4f9d\u8d56\u7684\u5e93\u53d8\u6210\u4e00\u4e2a cmake target\u3002\u540c\u65f6\u8fd9\u4e2a target \u5c31\u81ea\u5e26\u4e86 include_directories \u7684\u5c5e\u6027\u548c library \u81ea\u8eab\u8def\u5f84\u3002<\/p>\n<p>\u90a3\u4e48\u53ef\u4ee5\u6765\u770b\u770b\u4e00\u4e2a\u6211\u5199\u7684\u65b0 libintl \u7684\u4f8b\u5b50\uff1a<a href=\"https:\/\/github.com\/fcitx\/fcitx5\/blob\/master\/cmake\/FindLibIntl.cmake\">https:\/\/github.com\/fcitx\/fcitx5\/blob\/master\/cmake\/FindLibIntl.cmake<\/a><\/p>\n<p>\u7cbe\u9ad3\u5c31\u662f\u5728\u6700\u540e\u8fd9\u91cc\uff1a<\/p>\n<pre>if(LIBINTL_FOUND AND NOT TARGET LibIntl::LibIntl)\r\n if (LIBINTL_LIBRARY)\r\n add_library(LibIntl::LibIntl UNKNOWN IMPORTED)\r\n set_target_properties(LibIntl::LibIntl PROPERTIES\r\n IMPORTED_LOCATION \"${LIBINTL_LIBRARY}\")\r\n else()\r\n add_library(LibIntl::LibIntl INTERFACE IMPORTED )\r\n endif()\r\n set_target_properties(LibIntl::LibIntl PROPERTIES\r\n INTERFACE_INCLUDE_DIRECTORIES \"${LIBINTL_INCLUDE_DIR}\"\r\n )\r\nendif()<\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u5728\u4f60\u7684\u9879\u76ee\u4e0a target_link_libraries([YourTarget] LibIntl::LibIntl)\uff0c\u5b83\u5c31\u4f1a\u81ea\u52a8\u5728\u7f16\u8bd1\u548c\u94fe\u63a5\u65f6\u627e\u5230\u5e93\u548c\u5934\u6587\u4ef6\uff0c\u518d\u4e5f\u4e0d\u7528\u5199 include_directories(&#8230;) \u4e86\u3002<\/p>\n<p>\u4e0a\u9762\u8fd8\u6709\u7528\u5230\u4e86\u4e00\u4e2a<\/p>\n<pre> add_library(LibIntl::LibIntl INTERFACE IMPORTED )<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u6765\u8868\u793a\u8fd9\u4e2a library \u662f\u7eaf\u5934\u6587\u4ef6\u7684\u5e93\u3002\u5bf9\u4e8e\u5f88\u591a\u6a21\u677f\u5e93\uff0c\u6216\u8005 libc \u5185\u7f6e\u7684\u60c5\u51b5\u6765\u8bf4\u662f\u5f88\u6709\u7528\u7684\u3002<\/p>\n<p>\u5bf9\u4e8e\u591a pc \u6587\u4ef6\u7684\u5e93\uff0c\u4f8b\u5982 cairo \u6765\u8bf4\uff0c\u628a\u5b83\u7528 package + component \u7684\u65b9\u5f0f\u6765\u8868\u793a\u6709\u65f6\u4f1a\u5f88\u65b9\u4fbf\u3002\u8fd9\u91cc\u53ef\u4ee5\u8bf7\u51fa KDE \u5199\u7684 extra-cmake-modules\u3002\u91cc\u9762\u81ea\u5e26\u7684 FindXCB.cmake \u53ef\u4ee5\u4f5c\u53c2\u8003\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u6211\u5199\u7684 Cairo \u7684\u5b9e\u4f8b\uff1a<a href=\"https:\/\/github.com\/fcitx\/fcitx5\/blob\/master\/cmake\/FindCairo.cmake\">https:\/\/github.com\/fcitx\/fcitx5\/blob\/master\/cmake\/FindCairo.cmake<\/a><\/p>\n<pre>find_package(Cairo COMPONENTS Cairo XCB EGL)<\/pre>\n<p>\u4f7f\u7528\u65f6\u53ef\u4ee5\u8fd9\u6837\uff0c\u662f\u4e0d\u662f\u770b\u8d77\u6765\u5c31\u5f88\u9ad8\u7aef\u5927\u6c14\u4e0a\u6863\u6b21\u3002\u5b9e\u9645 link \u65f6\u5c31\u53ea\u8981 target_link_libraries([Target] Cairo::XCB) \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u8fd9\u6837\u5f15\u5165\u7684 Tagert \u4e5f\u5e76\u4e0d\u53ea\u6709\u8fd9\u4e00\u4e9b\u5c5e\u6027\uff0c\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u4f9d\u8d56\u65f6\u9700\u8981\u4f7f\u7528\u7684 C++ \u6807\u51c6\u4e4b\u7c7b\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4f60\u60f3\u8981\u5728\u7f16\u8bd1\u65f6\u652f\u6301 third_party \u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u5e93\uff0c\u6216\u8005\u4f7f\u7528 bundle \u7684 source\uff0c\u8fd9\u4e2a\u65b9\u5f0f\u5c31\u53ef\u4ee5\u5728\u4e0d\u6539\u53d8\u4f60\u81ea\u5df1 target \u7684 CMakeLists.txt \u7684\u524d\u63d0\u4e0b\uff0c\u66f4\u52a0\u65b9\u4fbf\u7684\u81ea\u52a8\u53d8\u6362\u3002<\/p>\n<p>\u5e0c\u671b\u6709\u66f4\u591a\u7684\u5e93\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u76f4\u63a5\u63d0\u4f9b Find[Package].cmake \u5462\u3002<\/p>\n<p>\u53e6\u5916\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u91c7\u7528 cmake \u81ea\u52a8 export \u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\u8fd9\u6837\u7684\u6548\u679c\u3002\u6682\u4e14\u6316\u4e00\u4e2a\u5751\uff0c\u4e0b\u4e00\u6b21\u518d\u6765\u8bf4\u4e00\u4e0b install(EXPORT &#8230;) \u7684\u6545\u4e8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u867d\u7136\u6709\u8d8a\u6765\u8d8a\u591a\u7684\u5305\u63d0\u4f9b CMake config \u4e86\uff0c\u65f6\u5e38\u8fd8\u662f\u9700\u8981\u624b\u5199\u4e00\u4e2a\u3002\u4f46\u662f\u5982\u679c\u4f60\u8fd8\u6d3b\u5728 cmake 2.8 \u7684\u65f6\u4ee3\uff0c \u4f60\u53ef\u80fd\u6d6a\u8d39\u4e86\u5f88\u591a\u7684\u7cbe\u529b\u5728\u4e0d\u5fc5\u8981\u7684\u5730\u65b9\u3002 \u7b80\u5355\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u8fd8\u5728\u5199 include_directories\uff0clink_directories \u6765\u7ed9\u4f60\u7684\u4f9d\u8d56\u6dfb\u52a0\u627e\u5934\u6587\u4ef6\u548c\u5e93\u7684\u76ee\u5f55\uff0c\u90a3\u4e48\u4f60\u5927\u6982\u5df2\u7ecf OUT \u4e86\u3002 \u6bd4\u8f83\u73b0\u4ee3\u7684\u4e00\u79cd\u65b9\u5f0f\u662f\uff0c\u7528 Target \u6765\u7ba1\u7406\u4f9d\u8d56\u7684\u5e93\u7684\u4e00\u5207\u3002\u6700\u65e9\u6211\u662f\u4ece Qt5 \u7684 cmake \u90a3\u91cc\u5b66\u5230\u7684\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u901a\u8fc7 add_library([Library] [TYPE] IMPORTED]) \u6765\u628a\u4f60\u4f9d\u8d56\u7684\u5e93\u53d8\u6210\u4e00\u4e2a cmake target\u3002\u540c\u65f6\u8fd9\u4e2a target \u5c31\u81ea\u5e26\u4e86 include_directories \u7684\u5c5e\u6027\u548c library \u81ea\u8eab\u8def\u5f84\u3002 \u90a3\u4e48\u53ef\u4ee5\u6765\u770b\u770b\u4e00\u4e2a\u6211\u5199\u7684\u65b0 libintl \u7684\u4f8b\u5b50\uff1ahttps:\/\/github.com\/fcitx\/fcitx5\/blob\/master\/cmake\/FindLibIntl.cmake \u7cbe\u9ad3\u5c31\u662f\u5728\u6700\u540e\u8fd9\u91cc\uff1a if(LIBINTL_FOUND AND NOT TARGET &hellip; <a href=\"https:\/\/www.csslayer.info\/wordpress\/cmake\/cmake-find-package\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[217],"class_list":["post-2305","post","type-post","status-publish","format-standard","hentry","category-cmake","tag-cmake"],"_links":{"self":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2305"}],"version-history":[{"count":1,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2305\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/posts\/2305\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csslayer.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}