{"id":7993,"date":"2024-06-27T22:01:01","date_gmt":"2024-06-27T14:01:01","guid":{"rendered":""},"modified":"2024-06-27T22:01:01","modified_gmt":"2024-06-27T14:01:01","slug":"\u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7993.html","title":{"rendered":"\u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n \u5b98\u7f51\u67e5\u9605<\/p>\n \u5f00\u80c3\u83dc\u4f8b\u5b50<\/p>\n CMakeLists\u751f\u6210\u548c\u6dfb\u52a0\u4f9d\u8d56\u5e93<\/p>\n CMakeLists\u66f4\u591a\u5c0f\u4f8b\u5b50<\/p>\n \u751f\u6210.so\u5171\u4eab\u5e93\u6587\u4ef6<\/p>\n \u8c03\u7528.so\u5171\u4eab\u5e93\u6587\u4ef6<\/p>\n \u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684 CMakeList<\/p>\n \u751f\u6210\u4e00\u4e2a.so\u52a8\u6001\u5e93\u7684 CMakeList <\/p>\n add_library\uff08\u751f\u6210\u5e93\uff09\uff0ctarget_link_libraries\uff08\u751f\u6210\u76ee\u6807\u8fde\u63a5\u7684\u5e93\uff09\uff0cset_target_properties<\/p>\n CMAKE \u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|-g\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/p>\n \u5305\u542b\u6587\u4ef6\u7684\u7684\u76ee\u5f55include_directories<\/p>\n \u4f18\u5316\u9879|\u4f18\u5316\u7b49\u7ea7<\/p>\n Cmake\u8bbe\u7f6e\u4f18\u5316\u7b49\u7ea7| cmake \u751f\u6210 debug\u548c release \u7248<\/p>\n \u8bbe\u7f6e\u9ed8\u8ba4\u6784\u5efa\u7c7b\u578b<\/p>\n CMake\u8bbe\u7f6e\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/p>\n \u5982\u4f55\u5728cmakelists\u4e2d\u52a0\u5165-ldl\u7f16\u8bd1\u9009\u9879<\/p>\n CMake\u6307\u5b9agcc,g++\u7248\u672c\u7f16\u8bd1<\/p>\n CMake \u5173\u95ed\u8b66\u544a\u7684\u65b9\u6cd5<\/p>\n \u5173\u95ed\u7f16\u8bd1\u5668\u4f18\u5316<\/p>\n CMakeLists \u5b9e\u73b0\u52a8\u6001\u5b8f\u5f00\u5173<\/p>\n \u53bb\u6389\u7f16\u8bd1\u4f18\u5316<\/p>\n CMake--List\u7528\u6cd5<\/p>\n CmakeLists.txt\u5355\u884c\u6ce8\u91ca\u548c\u591a\u884c\u6ce8\u91ca <\/p>\n CMakeList \u901a\u914d\u7b26<\/p>\n \u5176\u4ed6\u672a\u5f52\u7c7b<\/p>\n add_custom_target \u81ea\u5b9a\u4e49\u547d\u4ee4<\/p>\n add_dependencies <\/p>\n Cmake\u6761\u4ef6\u5224\u65ad\u6307\u4ee4|if \u5224\u65ad\u4f18\u5148\u7ea7<\/p>\n \u8bbe\u7f6e\u7f16\u8bd1\u65f6\u548c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53bb\u54ea\u4e2a\u76ee\u5f55\u627e\u52a8\u6001\u5e93<\/p>\n #\u6307\u5b9a\u8fd0\u884c\u65f6\u52a8\u6001\u5e93\u7684\u52a0\u8f7d\u8def\u5f84<\/p>\n #\u6307\u5b9a\u94fe\u63a5\u65f6\u52a8\u6001\u5e93\u7684\u8def\u5f84<\/p>\n cmake install \u548c\u6253\u5305\u8bbe\u5b9a<\/p>\n CMakeLists.txt\u5355\u884c\u6ce8\u91ca\u548c\u591a\u884c\u6ce8\u91ca<\/p>\n target_link_libraries \u4e2d\u7684PRIVATE, PUBLIC, INTERFACE \u533a\u522b<\/p>\n \u4f5c\u8005\uff1abandaoyu\uff0c\u6301\u7eed\u66f4\u65b0\uff0c\u94fe\u63a5:\u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173_cmake\u6dfb\u52a0\u5934\u6587\u4ef6\u76ee\u5f55_bandaoyu\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n CMake 3.22.0-rc1\u6587\u6863\uff1ahttps:\/\/cmake.org\/cmake\/help\/latest\/search.html?q=add_library<\/p>\n cmake-commands(7) \u2014 CMake 3.23.5 Documentation<\/p>\n CMake Cookbook\uff1a\u300aCMake\u83dc\u8c31\uff08CMake Cookbook\u4e2d\u6587\u7248\uff09\u300b \uff1ahttps:\/\/www.bookstack.cn\/read\/CMake-Cookbook\/content-preface-preface-chinese.md<\/p>\n<\/p>\n \u539f\u6587;cmake\u4e4b\u751f\u6210\u52a8\u6001\u5e93\uff1ahttps:\/\/www.cnblogs.com\/pandamohist\/p\/13408455.html<\/p>\n 1\u3001\u76ee\u5f55\u7ed3\u6784<\/strong><\/p>\n 2\u3001CMakeLists.txt<\/strong><\/p>\n 3\u3001configure and generate<\/strong><\/p>\n <\/p>\n 4\u3001\u5176\u4ed6\u8bbe\u7f6e<\/strong><\/p>\n \u82e5\u9700\u8981\u6307\u5b9a\u8f93\u51fa\u8def\u5f84\uff0c\u5c1d\u8bd5\u4e0b\u9762\u7684\u793a\u4f8b\u6307\u4ee4\uff1a<\/p>\n \u4e0b\u9762\u662f\u6211\u7684\u51e0\u4e2a\u6587\u4ef6:<\/p>\n 1hello.cpp<\/p>\n 2hello.hpp<\/p>\n 3 main.cpp<\/p>\n 4 CMakeLists.txt<\/p>\n \u4e0a\u9762CmakeLists.txt\u91cc\u9762\uff0c \u5171\u4eab\u5e93\u7684\u540d\u79f0\u662fcalculate_shared\uff0c\u8fd9\u4e2a\u662f\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u66f4\u6539\u7684\u3002\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u662fmain, \u8fd9\u4e2a\u540d\u79f0\u4e5f\u662f\u53ef\u4ee5\u66f4\u6539\u7684\u3002<\/p>\n \u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0chello.cpp\u91cc\u9762\u4e0d\u7528\u5728\u5305\u542bhello.hpp \u4e86\u3002(\u6c57\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5bfc\u81f4\u51fa\u9519\uff0c\u63d0\u793a\u8bf4\u662f\u91cd\u590d\u5b9a\u4e49\u51fd\u6570\u4e86)\uff1b<\/p>\n \u7f16\u8bd1\u751f\u6210:<\/p>\n mkdir build CMakeCache.txt cmake_install.cmake main libcalculate_shared.so\u5c31\u662f\u751f\u6210\u7684\u5171\u4eab\u5e93\u6587\u4ef6\u3002<\/p>\n \u4ed6\u4eec\u7684\u8def\u5f84\u662f:\/home\/fan\/dev\/cmake\/4-exer\/<\/p>\n \u4e0b\u9762\u6709build\u6587\u4ef6\u5939\uff0c\u4ee5\u53camain.cpp, hello.cpp, hello.hpp, <\/p>\n build\u6587\u4ef6\u5939\u4e0b\u9762\u6709\u5171\u4eab\u5e93 libcalculate_shared.so.so<\/p>\n<\/p>\n \u6240\u6709\u7684\u5916\u90e8\u4f9d\u8d56\u5e93\u90fd\u662f\u8fd9\u6837\u7684\uff0c\u6bd4\u5982opencv ,openni, eigen\u7b49\u7b49\uff0c\u539f\u7406\u662f\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u4eec\u5df2\u7ecf\u5b89\u88c5\u5728\u7cfb\u7edf\u91cc\u9762\u4e86\uff0c\u53ef\u4ee5\u67e5\u627e\uff0c\u800c\u8fd9\u4e2a\u5219\u662f\u9700\u8981\u6211\u4eec\u81ea\u5df1\u53bb\u914d\u7f6e\u3002<\/p>\n \u5373\u6211\u4e0a\u9762\u751f\u6210\u7684\u5171\u4eab\u5e93\u6587\u4ef6\u672c\u8d28\u4e0a\u548copencv\u7684\u5e93\u662f\u76f8\u540c\u7684\u3002\u53ea\u4e0d\u8fc7\u8fd9\u4e2a\u5171\u4eab\u5e93\u9700\u8981\u81ea\u5df1\u624b\u52a8\u914d\u7f6e\u3002<\/p>\n \u6bd4\u5982\u6211\u53c8\u65b0\u5efa\u4e86\u4e00\u4e2a\u5de5\u7a0b\uff0c\u9700\u8981\u8c03\u7528\u4e0a\u9762\u7684\u5171\u4eab\u5e93 libcalculate_shared.so\u3002<\/p>\n main.cpp\u5982\u4e0b:<\/p>\n \u90a3\u4e48\u5728CMakeLists.txt\u91cc\u9762\uff0c\u6211\u9700\u8981\u544a\u8bc9CMake, \u8fd9\u4e2a\u5934\u6587\u4ef6\u53ef\u4ee5\u5728\u54ea\u91cc\u627e\u5230\uff0c\u5934\u6587\u4ef6\u6240\u5b9a\u4e49\u7684\u51fd\u6570\u53c8\u53ef\u4ee5\u5728\u54ea\u91cc\u627e\u5230\u3002<\/p>\n \u4e0a\u9762hello.hpp\u7684\u8def\u5f84\u662f:\/home\/fan\/dev\/cmake\/4-exer\/hello.hpp<\/p>\n libcalculate_shared.so\u7684\u8def\u5f84\u662f\/home\/fan\/dev\/cmake\/4-exer\/build\/libcalculate_shared.so<\/p>\n \u5219CMakeLists.txt\u5982\u4e0b:<\/p>\n \u8fd9\u91cc\u8981\u6ce8\u610f\u4e00\u4e9b\u7ec6\u8282\uff08\u5bf9\u4e8e\u6211\u8fd9\u4e2a\u6e23\u6e23\u6765\u8bf4\u7684\uff09<\/p>\n 1\u3001${ }\u8fd9\u79cd\u5f62\u5f0f\u4ee3\u8868\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u4e0a\u9762\u7684\uff0cHELLO_INCLUE \uff0c\u5c31\u662f\u6211\u81ea\u5df1\u5b9a\u4e49\u7684\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n 2\u3001\u5934\u6587\u4ef6\u5305\u542b\u5230\u5934\u6587\u4ef6\u6240\u5728\u7684\u6587\u4ef6\u5939\uff0c\u5373 \/home\/fan\/dev\/cmake\/4-exer\/<\/p>\n 3\u3001\u5171\u4eab\u5e93\u8981\u6307\u660e\u5177\u4f53\u7684\u5171\u4eab\u5e93 \uff0c\u7cbe\u786e\u5230.so<\/p>\n \u5176\u5b9e\u4e3b\u8981\u7684\u5c31\u662f\u6307\u660e\u8fd9\u4e2a\u8c03\u7528\u8fd9\u4e2a\u5171\u4eab\u5e93\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7684\u5934\u6587\u4ef6\uff0c\u4ee5\u53ca\u5171\u4eab\u5e93\u672c\u8eab\u6240\u5728\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u5305\u542b\u94fe\u63a5\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n \u5b89\u88c5\u8fc7\u7684\u5171\u4eab\u5e93\uff08\u4f8b\u5982opencv\uff09\u5c31\u4e0d\u7528\u8fd9\u4e48\u9ebb\u70e6\u4e86\uff0c\u56e0\u4e3a\u5b83\u7684\u5730\u5740\u90fd\u653e\u5728\u4e86\u53d8\u91cf\u91cc\u9762\u3002<\/p>\n Opencv\u7684\u4f9d\u8d56\u6dfb\u52a0 \u5b83\u7684CMakeLists.txt\u5982\u4e0b:<\/p>\n find_package(OpenCV REQUIRED)<\/p>\n include_directories(${OPENCV_INCLUDE_DIRS})<\/p>\n target_link_libraries(MAIN ${OpenCV_LIBS})<\/p>\n \u53ea\u9700\u8981\u67e5\u627e\u5c31\u53ef\u4ee5\u4e86\uff0cOpenCV_LIBS \u548c OPENCV_INCLUDE_DIRS \u90fd\u662f\u7cfb\u7edf\u5e2e\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\uff0c\u6240\u4ee5\u6bd4\u8f83\u5bb9\u6613<\/p>\n \u53c2\u8003\u535a\u5ba2:<\/p>\n 1\u3001\u5982\u4f55\u5199\u81ea\u5df1\u7684CmakeLists.txt https:\/\/www.cnblogs.com\/chaofn\/p\/10160555.html<\/p>\n 2\u3001 \u3010OpenCV\u3011\u4f7f\u7528CMake\u94fe\u63a5\u81ea\u5df1\u8def\u5f84\u4e0b\u9762\u7684OpenCV\u5e93 https:\/\/blog.csdn.net\/twt520ly\/article\/details\/<\/p>\n \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/_\/article\/details\/<\/p>\n \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/bandaoyu\/article\/details\/<\/p>\n grep -nR \"common\" .\/ --include=*.txt|grep -vE \"src_bak|boost|erasure-code|doc|link.txt\"<\/p>\n \u751f\u6210\u9759\u6001\u5e93\uff1a<\/strong><\/p>\n add_library(libsugan ${SRC_LISTS}) #\u7528${SRC_LISTS}\u751f\u6210\u9759\u6001\u5e93libsugan<\/p>\n \u6216<\/p>\n ADD_LIBRARY(static_lib STATIC ${DIR_SUB_SRCS})<\/p>\n \u751f\u6210\u52a8\u6001\u5e93\uff08\u52a0SHARED \uff09\uff1a<\/strong> target_link_libraries(libsugan #\u751f\u6210\u9759\u6001\u5e93libsugan\u8fd8\u9700\u94fe\u63a5\u4f9d\u8d56\u5e93${OpenCV_LIBS}\u2026 #\u4e0a\u9762\u7684\u914d\u7f6e\u751f\u6210\u540d\u5b57\u4e3alibsugan\u7684\u9759\u6001\u5e93\uff0c\u4f46Linux\u4e0b\u5bf9\u5e93\u7684\u5b58\u50a8\u683c\u5f0f\u662flib+name.a\uff0c\u6240\u4ee5\u5e93libsugan\u5b58\u50a8\u51fa\u6765\u7684\u7ed3\u679c\u5c31\u662fliblibsugan.a\uff0c\u770b\u7740\u5f88\u522b\u626d\u3002\u7528\u4e0b\u9762\u8fd9\u53e5\uff0c\u4fdd\u8bc1\u4e86\u5b58\u50a8\u51fa\u6765\u7684\u9759\u6001\u5e93\u53eb\u505alibsugan.a\uff1a<\/p>\n set_target_properties(libsugan PROPERTIES OUTPUT_NAME \"sugan\")<\/p>\n<\/p>\n #\u4f46\u662f\u8bf7\u5343\u4e07\u6ce8\u610f\uff0c\u5728\u6574\u4e2aCmakeLists.txt\u91cc<\/p>\n #\u5982\u679c\u60f3\u94fe\u63a5\u751f\u6210\u7684\u8fd9\u4e2a\u5e93\u5fc5\u987b\u4f7f\u7528 \u201cadd_library(libsugan ${SRC_LISTS})\u201d\u6307\u660e\u7684\u540d\u5b57\u3002 add_executable(demo .\/src\/main.cpp) \u8fde\u63a5\u5e93\uff1a<\/strong><\/p>\n target_link_libraries(demo libsugan)<\/p>\n target_link_libraries(app libsort.a) #\u751f\u6210app \u94fe\u5165 libsort.a\u9759\u6001\u5e93<\/p>\n TARGET_LINK_LIBRARIES(app libsort.a)<\/p>\n \u539f\u4f8b\u5b50\uff1a<\/p>\n \u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173<\/p>\n \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/michaelhan3\/article\/details\/<\/p>\n<\/p>\n add_definitions \u548cadd_compile_options\uff0c\u4e8c\u8005\u6dfb\u52a0\u7684\u7f16\u8bd1\u9009\u9879\u662f\u9488\u5bf9\u6240\u6709\u7f16\u8bd1\u5668\u7684(\u5305\u62ecc\u548cc++\u7f16\u8bd1\u5668)\u3002<\/p>\n add_definitions \u548cadd_compile_options\u7684\u533a\u522b\u662f\uff1a<\/p>\n add_definitions \u53ef\u7528\u4e8e\u6dfb\u52a0\u4efb\u4f55\u6807\u5fd7\uff0c\u4f46\u65e8\u5728\u6dfb\u52a0\u9884\u5904\u7406\u5668\u5b9a\u4e49\u3002<\/p>\n \u6b64\u547d\u4ee4\u5df2\u88ab\u66ff\u4ee3\u65b9\u6848\u53d6\u4ee3\uff1a add_definitions\uff1ahttps:\/\/cmake.org\/cmake\/help\/latest\/command\/add_definitions.html<\/p>\n<\/p>\n \u6dfb\u52a0 -g\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/strong><\/p>\n<\/p>\n \u65b9\u6cd5\u4e00\uff1aadd_definitions(\"-g\")\/ add_compile_options<\/strong><\/p>\n \u5728\u6587\u4ef6 CMakeLists.txt\u6dfb\u52a0\u4e0b\u9762\u4e00\u6761\u8bed\u53e5 \u6dfb\u52a0\u5176\u4ed6\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/p>\n \u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n #\u5224\u65ad\u7f16\u8bd1\u5668\u7c7b\u578b,\u5982\u679c\u662fgcc\u7f16\u8bd1\u5668,\u5219\u5728\u7f16\u8bd1\u9009\u9879\u4e2d\u52a0\u5165c++11\u652f\u6301<\/p>\n<\/p>\n if(CMAKE_COMPILER_IS_GNUCXX) \u4f7f\u7528add_compile_options\u6dfb\u52a0-std=c++11\u9009\u9879\uff0c\u662f\u60f3\u5728\u7f16\u8bd1c++\u4ee3\u7801\u65f6\u52a0\u4e0ac++11\u652f\u6301\u9009\u9879\u3002\u4f46\u662f\u56e0\u4e3aadd_compile_options\u662f\u9488\u5bf9\u6240\u6709\u7c7b\u578b\u7f16\u8bd1\u5668\u7684\uff0c\u6240\u4ee5\u5728\u7f16\u8bd1c\u4ee3\u7801\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u5982\u4e0bwarning<\/p>\n \u867d\u7136\u5e76\u4e0d\u5f71\u54cd\u7f16\u8bd1\uff0c\u4f46\u770b\u7740\u7684\u786e\u662f\u4e0d\u723d\u554a\uff0c\u8981\u6d88\u9664\u8fd9\u4e2awarning,\u5c31\u4e0d\u80fd\u4f7f\u7528add_compile_options\uff0c\u800c\u662f\u53ea\u9488\u5bf9c++\u7f16\u8bd1\u5668\u6dfb\u52a0\u8fd9\u4e2aoption\u3002<\/p>\n \u65b9\u6cd5\u4e8c\uff1aset<\/strong><\/p>\n \u6240\u4ee5\u5982\u4e0b\u4fee\u6539\u4ee3\u7801\uff0c\u5219\u8b66\u544a\u6d88\u9664\u3002<\/p>\n #\u5224\u65ad\u7f16\u8bd1\u5668\u7c7b\u578b,\u5982\u679c\u662fgcc\u7f16\u8bd1\u5668,\u5219\u5728\u7f16\u8bd1\u9009\u9879\u4e2d\u52a0\u5165c++11\u652f\u6301 \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/qinglongzhan\/article\/details\/<\/p>\n<\/p>\n include_directories(${cppzmq_INCLUDE_DIR}) \/\/\u6dfb\u52a0\u5305\u542b\u6587\u4ef6\u7684\u7684\u76ee\u5f55<\/p>\n add_definitions \u53ef\u7528\u4e8e\u6dfb\u52a0\u4efb\u4f55\u6807\u5fd7\uff0c\u4f46\u65e8\u5728\u6dfb\u52a0\u9884\u5904\u7406\u5668\u5b9a\u4e49\u3002<\/p>\n \u6b64\u547d\u4ee4\u5df2\u88ab\u66ff\u4ee3\u65b9\u6848\u53d6\u4ee3\uff1a \u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173<\/p>\n -O0\u7981\u6b62\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002\u9ed8\u8ba4\u4e3a\u6b64\u9879\u3002 -O2\u66f4\u591a\u7684\u4f18\u5316\uff0c\u4f1a\u5c1d\u8bd5\u51e0\u4e4e\u5168\u90e8\u7684\u4f18\u5316\u529f\u80fd\uff0c\u4f46\u4e0d\u4f1a\u8fdb\u884c\u201c\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n -O3\u5728 -O2 \u7684\u57fa\u7840\u4e0a\u518d\u6253\u5f00\u4e00\u4e9b\u4f18\u5316\u9009\u9879\uff1a-finline-functions\uff0c -funswitch-loops \u548c -fgcse-after-reload \u3002<\/p>\n -Os\u5bf9\u751f\u6210\u6587\u4ef6\u5927\u5c0f\u8fdb\u884c\u4f18\u5316\u3002\u5b83\u4f1a\u6253\u5f00 -O2 \u5f00\u7684\u9664\u4e86\u4f1a\u90a3\u4e9b\u589e\u52a0\u6587\u4ef6\u5927\u5c0f\u7684\u5168\u90e8\u9009\u9879\u3002<\/p>\n<\/p>\n \u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u67e5\u627e\u5de5\u7a0b\u4e2d\u8bbe\u7f6e\u4f18\u5316\u9879\u7684\u5730\u65b9\uff1a<\/p>\n grep -nR \"\\-O\" .\/ --include=*.txt<\/p>\n \u8fc7\u6ee4\u6389\u542b\u6709 build\u3001boost\u3001erasure-code\u3001doc\u3001link.txt\u5b57\u6837\u7684\u7ed3\u679c<\/p>\n grep -nR \"\\-O\" .\/ --include=*.txt|grep -vE \"build|src_bak|boost|erasure-code|doc|link.txt\"<\/p>\n<\/p>\n \u89c1\u6587\u7ae0\uff1a<\/p>\n \u3010gcc\u3011gcc\u4f18\u5316\u7b49\u7ea7 -O1 -O2 -O3 -Os -Ofast -Og|gcc\u5173\u95ed\u4f18\u5316-CSDN\u535a\u5ba2<\/p>\n<\/p>\n \u800cset\u547d\u4ee4\u8bbe\u7f6eCMAKE_C_FLAGS\u6216CMAKE_CXX_FLAGS\u53d8\u91cf\u5219\u662f\u5206\u522b\u53ea\u9488\u5bf9c\u548cc++\u7f16\u8bd1\u5668\u7684<\/p>\n<\/blockquote>\n \u5bf9c\u7f16\u8bd1\u5668\u7684<\/strong><\/p>\n set(CMAKE_C_FLAGS\"-O3 -fopenmp -fPIC -Wno-deprecated -Wenum-compare -std=c++14\")<\/p>\n<\/blockquote>\n \u9488\u5bf9c++\u7f16\u8bd1\u5668\u7684<\/strong><\/p>\n set(CMAKE_CXX_FLAGS \"-O3 -fopenmp -fPIC -Wno-deprecated -Wenum-compare -std=c++14\")<\/p>\n<\/blockquote>\n cmakelists.txt\u4e2d\uff0c\u5728\u589e\u52a0\u53ef\u6267\u884c\u7a0b\u5e8f\u540e\u589e\u52a0TARGET_LINK_LIBRARIES set(CMAKE_C_FLAGS \"-ldl\")<\/p>\n \u5728add_executable(${PROJECT_NAME} \"main.cpp\")\u540e\u9762\u6dfb\u52a0 \u7cfb\u7edf\u9ed8\u8ba4\u7684gcc\/g++\u5728\/usr\/bin\u76ee\u5f55\u4e0b\u3002<\/p>\n \u6211\u4eec\u5347\u7ea7\u5b89\u88c5\u7684gcc\u76ee\u5f55\u5728\/usr\/local\/bin\u76ee\u5f55\u4e0b\uff0c\u73b0\u5728\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u5347\u7ea7\u540e\u7684gcc\u3002<\/p>\n \u901a\u8fc7\u767e\u5ea6\u641c\u7d22\u51fa\u6765\u7684\u7ed3\u679c\uff0c\u5927\u591a\u662f\u5982\u4e0b\u64cd\u4f5c:<\/p>\n \u5728CMakeLists.txt\u4e2d\u8c03\u7528\u7f16\u8bd1\u5668\u4e4b\u524d\u6dfb\u52a0\uff1a<\/p>\n SET(CMAKE_C_COMPILER \"\/usr\/local\/bin\/gcc\") \u7136\u800c\u7ecf\u8fc7\u672c\u4eba\u4eb2\u81ea\u5b9e\u8df5\uff0c\u8be5\u65b9\u6cd5\u4e0d\u8d77\u4f5c\u7528\uff08\u6ca1\u6709\u5220\u6389build\u91cc\u9762\u7684\u4e1c\u897f\u91cd\u65b0cmake\u7684\u539f\u56e0\uff1f\uff09\uff0c\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff1a<\/p>\n \u6267\u884ccmake\u547d\u4ee4\u4e4b\u524d\uff0c\u5728shell\u7ec8\u7aef\u5148\u8bbe\u7f6e\u5982\u4e0b\u4e24\u4e2a\u53d8\u91cf\uff1a<\/p>\n export CC=\/usr\/local\/bin\/gcc \u53ef\u80fd\u8fd8\u9700\u8981<\/p>\n export LD_LIBRARY_PATH=\/usr\/local\/gcc-xxx\/lib:$LD_LIBRARY_PATH<\/p>\n \/usr\/local\/gcc-xxx\/lib \u662f\u4f60\u7684\u65b0gcc\u7684lib\u4f4d\u7f6e<\/p>\n<\/p>\n<\/blockquote>\n \u7136\u540e\u518d\u6267\u884ccmake\u7b49\u540e\u7eed\u547d\u4ee4\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u7528\u6307\u5b9a\u7684\u7f16\u8bd1\u5668\u7248\u672c\u4e86\u3002<\/p>\n vim ~\/.bashrc source ~\/.bashrc<\/p>\n \u5728CMakeLists.txt\u4e2d\u6dfb\u52a0add_definitions(-w)<\/p>\n \u5e94\u7528\u4e8e\u5355\u4e2atarget<\/strong><\/p>\n \u5e94\u7528\u4e8e\u6240\u6709target<\/strong><\/p>\n \u6ce8\u610f\uff1a\u4e3aGCC\u6216\/ WX\u6dfb\u52a0-Werror\u4ee5\u4fbfMSVC\u5c06\u6240\u6709\u8b66\u544a\u89c6\u4e3a\u9519\u8bef\u3002\u8fd9\u4f1a\u5c06\u6240\u6709\u8b66\u544a\u89c6\u4e3a\u9519\u8bef\u3002\u8fd9\u5bf9\u4e8e\u65b0\u9879\u76ee\u6765\u8bf4\u53ef\u4ee5\u65b9\u4fbf\u5730\u6267\u884c\u4e25\u683c\u7684\u8b66\u544a\u3002<\/p>\n \u53e6\u5916\uff0c -Wall \u5e76\u4e0d\u610f\u5473\u7740\"\u6240\u6709\u9519\u8bef\";\u4ece\u5386\u53f2\u610f\u4e49\u4e0a\u8bb2\uff0c\u5b83\u610f\u5473\u7740\"\u6bcf\u4e2a\u4eba\u90fd\u53ef\u4ee5\u8fbe\u6210\u4e00\u81f4\u7684\u6240\u6709\u9519\u8bef\"\"\u3002\u4ece -Wall -Wextra \u5f00\u59cb\uff0c\u7136\u540e\u4ed4\u7ec6\u9605\u8bfb\u60a8\u7684<\/em>\u7248\u672c\u7684GCC\u624b\u518c\uff0c\u5e76\u627e\u5230 else <\/em>\u7f16\u8bd1\u5668\u53ef\u4ee5\u4e3a\u60a8\u63d0\u4f9b\u5173\u4e8e\u8b66\u544a\u7684\u4fe1\u606f\u3002<\/p>\n CMake\u548c\u7f16\u8bd1\u5668\u8b66\u544a https:\/\/www.it1352.com\/784160.html<\/p>\n<\/p>\n \uff08\u672a\u9a8c\u8bc1\uff09<\/p>\n 1\uff09add_compile_options(-fno-elide-constructors) #\u5173\u95ed\u7f16\u8bd1\u5668\u4f18\u5316<\/p>\n 2\uff09set(CMAKE_CXX_FLAGS \"-fno-elide-constructors ${CMAKE_CXX_FLAGS}\")<\/p>\n<\/p>\n \u5728CMakeList\u4e2d\u6dfb\u52a0\uff1a<\/p>\n \u6267\u884c\u7684\u65f6\u5019<\/p>\n cmake -DCMAKE_BUILD_TYPE=Release<\/p>\n \u4e5f\u53ef\u4ee5\u5728\u4e0a\u4e00\u5c42\uff08\u8c03\u7528\u672cCMakeList.txt\u7684\uff09\u7684CMakeList.txt\u4e2d\u6dfb\u52a0\u4e0b\u9762\uff1a<\/p>\n option (CMAKE_BUILD_TYPE \"Use tutorial provided math implementation\" ON<\/strong>)<\/p>\n \u8868\u793a\u542f\u7528CMAKE_BUILD_TYPE \u5b8f\u3002<\/p>\n option (CMAKE_BUILD_TYPE \"Use tutorial provided math implementation\" OFF<\/strong>) #\u8868\u793a\u5173<\/p>\n \u53c2\u8003\uff1a<\/p>\n \u300ac++ - Optimize in CMake by default \u300b:https:\/\/stackoverflow.com\/questions\/\/optimize-in-cmake-by-default<\/p>\n \u300aHow to compile without optimizations -O0 using CMake\u300b\uff1ahttps:\/\/unix.stackexchange.com\/questions\/\/how-to-compile-without-optimizations-o0-using-cmake<\/p>\n<\/p>\n \u4f8b\u5b50<\/p>\n \u6700\u8fd1\u5728\u5de5\u4f5c\u4e2d\u9700\u8981\u901a\u8fc7\u4e00\u4efdC\u4ee3\u7801\u63a7\u5236\u903b\u8f91\u8d70\u5411\uff0c\u7f51\u4e0a\u627e\u4e86\u4e00\u4e0b\u8d44\u6599\uff0c\u53d1\u73b0\u53ef\u4ee5\u901a\u8fc7\u5728CMakeLists\u6587\u4ef6\u4e2d\u52a8\u6001\u5b9a\u4e49\u5b8f\u5f00\u5173\uff0c\u4ece\u800c\u80fd\u591f\u8fbe\u5230\u7f16\u8bd1\u51fa\u4e0d\u540c\u903b\u8f91\u6d41\u7684\u4ee3\u7801\u3002<\/p>\n \u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n \u9996\u5148\uff0c\u6211\u5728src\u4ee3\u7801\u91cc\u7f16\u5199\u4e86\u82e5\u5e72debug\u7684\u8f93\u51fa\uff1a<\/p>\n #IFDEF DEBUG IF (CMAKE_BUILD_TYPE STREQUAL DEBUG) $ cmake .. -DCMAKE_BUILD_TYPE=DEBUG $ cmake .. \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/_\/article\/details\/<\/p>\n CMake--List\u7528\u6cd5 \uff1ahttps:\/\/www.cnblogs.com\/narjaja\/p\/8343765.html<\/p>\n<\/p>\n \u5355\u884c\u6ce8\u91ca\uff1a\u4f7f\u7528\u201c#\u201d <\/p>\n $<TARGET_OBJECTS:A>\uff1a<\/strong><\/p>\n \u8bf4\u660e\uff1a<\/strong><\/p>\n add_executable(test test.cc $<TARGET_OBJECTS:A>)\uff0c\u8868\u793a\u4f7f\u7528 test.cc\u548c\u6784\u5efaA\u4ea7\u751f\u7684\u5bf9\u8c61 \u8054\u5408\u7f16\u8bd1\u51fatest\u3002<\/p>\n add_executable(test2 $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B> )\uff0c\u8868\u793a\u4f7f\u7528\u6784\u5efaA\u4ea7\u751f\u7684\u5bf9\u8c61 \u548c \u6784\u5efaB\u4ea7\u751f\u7684\u5bf9\u8c61 \u8054\u5408\u7f16\u8bd1\u51fatest\u3002<\/p>\n \u4f8b\u5b50\uff1a\u67d0\u4e2a\u5de5\u7a0b\u5982\u4e0b<\/p>\n \u4f7f\u7528 CMake \u4f7f\u7528\u547d\u4ee4\u7f16\u8bd1 test.cc \u4f7f\u7528\u7684A.o \u7684\u7f16\u8bd1\u4e3a \u6765\u81ea\uff1ahttps:\/\/stackoverflow.com\/questions\/\/cmake-wildcard-for-target-objects<\/p>\n \n \u7b2c5\u7ae0 \u914d\u7f6e\u65f6\u548c\u6784\u5efa\u65f6\u7684\u64cd\u4f5c - 5.4 \u6784\u5efa\u65f6\u8fd0\u884c\u81ea\u5b9a\u4e49\u547d\u4ee4:\u2161. \u4f7f\u7528add_custom_target - \u300aCMake\u83dc\u8c31\uff08CMake Cookbook\u4e2d\u6587\u7248\uff09\u300b - \u4e66\u6808\u7f51 \u00b7 BookStack<\/p>\n \u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u547d\u4ee4\uff1afinish ,\u6267\u884c\u8be5\u547d\u4ee4\u5c31\u4f1a\u8fdb\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n \u5355\u72ec\u6267\u884c\u547d\u4ee4\u7684\u65b9\u5f0f\u662f\uff1a cmake --build <dir> [<options>] [-- <build-tool-options>]<\/p>\n cmake --build \/home\/mydir --target finish<\/p>\n <\/p>\n \u4e0e add_executable(main main.cpp) \u3001add_library(mylib mylib.cpp) \u7684\u4e0d\u540c\u662f\uff0c\u8fd9\u4e24\u4e2a\u4f1a\u751f\u6210\uff08\u4ea7\u51fa\uff09main\u3001mylib\u6587\u4ef6\u3002add_custom_target(comandname \u2026\u2026\uff09\u53ea\u4f1a\u6267\u884c\u5217\u51fa\u7684\u64cd\u4f5c\uff0c\u4e0d\u4f1a\u4ea7\u751fcomandname\u6587\u4ef6<\/p>\n<\/p>\n \u5982\u679cmain \u4f9d\u8d56a.so b.so TARGET_LINK_LIBRARIES(main a.so b.so c.so d.so) <\/p>\n \u800ca.so b.so \u7684\u751f\u6210\u665a\u4e8emain\uff08\u5373\u7f16\u8bd1\u811a\u672c\u7684\u987a\u5e8f\u628aa.so b.so \u5b89\u6392\u5728\u540e\u9762\u7f16\u8bd1\uff09\uff0c\u5219\u9700\u8981ADD_DEPENDENCIES(main a.so b.so) \u63d0\u524d\u4e3amain\u7f16\u8bd1a.so b.so\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u62a5\u9519\uff1a\u7b26\u53f7\u7684\u5b9a\u4e49\u627e\u4e0d\u5230 \uff08\u8fd9\u4e9b\u7b26\u53f7\u6070\u6070\u5c31\u5728a.so \u548c b.so\u4e2d\uff09<\/p>\n \u8be6\u60c5\u89c1\uff1acmake\u4e2dadd_dependencies\u7684\u57fa\u672c\u4f5c\u7528_KingOfMyHeart\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n mark_as_advanced \u5c06CMake \u7684\u7f13\u5b58\u53d8\u91cf\u6807\u8bb0\u4e3a\u9ad8\u7ea7\u3002<\/p>\n mark_as_advanced([CLEAR|FORCE] VAR VAR2 VAR...) \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/hankern\/article\/details\/<\/p>\n \u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173<\/p>\n \u4e00 \u57fa\u7840\u8bed\u6cd5<\/p>\n \u2460 \u57fa\u672c\u6846\u67b6<\/p>\n <\/p>\n \u2461 \u4f18\u5148\u7ea7<\/p>\n <\/p>\n \u2462 \u6761\u4ef6\u7684\u7c7b\u578b<\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n<\/p>\n SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) #Use the install path for the RPATH CMAKE_INSTALL_RPATH \u2014 CMake 3.0.2 Documentation<\/p>\n \u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u641c\u7d22\u52a8\u6001\u5e93\u7684\u987a\u5e8f\uff08\u4f18\u5148\u7ea7\uff09\u4f18\u5148\u7ea7\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n 1. RPATH \uff0c\u7f16\u8bd1\u94fe\u63a5\u65f6\u52a0\u5165 -rpath \u53c2\u6570 \u6307\u5b9a\u7684\u76ee\u5f55 \u6240\u4ee5\u6211\u4eec\u8bbe\u7f6e\u4e86RPATH \uff0c\u5e76\u4e14RPATH \u4e0b\u6709\u8981\u627e\u7684\u52a8\u6001\u5e93\uff0c\u7a0b\u5e8f\u5c31\u9996\u5148\u52a0\u8f7d\u5b83<\/p>\n \u6ce8\u610f\uff1a<\/p>\n \u53ef\u4ee5\u770b\u5230\uff0cRPATH\u4e0eRUNPATH\u4e2d\u95f4\u9694\u7740LD_LIBRARY_PATH\u3002\u4e3a\u4e86\u8ba9\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539LD_LIBRARY_PATH\u6765\u6307\u5b9a \u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173<\/p>\n \u63a2\u8ba8CMake\u4e2d\u5173\u4e8eRPATH\u7684\u4f7f\u7528(https:\/\/www.cnblogs.com\/rickyk\/p\/3875084.html):<\/p>\n CMake\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u4f1a\u7ed9\u4f60\u7684exe\u52a0\u5165\u76f8\u5173RPATH\u7684\uff0c\u53ef\u80fd\u7ed9\u4f60\u52a0\u5165\u4f60\u4e0d\u60f3\u8981\u7684\u4e00\u4e9bRPATH\uff08\u5c31\u662f\u4e00\u4e9bCmake\u8ba4\u4e3a\u7684\u4f60\u7684\u7a0b\u5e8f\u5e94\u8be5\u53bb\u54ea\u91cc\u5bfb\u627e\u9700\u8981\u7684\u52a8\u6001\u5e93\u7684\u76ee\uff0c\u53ef\u4ee5\u7528\u201creadelf -d \u4f60\u7684\u7a0b\u5e8f\u201d\u6765\u67e5\u770b\uff0c\u4f8b\u5b50\uff1a<\/p>\n [root]readelf -d \/opt\/bin\/ceph-osd<\/p>\n Dynamic section at offset 0x10a36e8 contains 59 entries: \uff09<\/p>\n<\/p>\n \u4ed4\u7ec6\u67e5\u9605\u5bfb\u627e\u4e4b\u540e\u53d1\u73b0\uff0cCMake\u91cc\u9762\u7ef4\u62a4\u4e863\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684RPATH\u53d8\u91cf\uff0c\u5373CMAKE_SKIP_RPATH,CMAKE_SKIP_BUILD_RPATH,CMKAE_INSTALL_RPATH.<\/p>\n CMAKE_SKIP_RPATH,\u7b80\u5355\u8bf4\uff0c\u5c31\u662f\u5f3a\u5236CMake\u4e0d\u5728\u6784\u5efa\u671f\u95f4\u548c\u5b89\u88c5install\u671f\u95f4\u7ed9\u4f60\u52a0\u4e0a\u5b83\u6240\u8ba4\u4e3a\u7684RPATH.(\u4e0d\u5728\u52a8\u6001\u5e93\u4e2d\u5199\u5165RUNPATH\u9879)<\/p>\n \u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u53d8\u91cf\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u5c31\u662f\u5206\u522b\u5728\u6784\u5efa\u671f\u95f4\u548c\u5b89\u88c5\u671f\u95f4\u4e0d\u5141\u8bb8CMake\u7ed9\u4f60\u52a0\u5165\u76f8\u5173RPATH<\/p>\n \u5f53\u7136\u4e86\uff0c\u5982\u679c\u4f60\u4e4b\u540e\u60f3\u8981\u8ffd\u52a0RPATH,\u53ea\u9700\u8981\u5bf9\u8fd9\u4e09\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u6210FALSE\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n \u518d\u8c08CMake\u4e0eRPATH\uff08\uff09https:\/\/www.cnblogs.com\/rickyk\/p\/3884257.html\u518d\u8c08CMake\u4e0eRPATH\uff08\uff09\uff1a CMake\u4e3a\u4e86\u65b9\u4fbf\u7528\u6237\u7684\u5b89\u88c5\uff0c\u9ed8\u8ba4\u5728make install\u4e4b\u540e\u4f1a\u81ea\u52a8remove\u5220\u9664\u6389\u76f8\u5173\u7684RPATH,\u5982\u679c\u6211\u4eec\u5728\u8fd0\u884c\u73af\u5883\u4e0a\u6709\u4e2a\u76ee\u5f55\uff1a${CMAKE_INSTALL_PREFIX}\/lib\uff0c\u6211\u4eec\u5e0c\u671b\u6267\u884cmake install \u4e4b\u540e\uff0cRPATH\u53ef\u4ee5\u81ea\u52a8\u6dfb\u52a0\u5b83\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fd9\u4e48\u5199<\/p>\n \u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u53ea\u9488\u5bf9\u5f53\u524d\u7684target\u8fdb\u884cmake install\u7684\u65f6\u5019RPATH\u7684\u5199\u5165\u4e86\u3002<\/p>\n<\/p>\n \uff08\u6269\u5c55\uff1a\u5df2\u7ecf\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684RPATH\u53ef\u4ee5\u7528patchelf \u5de5\u5177\u4fee\u6539\u3010linux\u3011\u7a0b\u5e8f\u627e\u4e0d\u5230\u52a8\u6001\u5e93.so\u7684\u89e3\u51b3\u529e\u6cd5|\u67e5\u770b.so\u52a8\u6001\u5e93\u4fe1\u606f|.so\u52a8\u6001\u5e93\u52a0\u8f7d\u987a\u5e8f_linux-vdso.so.1\u627e\u4e0d\u5230_bandaoyu\u7684\u535a\u5ba2-CSDN\u535a\u5ba2\uff09<\/em><\/p>\n \u7c7b\u4f3c\u4e0a\u9762\uff0c\u6dfb\u52a0\u597d\u6240\u6709\u9700\u8981\u7684\u5e93\uff0c\u94fe\u63a5\u7684\u65f6\u5019\u5c31\u80fd\u627e\u5f97\u5230\u81ea\u5b9a\u4e49\u8def\u5f84lib\u4e0b\u9762\u7684\u5e93\u4e86<\/p>\n \u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173<\/p>\n set_target_properties \u8bbe\u7f6e\u76ee\u6807\u5c5e\u6027\u8bed\u6cd5\u662f\u5217\u51fa\u60a8\u8981\u66f4\u6539\u7684\u6240\u6709\u6587\u4ef6\uff0c\u7136\u540e\u63d0\u4f9b\u60a8\u63a5\u4e0b\u6765\u8981\u8bbe\u7f6e\u7684\u503c\u3002<\/p>\n set_target_properties\uff1ahttps:\/\/cmake.org\/cmake\/help\/v3.0\/command\/set_target_properties.html?highlight=set_target_properties<\/p>\n<\/blockquote>\n cmake\u4f1a\u5148\u5230*_DIR\u91cc\u6307\u5b9a.cmake\u6587\u4ef6\u91cc\u53bb\u627e\u627e\u4e0d\u5230\u4e86\u624d\u4f1a\u5230\/usr\u91cc\u53bb\u627e<\/p>\n \u6240\u4ee5\u8981\u5c0f\u5fc3cmake\u7ed9\u4f60\u627e\u7684\u6587\u4ef6\uff0c\u5982\u679c\u5728\u4e00\u53f0\u7535\u8111\u91cc\u88c5\u4e86\u4e0d\u6b62\u4e00\u4e2a\u7248\u672c\u5e93\uff08\u6216\uff1a\u591a\u4e2a\u7248\u672c\u7684\u6e90\u7801\u7f16\u8bd1\u540e\u628a\u5e93\u653e\u5230\u73af\u5883\u516c\u5171\u76ee\u5f55\u4e0b\uff09\u3002<\/p>\n<\/p>\n \u4ec0\u4e48\u662fRPATH?<\/strong><\/p>\n #\u5176\u5b9e\u5728Linux\u73af\u5883\u4e0b\uff0c\u4f7f\u7528\u52a8\u6001\u94fe\u63a5\u7684\u7a0b\u5e8f\u4f1a\u5148\u94fe\u63a5 Cmake\u548cRPATH<\/strong><\/p>\n \u90e8\u7f72\u7a0b\u5e8f\u5230\u673a\u5668\u4e0a\u540e\uff0c\u7a0b\u5e8f\u4f9d\u8d56\u7684\u52a8\u6001\u5e93 \u673a\u5668\u4e0a\u7684\u7cfb\u7edf\u5185\u4e0d\u4e00\u5b9a\u4f1a\u6709\uff0c\u6216\u8005\u81ea\u5e26\u4e86\u7684\u7248\u672c\u4e0d\u5bf9\uff0c\u6240\u4ee5\u4e00\u822c\u4f1a\u5728\u7a0b\u5e8f\u6587\u4ef6\u5939\u5185\u9644\u5e26\u5176\u4f9d\u8d56\u7684\u94fe\u63a5\u5e93\uff0c\u6240\u4ee5\u6700\u597d\u8fd8\u662f\u628a Cmake\u5bf9RPATH\u63d0\u4f9b\u4e86\u5f88\u591a\u9009\u9879\u652f\u6301\uff0c\u6211\u4eec\u4e00\u822c\u53ea\u5173\u6ce8\u8fd9\u51e0\u4e2a\u53d8\u91cf\u5c31\u597d\u4e86\uff1a<\/p>\n CMAKE_SKIP_BUILD_RPATH (\u5c31\u662f\u4e0d\u5728 \u53ef\u6267\u884c\u6587\u4ef6\u91cc\u9762\u6dfb\u52a0Cmake\u81ea\u8ba4\u4e3a\u7684 RPATH\uff09 \u4e0d\u4f7f\u7528Cmake\u9ed8\u8ba4RPATH\u8bbe\u7f6e\uff0c\u52a0\u4e0a\u5b8c\u6574\u7684RPATH<\/strong><\/p>\n<\/p>\n Cmake\u9ed8\u8ba4RPATH\u8bbe\u7f6e\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n Cmake\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c Cmake\u7684\u9ed8\u8ba4\u8bbe\u7f6e\u6211\u4eec\u80af\u5b9a\u662f\u4e0d\u80fd\u4f7f\u7528\u7684<\/strong>\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u7684\u65f6\u5019\u4e5f\u8981\u5e26\u4e0a \/\/\u6267\u884cmake install\u7684\u65f6\u5019 \u628astatic_lib \u653e\u5230bin<\/p>\n install(TARGETS static_lib DESTINATION bin)<\/p>\n \/\/\u4e5f\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf<\/p>\n install(TARGETS static_lib DESTINATION ${your_path})<\/p>\n \/\/\u8fd8\u53ef\u4ee5\u4e00\u6b21\u6307\u5b9a\u591a\u4e2a\u5bf9\u8c61\uff0c\u5e76\u6309\u7c7b\u578b\u6307\u5b9a\u653e\u5165\u7684\u76ee\u7684\u5730<\/p>\n install(TARGETS static_lib shared_lib exe \/\/static_lib shared_lib exe \u8fd9\u4e2a\u4e09\u4e2a\u5bf9\u8c61 \u547d\u4ee4\u7b2c\u4e00\u4e2a\u53c2\u6570 \u9488\u5bf9\u4e0d\u540c\u6587\u4ef6\u7c7b\u578b\uff0c\u6bd4\u5982\uff08 \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c \u66f4\u591a\u8be6\u60c5\u89c1\u6587\u7ae0\uff1a<\/p>\n cmake\u5e94\u7528\uff1a\u5b89\u88c5\u548c\u6253\u5305\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/<\/p>\n<\/p>\n \u5355\u884c\u6ce8\u91ca<\/em>:\u4f7f\u7528\u201c#\u201d\u591a\u884c\u6ce8\u91ca<\/em>:\u4f7f\u7528\u201c#[[ ]]\u201d\u4ee5\u4e0b\u56fe\u4e3a\u4f8b<\/p>\n <\/p>\n<\/p>\n \u5e93\u7684\u4f9d\u8d56\u5173\u7cfb\u4e3a\uff1a<\/p>\n app-->libbar.so-->libfoo.so<\/p>\n<\/p>\n \u94fe\u63a5\u9009\u9879\uff1aPRIVATE\uff0cINTERFACE\uff0cPUBLIC :https:\/\/zhuanlan.zhihu.com\/p\/<\/p>\n<\/p>\n \u7f51\u4e0a\u5173\u4e8e target_link_libraries \u4e2d\u7684 PRIVATE, PUBLIC , INTERFACE \u7684\u89e3\u91ca\u5927\u90e8\u5206\u662f\u9519\u7684\uff0c\u800c\u4e14\u4e0d\u662f\u4e00\u822c\u7684\u9519\uff0c\u662f\u80e1\u8bf4\u3002 \u56e0\u4e3a\u8fd9\u4e09\u4e2a\u5c5e\u6027\u5728\u4e0d\u540c\u7684\u547d\u4ee4\u4e2d\u4f7f\u7528\u65f6\u610f\u4e49\u4e0d\u540c\uff0c\u6709\u5f88\u591a\u662f\u4ecetarget_include_libraries\u4e2d\u6284\u6765\u7684\u3002<\/p>\n \u4ee5\u4e0b\u7684\u89e3\u91ca\u4e3b\u8981\u573a\u666f\u662flinux\u4e2d\uff0cwindows\u4e0d\u5b58\u5728\u8fd9\u79cd\u5173\u7cfb\uff0c\u65e0\u9700\u8003\u8651\u3002<\/p>\n \u6211\u4eec\u6765\u89e3\u91ca\u4e0b\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7a0b\u5e8f A , A\u8c03\u7528\u5e93B, B\u8c03\u7528\u5e93C. <\/p>\n A -> B -> C<\/p>\n A link B\u65f6\u4e0d\u7ba1\u662fprivate\u8fd8\u662fpublic\u90fd\u6ca1\u5173\u7cfb\uff0c\u6bd5\u7adfA\u4e0d\u9700\u8981\u5bfc\u51fa\u7b26\u53f7\uff0c\u4e5f\u6ca1\u6709\u4eba\u4ee5API\u65b9\u5f0f\u8c03\u7528\u5b83\u3002<\/p>\n \u73b0\u5728\u4e3b\u8981\u95ee\u9898\u5c31\u662fB\u8fd9\u4e2a\u5e93\u7528private\u8fd8\u662fpublic. C\u662f\u52a8\u6001\u5e93\u3002<\/p>\n \u5982\u679cB\u662f\u52a8\u6001\u6216\u9759\u6001\u5e93\uff0cC\u662f\u52a8\u6001\u5e93\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c31\u4f1a\u6709\u5f71\u54cd\u3002\u540c\u6837\uff0c\u5982\u679cB\u3001C\u540c\u4e3a\u9759\u6001\u5e93\u65f6\u4e5f\u4f1a\u6709\u95ee\u9898\u3002 <\/p>\n B\u7528private link C, \u6b64\u65f6A link B,\u4f46\u662f\u4e0d\u77e5\u9053B->C\u8fd9\u5c42\u5173\u7cfb\uff0c\u53ef\u4ee5\u6b63\u5e38link B. \u8fd0\u884c\u65f6\uff0cA->B->C \u65f6\uff0cB\u627e\u4e0d\u5230C\u4e2d\u7684\u51fd\u6570\u3002linux\u4e0b\u6ca1\u6709\u76f4\u63a5\u4f9d\u8d56\u5173\u7cfb\uff0c\u6240\u6709\u7684B\/C\u7684\u4f9d\u8d56\u90fd\u4f1a\u8f6c\u5230\u5230A\u4e0b\uff0c\u53ef\u4ee5\u7528LDD\u547d\u4ee4\u9a8c\u8bc1\uff0c\u6b64\u65f6A\u53ea\u4f9d\u8d56\u4e8eB, \u4e0d\u89c1C, \u5f53B\u4e2d\u7684\u51fd\u6570\u8c03\u7528C\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u56e0\u4e3a\u6ca1\u6709\u52a0\u8f7dC, \u6240\u4ee5\u62a5\u627e\u4e0d\u5230\u7b26\u53f7\u9519\u8bef\u3002\u89e3\u51b3\u7684\u529e\u6cd5\u5c31\u662f\u5728A link B\u65f6\uff0c\u540c\u6837\u4e5f\u5199\u4e0aC. \u4f46\u662f\u56e0\u4e3aprivate\u7684\u539f\u56e0\uff0cA\u662f\u4e0d\u77e5\u9053C\u4e2d\u7684\u7b26\u53f7\u8fd9\u4e8b\uff0c\u53ea\u80fd\u5f3a\u5236 link C\u5230A\u624d\u80fd\u89e3\u51b3\u3002<\/p>\n \u5982\u679cB link C\u65f6\u7528public \u6307\u793a\uff0c \u5f53\u7f16\u8bd1A\u65f6\uff0c\u5c31\u4f1a\u68c0\u67e5\u5230C\u4e2d\u7684\u7b26\u53f7\u6ca1\u6709\u5b9e\u73b0\uff0c\u6b64\u65f6\u4f60\u5c31\u4f1a\u77e5\u9053\u8981\u628aC link\u5230A\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\u3002<\/p>\n \u5176\u5b9eprivate\/public \u89e3\u51b3\u7684\u662f\u6307\u793a\u95ee\u9898\uff0c\u672c\u8d28\u4e0a\u53ef\u4ee5\u4f7f\u7528public \u6765\u89e3\u51b3\uff0c \u53ef\u4ee5\u51cf\u5c11\u5751\u3002<\/p>\n \u4e0b\u9762\u662ftarget_link_libraries\u4e2d\u7684\u89e3\u91ca\uff0c\u4e0d\u60f3\u770b\u82f1\u6587\u7684\uff0c\u76f4\u63a5\u62c9\u5230\u6700\u540e\u3002<\/p>\n Link Inheritance<\/p>\n Similarly, for any target, in the linking stage, we would need to decide, given the item to be linked, whether we have to put the item in the link dependencies, or the link interface, or both, in the compiled target. Here the link dependencies means the item has some implementations that the target would use, and it is linked to the item, so that whenever we call the functions or methods corresponding to those implementations it will always be mapped correctly to the implementations in item via the link, whereas the link interface means the target becomes an interface for linking the item for other targets which have dependencies on the target, and the target does not have to use item at all.<\/p>\n Link Type Description \u4e0b\u9762\u7528\u4eba\u8bdd(\u6c49\u8bed\uff09\u7ffb\u8bd1\u4e0b\uff1a<\/p>\n PUBLIC \u5728public\u540e\u9762\u7684\u5e93\u4f1a\u88abLink\u5230\u4f60\u7684target\u4e2d\uff0c\u5e76\u4e14\u91cc\u9762\u7684\u7b26\u53f7\u4e5f\u4f1a\u88ab\u5bfc\u51fa\uff0c\u63d0\u4f9b\u7ed9\u7b2c\u4e09\u65b9\u4f7f\u7528\u3002<\/p>\n PRIVATE \u5728private\u540e\u9762\u7684\u5e93\u4ec5\u88ablink\u5230\u4f60\u7684target\u4e2d\uff0c\u5e76\u4e14\u7ec8\u7ed3\u6389\uff0c\u7b2c\u4e09\u65b9\u4e0d\u80fd\u611f\u77e5\u4f60\u8c03\u4e86\u5565\u5e93<\/p>\n INTERFACE \u5728interface\u540e\u9762\u5f15\u5165\u7684\u5e93\u4e0d\u4f1a\u88ab\u94fe\u63a5\u5230\u4f60\u7684target\u4e2d\uff0c\u53ea\u4f1a\u5bfc\u51fa\u7b26\u53f7\u3002<\/p>\n ---- \u66f4\u65b0----------<\/p>\n target_link_libraries \u4f1a\u5728\u76ee\u6807\u7a0b\u5e8f\u4e2d\u751f\u6210rpath, \u8fd9\u70b9\u8bf7\u6ce8\u610f \u3002<\/p>\n \u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/znsoft\/article\/details\/<\/p>\n","protected":false},"excerpt":{"rendered":"\u3010cmake\u3011CMakeList\u6dfb\u52a0\u5e93|\u6dfb\u52a0\u5934\u6587\u4ef6|\u6dfb\u52a0\u8def\u5f84|add_executable\u3001add_library\u3001target_link_libraries|\u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|\u5b8f\u5f00\u5173CM...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7993"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7993"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7993\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u5b98\u7f51\u67e5\u9605<\/h2>\n
\u5f00\u80c3\u83dc\u4f8b\u5b50<\/h2>\n
CMakeLists\u751f\u6210\u548c\u6dfb\u52a0\u4f9d\u8d56\u5e93<\/h3>\n
\u2502 CMakeLists.txt \u2502 index.txt \u2502 \u251c\u2500build \u251c\u2500include \u2502 hello.h \u2502 hi.h \u2502 \u2514\u2500src hello.cxx hi.cxx<\/pre>\n
cmake_minimum_required(VERSION 3.1) #\u9879\u76ee\u540d project(libhello) # 1\u3001\u6307\u5b9a\u5e93\u7684\u76ee\u5f55\u53d8\u91cf set(libhello_src src\/hello.cxx) # \u6307\u5b9a\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 include_directories(\"${PROJECT_SOURCE_DIR}\/include\") # 2\u3001\u6dfb\u52a0\u5e93(\u5bf9\u5e94\u7684\u4e24\u4e2a\u9879\u76ee) add_library( hello_shared SHARED ${libhello_src}) add_library( hello_static STATIC ${libhello_src}) # \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 # \u5373\uff1a\u9759\u6001\u5e93\u540d\u4e3a libhello.a\uff1b \u52a8\u6001\u5e93\u540d\u4e3alibhello.so \uff1b # \u6240\u4ee5\uff0c\u5e0c\u671b \"hello_static\" \u5728\u8f93\u51fa\u65f6\uff0c\u4e0d\u662f\"hello_static\"\uff0c\u800c\u662f\u4ee5\"hello\"\u7684\u540d\u5b57\u663e\u793a\uff0c\u6545\u8bbe\u7f6e\u5982\u4e0b # SET_TARGET_PROPERTIES (hello_static PROPERTIES OUTPUT_NAME \"hello\") # 3\u3001cmake\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 # \u56e0\u6b64\uff0c\u5728\u6784\u5efalibhello.a\u65f6\uff0c\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 CLEAN_DIRECT_OUTPUT\u5c5e\u6027\u3002 SET_TARGET_PROPERTIES (hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) SET_TARGET_PROPERTIES (hello_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1) # 4\u3001\u6309\u7167\u89c4\u5219\uff0c\u52a8\u6001\u5e93\u662f\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u7248\u672c\u53f7\u7684\uff0c # VERSION\u6307\u4ee3\u52a8\u6001\u5e93\u7248\u672c\uff0cSOVERSION\u6307\u4ee3API\u7248\u672c\u3002 SET_TARGET_PROPERTIES (hello_static PROPERTIES VERSION 1.1 SOVERSION 1) SET_TARGET_PROPERTIES (hello_shared PROPERTIES VERSION 1.1 SOVERSION 1) # 5\u3001\u82e5\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 # \u672c\u4f8b\u4e2d\uff0c\u5c06hello\u7684\u5171\u4eab\u5e93\u5b89\u88c5\u5230<prefix>\/lib\u76ee\u5f55\uff1b # \u5c06hello.h\u5b89\u88c5<prefix>\/include\/hello\u76ee\u5f55\u3002 #INSTALL (TARGETS hello hello_shared LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) #INSTALL (TARGETS hello hello_static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) #INSTALL (FILES hello.h DESTINATION include\/hello)<\/pre>\n
xxx\/to\/path cd build cmake .. <\/pre>\n
# \u8bbe\u7f6eVS\u4f1a\u81ea\u52a8\u65b0\u5efaDebug\u548cRelease\u6587\u4ef6\u5939 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}\/Bin) # \u8bbe\u7f6e\u5206\u522b\u8bbe\u7f6eDebug\u548cRelease\u8f93\u51fa\u76ee\u5f55 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}\/..\/..\/build\/Debug) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}\/Lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}\/Bin)<\/pre>\n
CMakeLists\u66f4\u591a\u5c0f\u4f8b\u5b50<\/h3>\n
<\/h4>\n
\u751f\u6210.so\u5171\u4eab\u5e93\u6587\u4ef6<\/h4>\n
\/\/hello.cpp int Calculate_sum_Of_Two_Number(int x,int y) { int z=0; z=x+y; return (z); }<\/code><\/pre>\n
\/\/hello.hpp #ifndef __HELLO_H #define __HELLO_H int Calculate_sum_Of_Two_Number(int x,int y); #endif<\/code><\/pre>\n
\/\/main.cpp #include \"hello.hpp\" #include <stdio.h> int main(void) { int a=0,b=0,c=0; printf(\"please input two parameter:\"); scanf(\"%d\",&a); scanf(\"%d\",&b); c=Calculate_sum_Of_Two_Number(a,b); printf(\"the sum is : %d\",c); return 0; }<\/code><\/pre>\n
#\u8981\u6c42\u7684Cmake\u6700\u4f4e\u7248\u672c CMAKE_MINIMUM_REQUIRED( VERSION 2.8) #\u5de5\u7a0b\u540d\u79f0 PROJECT(main) #\u8bbe\u7f6e\u7f16\u8bd1\u5668\u7f16\u8bd1\u6a21\u5f0f\uff1a set( CMAKE_BUILD_TYPE \"Debug\" ) #\u751f\u6210\u5171\u4eab\u5e93 #get the shared package #here needs no .hpp add_library(calculate_shared SHARED hello.cpp) #\u751f\u6210\u53ef\u4ee5\u6267\u884c\u7684\u6587\u4ef6 add_executable(main main.cpp) #\u8fde\u63a5\u5171\u4eab\u5e93 target_link_libraries(main calculate_shared)<\/code><\/pre>\n
cd build
cmake ..
make
\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230build\u751f\u6210\u4e86 \u5982\u4e0b\u7684\u6587\u4ef6:<\/p>\n
CMakeFiles libcalculate_shared.so Makefile<\/p>\n\u8c03\u7528\u5171\u4eab\u5e93\u6587\u4ef6<\/strong><\/h4>\n
\/\/main.cpp #include <stdio.h> #include <iostream> #include \"hello.hpp\" using namespace std; int main(void) { int x=2,y=3; int z=0; z=Calculate_sum_Of_Two_Number(x,y); cout<<\"the result is:\"<<z<<endl; return 0; }<\/code><\/pre>\n
CMAKE_MINIMUM_REQUIRED( VERSION 2.8) PROJECT(main) #\u8bbe\u7f6e\u7f16\u8bd1\u5668\u7f16\u8bd1\u6a21\u5f0f\uff1a SET( CMAKE_BUILD_TYPE \"Debug\" ) SET(HELLO_INCLUE \/home\/fan\/dev\/cmake\/4-exer\/) SET(HELLO_SO \/home\/fan\/dev\/cmake\/4-exer\/build\/libcalculate_shared.so) INCLUDE_DIRECTORIES(${HELLO_INCLUE}) add_executable(main main.cpp) target_link_libraries(main ${HELLO_SO})<\/code><\/pre>\n
\u6bd4\u5982Opencv, \u5b83\u7684\u5934\u6587\u4ef6\u548c.so\u6587\u4ef6\u90fd\u5df2\u7ecf\u653e\u5728\u4e86\u7cfb\u7edf\u53d8\u91cf\u91cc\u9762\uff0c\u4e0d\u7528\u5411\u4e0a\u9762\u81ea\u5df1\u5b9a\u4e49\u4e86\uff08\u4e0a\u9762\u4f8b\u5b50\u91cc\u9762\u7684\u5934\u6587\u4ef6\u548c\u5171\u4eab\u5e93\u6587\u4ef6\u7684\u5730\u5740\u90fd\u662f\u6211\u81ea\u5df1\u8bbe\u7f6e\u7684\uff09<\/p>\n\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u7684 CMakeList<\/strong><\/h4>\n
#\u6dfb\u52a0\u5305\u542b\u6587\u4ef6\u7684\u7684\u76ee\u5f55 include_directories(${cppzmq_INCLUDE_DIR}) #\u7528${SOURCE_FILES}\u6307\u5b9a\u7684\u6587\u4ef6\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6sample_project add_executable(sample_project ${SOURCE_FILES}) #\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6sample_project \u9700\u8981\u8fde\u63a5 ${CMAKE_THREAD_LIBS_INIT}\u6307\u5b9a\u7684\u5e93 target_link_libraries (sample_project ${CMAKE_THREAD_LIBS_INIT}) <\/code><\/pre>\n
\u751f\u6210\u4e00\u4e2a.so\u52a8\u6001\u5e93\u7684 CMakeList <\/h4>\n
#\u7528${SRC_LISTS}\u6307\u5b9a\u7684\u6240\u6709\u7684\u6e90\u6587\u4ef6\u751f\u6210\u4e00\u4e2a\u5e93\uff0c\u540d\u5b57\u53eblibsugan add_library(libsugan ${SRC_LISTS}) #\u751f\u6210libsugan\u5e93\u9700\u8981\u94fe\u63a5 ${OpenCV_LIBS}\u3001 ${PROJECT_SOURCE_DIR}\/lib\/libCommonUtilities.so\u3001${PROJECT_SOURCE_DIR}\/lib\/libInuStreams.so target_link_libraries(libsugan ${OpenCV_LIBS} ${PROJECT_SOURCE_DIR}\/lib\/libCommonUtilities.so ${PROJECT_SOURCE_DIR}\/lib\/libInuStreams.so )<\/code><\/pre>\n
add_library\uff08\u751f\u6210\u5e93\uff09\uff0ctarget_link_libraries\uff08\u751f\u6210\u76ee\u6807\u8fde\u63a5\u7684\u5e93\uff09\uff0cset_target_properties<\/h2>\n
add_library(libsugan SHARED <\/strong>${SRC_LISTS}) #\u7528${SRC_LISTS}\u751f\u6210\u52a8\u6001\u5e93libsugan<\/p>\n<\/p>\n
${OpenCV_LIBS}
${PROJECT_SOURCE_DIR}\/lib\/libCommonUtilities.so
${PROJECT_SOURCE_DIR}\/lib\/libInuStreams.so
)<\/p>\n
set_target_properties(libsugan PROPERTIES OUTPUT_NAME \"sugan\")<\/p>\n
target_link_libraries(demo libsugan)<\/p>\n<\/p>\n#\u5de5\u7a0b\u540d\u5b57 project(Camera_sugan) #\u7f16\u8bd1\u6700\u4f4ecmake\u7248\u672c cmake_minimum_required(VERSION 2.6) #\u8bbe\u7f6ec++\u7f16\u8bd1\u5668 set( CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++11\" ) #\u5728\u6574\u4e2a\u7535\u8111\u4e0a\u627eopencv\u5305 find_package(OpenCV REQUIRED) #\u5305\u542b\u5934\u6587\u4ef6\u8def\u5f84 include_directories( .\/include\/inudev\/ .\/src\/ ) #\u5c06\u6240\u6709\u7684\u6e90\u6587\u4ef6\u5217\u4e3a\u4e00\u4e2a\u96c6\u5408\uff0c\u96c6\u5408\u540d\u5b57\u53eb\u505aSRC_LISTS set(SRC_LISTS .\/src\/inuitive.cpp .\/src\/runCamera_Qfeeltech.cpp ) #\u5c06\u96c6\u5408\u91cc\u7684\u6240\u6709\u7684\u6e90\u6587\u4ef6\u751f\u6210\u4e00\u4e2a\u9759\u6001\u5e93\uff0c\u8be5\u9759\u6001\u5e93\u7684\u540d\u5b57libsugan\uff0c \u6ce8\u610f\uff0c\u5728\u6574\u4e2aCmakeLists\u91cc\u90fd\u8981\u7528libsugan\u8fd9\u4e2a add_library(libsugan ${SRC_LISTS}) #\u540d\u5b57\u6765\u4ee3\u66ff\u4e4b\u524d\u90a3\u4e2a\u96c6\u5408\u751f\u6210\u7684\u5e93\u3002 target_link_libraries(libsugan #\u94fe\u63a5\u9759\u6001\u5e93\u9700\u8981\u7684\u4f9d\u8d56\u5e93 ${OpenCV_LIBS} ${PROJECT_SOURCE_DIR}\/lib\/libCommonUtilities.so ${PROJECT_SOURCE_DIR}\/lib\/libInuStreams.so )<\/code><\/pre>\n
CMAKE \u6dfb\u52a0\u7f16\u8bd1\u9009\u9879|-g\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/h4>\n
\u4f7f\u7528 add_compile_definitions() \u6dfb\u52a0\u9884\u5904\u7406\u5668\u5b9a\u4e49\u3002
\u4f7f\u7528 include_directories() \u6dfb\u52a0\u5305\u542b\u76ee\u5f55\u3002
\u4f7f\u7528 add_compile_options() \u6dfb\u52a0\u5176\u4ed6\u9009\u9879\u3002<\/p>\n
add_definitions(\"-g\")<\/p>\n\n
add_compile_options(-std=c++11)
message(STATUS \"optional:-std=c++11\")
endif(CMAKE_COMPILER_IS_GNUCXX)
<\/p>\n<\/blockquote>\nJ:\\workspace\\facecl.gcc>make b64 [ 50%] Building C object libb64\/CMakeFiles\/b64.dir\/libb64-1.2.1\/src\/cdecode.c.obj cc1.exe: warning: command line option \u2018-std=c++11\u2019 is valid for C++\/ObjC++ but not for C [100%] Building C object libb64\/CMakeFiles\/b64.dir\/libb64-1.2.1\/src\/cencode.c.obj cc1.exe: warning: command line option \u2018-std=c++11\u2019 is valid for C++\/ObjC++ but not for C Linking C static library libb64.a [100%] Built target b64<\/code><\/pre>\n
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS \"-std=c++11 ${CMAKE_CXX_FLAGS}\")
message(STATUS \"optional:-std=c++11\")
endif(CMAKE_COMPILER_IS_GNUCXX)<\/p>\n\u5305\u542b\u6587\u4ef6\u7684\u7684\u76ee\u5f55include_directories<\/h4>\n
\u4f7f\u7528 add_compile_definitions() \u6dfb\u52a0\u9884\u5904\u7406\u5668\u5b9a\u4e49\u3002
\u4f7f\u7528 include_directories() \u6dfb\u52a0\u5305\u542b\u76ee\u5f55\u3002
\u4f7f\u7528 add_compile_options() \u6dfb\u52a0\u5176\u4ed6\u9009\u9879\u3002<\/p>\n<\/p>\n\u4f18\u5316\u9879|\u4f18\u5316\u7b49\u7ea7<\/h4>\n
-O1\u5c1d\u8bd5\u4f18\u5316\u7f16\u8bd1\u65f6\u95f4\u548c\u53ef\u6267\u884c\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\nCmake\u8bbe\u7f6e\u4f18\u5316\u7b49\u7ea7| cmake \u751f\u6210 debug\u548c release \u7248<\/h4>\n
CMake \u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf CMAKE_BUILD_TYPE ,\u53ef\u4ee5\u7684\u53d6\u503c\u662f Debug \u3001Release\u3001 RelWithDebInfo \u548c MinSizeRel\u3002<\/em><\/s><\/p>\n\u5f53\u8fd9\u4e2a\u53d8\u91cf\u503c\u4e3a Debug \u7684\u65f6\u5019,CMake \u4f1a\u4f7f\u7528\u53d8\u91cf CMAKE_CXX_FLAGS_DEBUG \u548c CMAKE_C_FLAGS_DEBUG \u4e2d\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u7f16\u8bd1\u9009\u9879\u751f\u6210 Makefile ,<\/em><\/s><\/p>\n\u5f53\u8fd9\u4e2a\u53d8\u91cf\u503c\u4e3a Release \u7684\u65f6\u5019,\u5de5\u7a0b\u4f1a\u4f7f\u7528\u53d8\u91cf CMAKE_CXX_FLAGS_RELEASE \u548c CMAKE_C_FLAGS_RELEASE \u9009\u9879\u751f\u6210 Makefile\u3002<\/em><\/s><\/p>\n<\/p>\n\u63d0\u4f9b\u7684\u7ea7\u522b\u4e3a\uff1a<\/em><\/s><\/p>\n\n
Release - Adds the -O3 -DNDEBUG<\/code> flags to the compiler<\/em><\/s><\/li>\n
Debug - Adds the -g<\/code> flag<\/em><\/s><\/li>\n
MinSizeRel - Adds -Os -DNDEBUG<\/code><\/em><\/s><\/li>\n
RelWithDebInfo - Adds -O2 -g -DNDEBUG<\/code> flags<\/em><\/s><\/li>\n<\/ul>\n
\u94fe\u63a5\uff1ahttps:\/\/www.jianshu.com\/p\/de8e90<\/em><\/s>
<\/p>\n\n
CMakeCache.txt:89:CMAKE_ASM_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMakeCache.txt:92:CMAKE_ASM_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMakeCache.txt:95:CMAKE_ASM_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
CMakeCache.txt:123:CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMakeCache.txt:126:CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMakeCache.txt:129:CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
CMakeCache.txt:150:CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMakeCache.txt:153:CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMakeCache.txt:156:CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG<\/em><\/s>
<\/p>\n<\/blockquote>\ncmake\u8bbe\u7f6e\u9ed8\u8ba4CMAKE_BUILD_TYPE<\/em><\/s><\/p>\n\u539f\u6587<\/em><\/s>\uff1a[CMake] Set default build type in CMakeLists.txt<\/em><\/p>\n\u5728CMakeLists.txt<\/code>\u91cc\u5199\u5165<\/em><\/s><\/p>\n
\n
IF (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE \"Release\" CACHE STRING
\"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel.\" FORCE)
ENDIF()<\/em><\/s><\/p>\n<\/blockquote>\n\u8bbe\u7f6e\u9ed8\u8ba4\u6784\u5efa\u7c7b\u578b<\/em><\/s><\/h4>\nCMake\u63d0\u4f9b\u7684\u9ed8\u8ba4\u6784\u5efa\u7c7b\u578b\u662f\u4e0d\u5305\u542b\u7528\u4e8e\u4f18\u5316\u7684\u7f16\u8bd1\u5668\u6807\u5fd7\u3002\u5bf9\u4e8e\u67d0\u4e9b\u9879\u76ee\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u9ed8\u8ba4\u751f\u6210\u7c7b\u578b\uff0c\u4ee5\u4fbf\u4e0d\u5fc5\u8bb0\u4f4f\u8bbe\u7f6e\u5b83\u3002
\u4e3a\u6b64\uff0c\u60a8\u53ef\u4ee5\u5c06\u4ee5\u4e0b\u5185\u5bb9\u6dfb\u52a0\u5230CMakeLists.txt\u6587\u4ef6\u9876\u5c42<\/em><\/s><\/p>\n\n
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(\"Setting build type to 'RelWithDebInfo' as none was specified.\")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING \"Choose the type of build.\" FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS \"Debug\" \"Release\"
\"MinSizeRel\" \"RelWithDebInfo\")
endif()<\/em><\/s><\/p>\n\u4f5c\u8005\uff1axingxingRealzyx
\u94fe\u63a5\uff1ahttps:\/\/www.jianshu.com\/p\/de8e90<\/em><\/s><\/p>\n<\/blockquote>\nCMake\u8bbe\u7f6e\u7f16\u8bd1\u53c2\u6570\/\u9009\u9879<\/h4>\n
\n
\n
\n
\u5982\u4f55\u5728cmakelists\u4e2d\u52a0\u5165-ldl\u7f16\u8bd1\u9009\u9879<\/h4>\n
eg:
add_executable(xx ${ALL_F} ${WE_F})
TARGET_LINK_LIBRARIES(dl)
TARGET_LINK_LIBRARIES(m)<\/p>\n
target_link_libraries(${PROJECT_NAME} dl)<\/p>\ntarget_link_libraries\uff08exe1 -Wl\uff0c - -whole-archive lib1 -Wl\uff0c - no-whole-archive\uff09<\/code><\/pre>\n
CMake\u6307\u5b9agcc,g++\u7248\u672c\u7f16\u8bd1<\/h4>\n<\/p>\n
\n
SET(CMAKE_CXX_COMPILER \"\/usr\/local\/bin\/g++\")<\/p>\n<\/blockquote>\n\n
export CXX=\/usr\/local\/bin\/g++<\/p>\nCMake \u5173\u95ed\u8b66\u544a\u7684\u65b9\u6cd5<\/h2>\n
if\uff08CMAKE_COMPILER_IS_GNUCC\uff09 target_compile_options\uff08main PRIVATE\"-Wall\"\uff09 endif\uff08\uff09 if\uff08MSVC\uff09 target_compile_options\uff08main PRIVATE\"\/ W4\"\uff09 endif\uff08\uff09 <\/code><\/pre>\n
if\uff08CMAKE_COMPILER_IS_GNUCC\uff09 set\uff08CMAKE_CXX_FLAGS\"$ {CMAKE_CXX_FLAGS} -Wall\"\uff09 endif\uff08\uff09 if\uff08MSVC\uff09 set\uff08CMAKE_CXX_FLAGS\"$ {CMAKE_CXX_FLAGS} \/ W4\"\uff09 endif\uff08\uff09 <\/code><\/pre>\n
\u5173\u95ed\u7f16\u8bd1\u5668\u4f18\u5316<\/h4>\n
<\/h2>\n
CMakeLists \u5b9e\u73b0\u52a8\u6001\u5b8f\u5f00\u5173<\/h2>\n<\/p>\n
\u53bb\u6389\u7f16\u8bd1\u4f18\u5316<\/h4>\n
if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() set(CMAKE_CXX_FLAGS \"-Wall -Wextra\") set(CMAKE_CXX_FLAGS_DEBUG \"-g\") set(CMAKE_CXX_FLAGS_RELEASE \"-O3\")<\/code><\/pre>\n
some print command;
#ENDIF
\u7136\u540e\uff0c\u5728CMakeLists\u6587\u4ef6\u4e2d\u6dfb\u52a0DEBUG\u7684\u5b9a\u4e49\uff1a<\/p>\n
ADD_DEFINITIONS(-DDEBUG)
ENDIF()
\u6700\u540e\uff0c\u5728cmake\u7684\u65f6\u5019\u8bbe\u7f6e\u53c2\u6570 -DCMAKE_BUILD_TYPE \u4e3a DEBUG:<\/p>\n
$ make -j4
\u8fd9\u6837\u518d\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u65f6\u5c31\u4f1a\u6253\u5370\u51fasome print command\u7684debug\u4fe1\u606f\u4e86\u3002\u5982\u679c\u4e0d\u60f3\u770b\u5230debug\u4fe1\u606f\uff0c\u53ea\u9700\u5728\u53c2\u6570\u4e2d\u4e0d\u8bbe\u7f6eDEBUG\u53c2\u6570\uff0c\u6216\u8005\u5c06DEBUG\u53c2\u6570\u8bbe\u7f6e\u4e3a\u5176\u5b83\u503c\u5373\u53ef\uff08\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u4e8c\u8005\u9009\u5176\u4e00\uff09\uff1a<\/p>\n
$ cmake .. -DCMAKE_BUILD_TYPE=RELEASE
\u5230\u6b64 CMakeLists \u5b9e\u73b0\u52a8\u6001\u5b8f\u5f00\u5173\u4ecb\u7ecd\u5b8c\u6210\u3002<\/p>\nCMake--List\u7528\u6cd5<\/h2>\n
CmakeLists.txt\u5355\u884c\u6ce8\u91ca\u548c\u591a\u884c\u6ce8\u91ca <\/h4>\n
\u591a\u884c\u6ce8\u91ca\uff1a\u4f7f\u7528\u201c#[[ ]]\u201d<\/p>\nCMakeList \u901a\u914d\u7b26<\/h2>\n<\/p>\n
$<TARGET_OBJECTS:objLib>\u00b6 New in version 3.1. List of objects resulting from build of objLib. \u6784\u5efa objLib \u4ea7\u751f\u7684\u5bf9\u8c61\u5217\u8868<\/code><\/pre>\n
- CMakeLists.txt - include - a.hh - lib - CMakeLists.txt - a.cc - src - CMakeLists.txt - main.cc - test - CMakeLists.txt - test.cc<\/code><\/pre>\n
add_executable(test test.cc $<TARGET_OBJECTS:A>)<\/p>\n
add_library(A OBJECT A.cc)<\/p>\n\u5176\u4ed6\u672a\u5f52\u7c7b<\/h2>\n<\/p>\n
add_custom_target \u81ea\u5b9a\u4e49\u547d\u4ee4<\/h2>\n
add_custom_target(finish COMMAND ${CMAKE_COMMAND} -E echo compile finish COMMAND ${CMAKE_COMMAND} -E copy_directory ${SOURCE_DIR}\/config ${SOURCE_DIR}\/etc COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_DIR}\/log.txt ${SOURCE_DIR}\/etc ) <\/code><\/pre>\n
COMMAND ${CMAKE_COMMAND} -E echo compile finish COMMAND ${CMAKE_COMMAND} -E copy_directory ${SOURCE_DIR}\/config ${SOURCE_DIR}\/etc COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_DIR}\/log.txt ${SOURCE_DIR}\/etc<\/code><\/pre>\n
add_dependencies <\/h3>\n
mark_as_advanced <\/h4>\n
\u5c06\u7f13\u5b58\u7684\u53d8\u91cf\u6807\u8bb0\u4e3a\u9ad8\u7ea7\u53d8\u91cf\u3002\u5176\u4e2d\uff0c\u9ad8\u7ea7\u53d8\u91cf\u6307\u7684\u662f\u90a3\u4e9b\u5728CMake GUI\u4e2d\uff0c\u53ea\u6709\u5f53\u201c\u663e\u793a\u9ad8\u7ea7\u9009\u9879\u201d\u88ab\u6253\u5f00\u65f6\u624d\u4f1a\u88ab\u663e\u793a\u7684\u53d8\u91cf\u3002\u5982\u679cCLEAR\u662f\u7b2c\u4e00\u4e2a\u9009\u9879\uff0c\u53c2\u6570\u4e2d\u7684\u9ad8\u7ea7\u53d8\u91cf\u5c06\u53d8\u56de\u975e\u9ad8\u7ea7\u53d8\u91cf\u3002\u5982\u679cFORCE\u662f\u7b2c\u4e00\u4e2a\u9009\u9879\uff0c\u53c2\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u88ab\u63d0\u5347\u4e3a\u9ad8\u7ea7\u53d8\u91cf\u3002\u5982\u679c\u4e24\u8005\u90fd\u672a\u51fa\u73b0\uff0c\u65b0\u7684\u53d8\u91cf\u4f1a\u88ab\u6807\u8bb0\u4e3a\u9ad8\u7ea7\u53d8\u91cf\uff1b\u5982\u679c\u8fd9\u4e2a\u53d8\u91cf\u5df2\u7ecf\u662f\u9ad8\u7ea7\/\u975e\u9ad8\u7ea7\u72b6\u6001\u7684\u8bdd\uff0c\u5b83\u5c06\u4f1a\u7ef4\u6301\u539f\u72b6\u3002
\u8be5\u547d\u4ee4\u5728\u811a\u672c\u4e2d\u65e0\u6548\u3002<\/p>\n<\/h3>\n
Cmake\u6761\u4ef6\u5224\u65ad\u6307\u4ee4|if \u5224\u65ad\u4f18\u5148\u7ea7<\/h3>\n
\u8bbe\u7f6e\u7f16\u8bd1\u65f6\u548c\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53bb\u54ea\u4e2a\u76ee\u5f55\u627e\u52a8\u6001\u5e93<\/h2>\n<\/p>\n
#\u6307\u5b9a\u8fd0\u884c\u65f6\u52a8\u6001\u5e93\u7684\u52a0\u8f7d\u8def\u5f84<\/h4>\n
SET(CMAKE_INSTALL_RPATH \"\\${ORIGIN}\/lib\" ) #The rpath to use for installed targets.<\/p>\n\n
2. LD_LIBRARY_PATH \u8fd9\u4e2a\u73af\u5883\u53d8\u91cf\u6307\u5b9a\u7684\u76ee\u5f55
3. \/etc\/ld.so.conf \u914d\u7f6e\u6587\u4ef6\u3002
4. \/usr\/lib \u3001 \/lib \u548c \/usr\/local\/lib \uff0c\u7cfb\u7edf\u9ed8\u8ba4\u8def\u5f84\u3002<\/p>\n.so<\/code>\u6587\u4ef6\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u90fd\u5c06\u8f93\u51fa\u7684RPATH\u7559\u7a7a\uff0c\u5e76\u7528RUNPATH\u4ee3\u66ffRPATH\u3002<\/p>\n<\/blockquote>\n
\n
Tag Type Name\/Value
\u2026\u2026
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x0000000000000001 (NEEDED) Shared library: [ld-linux-x86-64.so.2]
0x000000000000000f (RPATH) Library rpath: [\/opt\/lib_upgrade\/osd:\/opt\/lib:\/opt\/lib_upgrade\/ceph-common:\/opt\/lib:\/opt\/lib\/ceph:\/opt\/lib\/ceph\/erasure-code:\/opt\/lib\/ceph\/compressor:\/opt\/lib\/ceph\/crypto:\/opt\/lib\/rados-classes:\/opt\/lib_upgrade_reserve\/ceph]
0x000000000000000c (INIT) 0x34ffe0
0x000000000000000d (FINI) 0xddee2c
\u2026\u2026<\/p>\n<\/blockquote>\n\n
cmake .. -DCMAKE_SKIP_RPATH=TRUE\u6216CMakelist\u4e2d\u52a0set(CMAKE_SKIP_BUILD_RPATH TRUE)<\/pre>\n<\/blockquote>\n
cmake .. -DCMAKE_SKIP_BUILD_RPATH=TRUE \u6216CMakelist\u4e2d\u52a0set(CMAKE_SKIP_BUILD_RPATH=TRUE) cmake .. -DCMAKE_SKIP_INSTALL_RPATH=TRUE \u6216CMakelist\u4e2d\u52a0set(CMAKE_SKIP_INSTALL_RPATH=TRUE)<\/code><\/pre>\n
<\/p>\nset(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}\/lib) \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u610f\u5473\u7740\u4f60\u6240\u6709\u7684target\u7684RPATH\u90fd\u4f1a\u5728install\u7684\u65f6\u5019\u88ab\u5199\u6210\u8fd9\u4e2a \u6709\u6ca1\u6709\u7b80\u5355\u7684\u9488\u5bf9\u67d0\u4e2atarget\u5462\uff0c\u806a\u660e\u7684\u4f60\u5e94\u8be5\u5df2\u7ecf\u60f3\u5230\u4e86<\/pre>\n
set_target_properties(myexe PROPERTIES INSTALL_RPATH \"${CMAKE_INSTALL_PREFIX}\/lib\")<\/pre>\n
#\u6307\u5b9a\u94fe\u63a5\u65f6\u52a8\u6001\u5e93\u7684\u8def\u5f84<\/h4>\n
################################################################################################################### set(OPENCV_DYNAMIC_LIBS \"${CMAKE_CURRENT_SOURCE_DIR}\/..\/lib\") # \u52a8\u6001 opencv native \u5e93\u8def\u5f84 add_library(libopencv_core SHARED IMPORTED ) set_target_properties(libopencv_core PROPERTIES IMPORTED_LOCATION \"${OPENCV_DYNAMIC_LIBS}\/libopencv_core.so\") add_library(libopencv_highgui SHARED IMPORTED ) set_target_properties(libopencv_highgui PROPERTIES IMPORTED_LOCATION \"${OPENCV_DYNAMIC_LIBS}\/libopencv_highgui.so\") add_library(libopencv_imgproc SHARED IMPORTED ) set_target_properties(libopencv_imgproc PROPERTIES IMPORTED_LOCATION \"${OPENCV_DYNAMIC_LIBS}\/libopencv_imgproc.so\") add_library(libopencv_video SHARED IMPORTED ) set_target_properties(libopencv_video PROPERTIES IMPORTED_LOCATION \"${OPENCV_DYNAMIC_LIBS}\/libopencv_video.so\") set(mOpenCV_LIBS libopencv_core libopencv_highgui libopencv_imgproc libopencv_video) set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -fopenmp \") target_link_libraries(main ${mOpenCV_LIBS} ) <\/code><\/pre>\n
\n
\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u641c\u7d22\u52a8\u6001\u5e93\u7684\u987a\u5e8f\uff08\u4f18\u5148\u7ea7\uff09\u4f18\u5148\u7ea7\u662f\u8fd9\u6837\u7684\uff1a 1. RPATH \uff0c\u7f16\u8bd1\u94fe\u63a5\u65f6\u52a0\u5165 -rpath \u53c2\u6570 \u6307\u5b9a\u7684\u76ee\u5f55 2. LD_LIBRARY_PATH \u8fd9\u4e2a\u73af\u5883\u53d8\u91cf\u6307\u5b9a\u7684\u76ee\u5f55 3. \/etc\/ld.so.conf \u914d\u7f6e\u6587\u4ef6\u3002 4. \/usr\/lib \u3001 \/lib \u548c \/usr\/local\/lib \uff0c\u7cfb\u7edf\u9ed8\u8ba4\u8def\u5f84\u3002<\/code><\/pre>\n
ld.so<\/code> \u8fd9\u4e2a\u5e93(OS X\u4e0a\u662f
dyld<\/code>)\uff0c\u7136\u540e\u901a\u8fc7
ld.so<\/code> \u6765\u67e5\u627e\u94fe\u63a5\u5176\u5b83\u7684\u5e93\u3002<\/em><\/p>\n
<\/h3>\n<\/p>\n
RPATH<\/code> \u52a0\u4e0a\u3002<\/p>\n
\n
CMAKE_BUILD_WITH_INSTALL_RPATH \uff08build\u7684\u65f6\u5019\u8fde\u63a5\u52a8\u6001\u5e93\u65f6\u53bbINSTALL_RPATH\u4e0b\u53bb\u627e\uff09
CMAKE_INSTALL_RPATH \uff08\u4e0a\u9762\u7684INSTALL_RPATH\u7684\u503c\uff0c\u5c31\u7528\u8fd9\u4e2a\u5566\u8bbe\u7f6e\uff09
CMAKE_INSTALL_RPATH_USE_LINK_PATH \uff08\u7528LINK_PATH\u7684\u503c\u7ed9INSTALL_RPATH\uff09<\/p>\n<\/blockquote>\n<\/h4>\n
set(CMAKE_SKIP_BUILD_RPATH FALSE) # FALSE-->\u8bbe\u5b9a\u7f16\u8bd1\u65f6\u52a0\u4e0a\u8981RPATH set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # FALSE-->\u7f16\u8bd1\u65f6RPATH\u4e0d\u4f7f\u7528INSTALL_RPATH set(CMAKE_INSTALL_RPATH \"\") # \u8bbe\u7f6eINSTALL_RPATH\u4e3a\u7a7a set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) # FALSE-->INSTALL_RPATH\u4e0d\u4f7f\u7528LINK_PATH\uff0c\u5b89\u88c5\u7684\u6267\u884c\u6587\u4ef6\u4e0d\u52a0\u4e0aRPATH<\/code><\/pre>\n
make install<\/code> \u4f1a\u628a\u5b89\u88c5\u7684\u6267\u884c\u6587\u4ef6\u7684
RPATH<\/code> \u5220\u6389\u7684\uff08\u5373\u6267\u884c\u6587\u4ef6\u641c\u7d22\u52a8\u6001\u5e93\u662f\u4e0d\u662f\u53bb
RPATH<\/code> \u627e\uff09\uff0c\u6240\u4ee5\u5c31\u4f1a\u51fa\u73b0\u4e0a\u9762\u6211\u6267\u884c\u5b89\u88c5\u597d\u7684\u6267\u884c\u6587\u4ef6\u62a5\u9519\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n
RPATH<\/code> \u7684\u8bbe\u7f6e\u3002<\/p>\n
set(INSTALL_LIB_DIR \"${PROJECT_BINARY_DIR}\/lib\") # \u5047\u8bbe\u5b89\u88c5\u76ee\u5f55\u5728\u7f16\u8bd1\u76ee\u5f55\u7684lib\u5b50\u76ee\u5f55\u5185 set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set(CMAKE_INSTALL_RPATH \"${CMAKE_INSTALL_PREFIX}\/lib\") set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)<\/code><\/pre>\n
# \u786e\u4fdd\u94fe\u63a5\u5e93\u4e0d\u5728 \u7cfb\u7edf\u9ed8\u8ba4\u5b89\u88c5\u7684\u76ee\u5f55 \u4e0a\u65f6\u66f4\u6539\u5230\u9879\u76eelib\u4e0a<\/code><\/p>\n
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_RPATH} isSystemDir) if(\"${isSystemDir}\" STREQUAL \"-1\") set(CMAKE_INSTALL_RPATH \"${INSTALL_LIB_DIR}\") endif(\"${isSystemDir}\" STREQUAL \"-1\")<\/code><\/pre>\n<\/p>\n
cmake install \u548c\u6253\u5305\u8bbe\u5b9a<\/h2>\n
\n
RUNTIME DESTINATION bin \/\/\u53ef\u6267\u884c\u6587\u4ef6\u653e\u5728bin<\/code>\u76ee\u5f55
LIBRARY DESTINATION lib \/\/\u5171\u4eab\u5e93\u6587\u4ef6\u653e\u5728lib<\/code>\u76ee\u5f55
ARCHIVE DESTINATION lib) \/\/\u9759\u6001\u5e93\u6587\u4ef6\u653e\u5728lib<\/code>\u76ee\u5f55<\/p>\n<\/p>\n
TARGETS<\/code>\u6307\u5b9a\u9700\u8981\u5b89\u88c5\u7684\u6784\u5efa\u76ee\u6807\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u662f\u9759\u6001\u5e93\u6587\u4ef6\u3001\u52a8\u6001\u5e93\u6587\u4ef6\u3001\u53ef\u6267\u884c\u6587\u4ef6\uff1b\u5b89\u88c5\u65f6\u5e38\u5e38\u6309\u7167\u6587\u4ef6\u7c7b\u578b\u5b89\u88c5\u5230\u4e0d\u540c\u7684\u5b50\u76ee\u5f55\uff0c\u6bd4\u5982\u5e93\u6587\u4ef6\u653e\u5728
lib<\/code>\u76ee\u5f55\uff0c\u53ef\u6267\u884c\u6587\u4ef6\u653e\u5728
bin<\/code>\u76ee\u5f55\u3002<\/p>\n
RUNTIME<\/code>,
ARCHIVE<\/code>,
LIBRARY<\/code>\uff0c
PUBLIC_HEADER<\/code>\uff09\uff0c\u53ef\u4ee5\u5206\u5f00\u8fdb\u884c\u914d\u7f6e\uff0c\u6bd4\u5982\u5206\u522b\u6307\u5b9a\u5b89\u88c5\u8def\u5f84\uff08
DESTINATION<\/code>\uff09\u3001\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\uff08
PERMISSIONS<\/code>\uff09\uff1b\u5982\u679c\u4e0d\u662f\u5728\u67d0\u4e2a\u7c7b\u522b\u4e0b\u7684\u5355\u72ec\u914d\u7f6e\uff0c\u90a3\u4e48\u5c31\u662f\u9488\u5bf9\u6240\u6709\u7c7b\u578b\u3002<\/p>\n
ARCHIVE<\/code>\u4e00\u822c\u662f\u6307\u9759\u6001\u5e93\uff0c
LIBRARY<\/code>\u5219\u662f\u6307\u5171\u4eab\u5e93\uff0c\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\uff0c\u7565\u6709\u5dee\u5f02\uff0c\u5b9e\u9645\u5e94\u7528\u611f\u89c9\u4e0d\u7b26\u5408\u9884\u671f\u65f6\u67e5\u770b\u4e00\u4e0b\u5b98\u65b9\u6587\u6863\u5373\u53ef\uff0c\u95ee\u9898\u4e0d\u5927\u3002<\/p>\n<\/blockquote>\n
CMakeLists.txt\u5355\u884c\u6ce8\u91ca\u548c\u591a\u884c\u6ce8\u91ca<\/h2>\n
target_link_libraries \u4e2d\u7684PRIVATE, PUBLIC, INTERFACE \u533a\u522b<\/h2>\n
\n\n
\n \u94fe\u63a5\u9009\u9879<\/th>\n bar<\/th>\n app<\/th>\n<\/tr>\n \n PRIVATE<\/td>\n \u4f20\u5165foo<\/td>\n \u4f20\u5165bar<\/td>\n<\/tr>\n \n INTERFACE<\/td>\n \u4e0d\u4f20\u5165foo<\/td>\n \u4f20\u5165bar\uff0c\u4f20\u5165foo<\/td>\n<\/tr>\n \n PUBLIC<\/td>\n \u4f20\u5165foo<\/td>\n \u4f20\u5165bar\uff0c\u4f20\u5165foo<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
PUBLIC All the objects following PUBLIC will be used for linking to the current target and providing the interface to the other targets that have dependencies on the current target.
PRIVATE All the objects following PRIVATE will only be used for linking to the current target.
INTERFACE All the objects following INTERFACE will only be used for providing the interface to the other targets that have dependencies on the current target.
For example, if the fruit library has the implementation of functions, such as size and color, and the apple library has a function apple_size which called the size from the fruit library and was PRIVATE linked with the fruit library. We could create an executable eat_apple that calls apple_size by PUBLIC or PRIVATE linking with the apple library. However, if we want to create an executable eat_apple that calls the size and color from the fruit library, only linking with the apple library will cause building error, since the fruit library was not part of the interface in the apple library, and is thus inaccessible to eat_apple. To make the apple library to inherit the size and color from the fruit library, we have to make the linking of the apple library to the the fruit library PUBLIC instead of PRIVATE.<\/p>\n