{"id":70648,"date":"2026-02-02T07:33:27","date_gmt":"2026-02-01T23:33:27","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/70648.html"},"modified":"2026-02-02T07:33:27","modified_gmt":"2026-02-01T23:33:27","slug":"%e3%80%8aros2-%e6%9e%84%e5%bb%ba%e7%b3%bb%e7%bb%9f-dissect%ef%bc%9acolcon-%e4%b8%8e-cmakelists-txt-%e6%98%af%e5%a6%82%e4%bd%95%e5%8d%8f%e4%bd%9c%e7%9a%84%e3%80%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/70648.html","title":{"rendered":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b"},"content":{"rendered":"<h2>\u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b<\/h2>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b<\/li>\n<li>\n<ul>\n<li>\n<ul>\n<li>1. \u4e09\u5c42\u67b6\u6784&#xff1a;\u8c01\u5728\u5e72\u6d3b&#xff1f;<\/li>\n<li>2. \u5b9e\u6218\u6d41\u7a0b&#xff1a;\u4e00\u6761\u6307\u4ee4\u7684\u80cc\u540e<\/li>\n<li>\n<ul>\n<li>Step 1: \u5305\u53d1\u73b0\u4e0e\u62d3\u6251\u6392\u5e8f<\/li>\n<li>Step 2: \u521b\u5efa\u9694\u79bb\u7684\u6784\u5efa\u73af\u5883<\/li>\n<li>Step 3: CMake \u914d\u7f6e\u9636\u6bb5&#xff08;Configuration&#xff09;<\/li>\n<li>Step 4: \u7f16\u8bd1\u9636\u6bb5&#xff08;Build&#xff09;<\/li>\n<li>Step 5: \u5b89\u88c5\u9636\u6bb5&#xff08;Install&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>3. \u4e3a\u4ec0\u4e48\u4e0d\u7528 colcon \u201c\u5168\u81ea\u52a8\u201d\u751f\u6210 CMakeLists&#xff1f;<\/li>\n<li>4. \u5e38\u89c1\u8bef\u533a\u6f84\u6e05<\/li>\n<li>\n<ul>\n<li>\u8bef\u533a 1&#xff1a;\u201c\u6211\u628a cpp \u653e\u5728 src\/ \u4e0b&#xff0c;colcon \u5e94\u8be5\u80fd\u81ea\u52a8\u627e\u5230\u201d<\/li>\n<li>\u8bef\u533a 2&#xff1a;\u201cCMakeLists.txt \u53ea\u662f\u7528\u6765\u627e ROS \u5305\u7684\u201d<\/li>\n<li>\u8bef\u533a 3&#xff1a;\u201c\u6539\u4e86 CMakeLists.txt \u4e0d\u9700\u8981\u91cd\u65b0 colcon build\u201d<\/li>\n<\/ul>\n<\/li>\n<li>5. \u5b9e\u6218&#xff1a;\u6700\u5c0f\u53ef\u8fd0\u884c\u7684 CMakeLists.txt \u89e3\u5256<\/li>\n<li>6. \u603b\u7ed3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u524d\u8a00 <\/p>\n<p>\u5f88\u591a\u4ece\u5355\u7247\u673a\u6216\u7eaf Python \u8f6c\u8fc7\u6765\u7684 ROS2 \u521d\u5b66\u8005\u90fd\u4f1a\u6709\u8fd9\u4e2a\u56f0\u60d1&#xff1a;\u201c\u6211\u660e\u660e\u53ea\u9700\u8981\u8dd1\u4e2a colcon build&#xff0c;\u4e3a\u4ec0\u4e48\u8fd8\u8981\u8d39\u52b2\u5199\u90a3\u4e00\u5806 CMakeLists.txt&#xff1f;colcon \u81ea\u5df1\u4e0d\u80fd\u628a cpp \u6587\u4ef6\u7f16\u8bd1\u4e86\u5417&#xff1f;\u201d<\/p>\n<p>\u7b54\u6848\u662f&#xff1a;colcon \u662f\u5305\u5de5\u5934&#xff0c;CMakeLists.txt \u662f\u65bd\u5de5\u56fe\u7eb8\u3002\u6ca1\u6709\u56fe\u7eb8&#xff0c;\u5305\u5de5\u5934\u4e0d\u77e5\u9053\u5982\u4f55\u628a\u94a2\u7b4b\u6c34\u6ce5&#xff08;\u6e90\u4ee3\u7801&#xff09;\u53d8\u6210\u697c\u623f&#xff08;\u53ef\u6267\u884c\u6587\u4ef6&#xff09;\u3002\u672c\u6587\u5c06\u62c6\u89e3 ROS2 Humble \u4e0b\u7684\u5b8c\u6574\u6784\u5efa\u94fe\u8def&#xff0c;\u8bb2\u6e05\u695a CMake \u5230\u5e95\u5728\u7f16\u8bd1\u65f6\u505a\u4e86\u4ec0\u4e48&#xff0c;\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u4f60\u7684 CMakeLists.txt \u4e0d\u80fd\u7701\u3002<\/p>\n<hr \/>\n<h4>1. \u4e09\u5c42\u67b6\u6784&#xff1a;\u8c01\u5728\u5e72\u6d3b&#xff1f;<\/h4>\n<p>\u5728 ROS2&#xff08;ament_cmake \u4f53\u7cfb&#xff09;\u4e2d&#xff0c;\u4e00\u6b21\u5b8c\u6574\u7684\u7f16\u8bd1\u6d89\u53ca\u4e09\u4e2a\u5c42\u7ea7&#xff1a;<\/p>\n<table>\n<tr>\u5c42\u7ea7\u89d2\u8272\u8f93\u5165\u6587\u4ef6\u8f93\u51fa<\/tr>\n<tbody>\n<tr>\n<td>\u6784\u5efa\u7f16\u6392\u5668<\/td>\n<td>colcon<\/td>\n<td>package.xml<\/td>\n<td>\u5e76\u884c\u8c03\u5ea6\u591a\u4e2a\u5305\u7684\u7f16\u8bd1\u987a\u5e8f<\/td>\n<\/tr>\n<tr>\n<td>\u6784\u5efa\u7cfb\u7edf\u751f\u6210\u5668<\/td>\n<td>cmake<\/td>\n<td>CMakeLists.txt<\/td>\n<td>\u751f\u6210 Makefile\/Ninja \u811a\u672c<\/td>\n<\/tr>\n<tr>\n<td>\u6784\u5efa\u6267\u884c\u5668<\/td>\n<td>make\/ninja<\/td>\n<td>Makefile<\/td>\n<td>\u8c03\u7528 g&#043;&#043; \u751f\u6210\u4e8c\u8fdb\u5236<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u952e\u6d1e\u5bdf&#xff1a;colcon \u4e0d\u76f4\u63a5\u7f16\u8bd1\u4ee3\u7801\u3002\u5b83\u53ea\u8d1f\u8d23\u627e\u5230\u5305 \u2192 \u89e3\u51b3\u4f9d\u8d56\u987a\u5e8f \u2192 \u8c03\u7528 cmake \u2192 \u8c03\u7528 make\u3002<\/p>\n<hr \/>\n<h4>2. \u5b9e\u6218\u6d41\u7a0b&#xff1a;\u4e00\u6761\u6307\u4ee4\u7684\u80cc\u540e<\/h4>\n<p>\u5f53\u4f60\u5728 ws01_plumbing \u76ee\u5f55\u6267\u884c&#xff1a;<\/p>\n<p>colcon build &#8211;packages-select cpp01_topic &#8211;cmake-args -DCMAKE_BUILD_TYPE<span class=\"token operator\">&#061;<\/span>Release<\/p>\n<p>\u7cfb\u7edf\u5185\u90e8\u7ecf\u5386\u4e86\u4ee5\u4e0b\u6d41\u6c34\u7ebf&#xff1a;<\/p>\n<h5>Step 1: \u5305\u53d1\u73b0\u4e0e\u62d3\u6251\u6392\u5e8f<\/h5>\n<p>colcon \u8bfb\u53d6 src\/ \u4e0b\u6240\u6709 package.xml&#xff0c;\u89e3\u6790\u51fa &lt;build_type&gt;ament_cmake&lt;\/build_type&gt; \u548c &lt;depend&gt; \u4f9d\u8d56&#xff0c;\u6784\u5efa\u51fa\u7f16\u8bd1\u987a\u5e8f\u56fe&#xff08;\u786e\u4fdd base_interfaces_demo \u5148\u4e8e cpp01_topic \u7f16\u8bd1&#xff09;\u3002<\/p>\n<h5>Step 2: \u521b\u5efa\u9694\u79bb\u7684\u6784\u5efa\u73af\u5883<\/h5>\n<p>colcon \u4e3a cpp01_topic \u521b\u5efa\u72ec\u7acb\u5de5\u4f5c\u76ee\u5f55&#xff1a;<\/p>\n<ul>\n<li>\u6e90\u7801\u76ee\u5f55&#xff1a;src\/cpp01_topic\/<\/li>\n<li>\u6784\u5efa\u76ee\u5f55&#xff1a;build\/cpp01_topic\/&#xff08;cmake \u548c make \u5728\u6b64\u5e72\u6d3b&#xff09;<\/li>\n<li>\u5b89\u88c5\u76ee\u5f55&#xff1a;install\/cpp01_topic\/&#xff08;\u6700\u7ec8\u4ea7\u7269\u5b58\u653e\u5730&#xff09;<\/li>\n<\/ul>\n<p>\u8fd9\u4e00\u6b65\u9694\u79bb\u6027\u662f colcon \u76f8\u6bd4\u65e7\u7248 catkin_make \u7684\u6700\u5927\u4f18\u52bf&#xff0c;\u907f\u514d\u591a\u4e2a\u5305\u5728\u76f8\u540c\u76ee\u5f55\u4e0b\u4e92\u76f8\u6c61\u67d3\u4e2d\u95f4\u6587\u4ef6\u3002<\/p>\n<h5>Step 3: CMake \u914d\u7f6e\u9636\u6bb5&#xff08;Configuration&#xff09;<\/h5>\n<p>colcon \u8c03\u7528 CMake&#xff0c;\u4f20\u5165\u5173\u952e\u53c2\u6570&#xff1a;<\/p>\n<p>cmake \/path\/to\/src\/cpp01_topic <span class=\"token punctuation\">\\\\<\/span><br \/>\n  -DCMAKE_INSTALL_PREFIX<span class=\"token operator\">&#061;<\/span>\/path\/to\/install <span class=\"token punctuation\">\\\\<\/span><br \/>\n  -DAMENT_PREFIX_PATH<span class=\"token operator\">&#061;<\/span>\/path\/to\/install<\/p>\n<p>\u6b64\u65f6 CMake \u9010\u884c\u89e3\u6790\u4f60\u7684 CMakeLists.txt&#xff0c;\u6267\u884c\u4ee5\u4e0b\u52a8\u4f5c&#xff1a;<\/p>\n<li>\n<p>\u67e5\u627e\u4f9d\u8d56&#xff1a;find_package(rclcpp REQUIRED) \u901a\u8fc7 rclcppConfig.cmake \u627e\u5230\u5934\u6587\u4ef6\u8def\u5f84&#xff08;\/opt\/ros\/humble\/include\/rclcpp&#xff09;\u548c\u5e93\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u58f0\u660e\u6784\u5efa\u76ee\u6807&#xff1a;add_executable(talker src\/demo01_talker_str.cpp) \u544a\u8bc9 CMake&#xff1a;\u6211\u8981\u628a\u4e00\u4e2a cpp \u7f16\u8bd1\u6210\u540d\u53eb talker \u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u94fe\u63a5\u4f9d\u8d56&#xff1a;ament_target_dependencies(talker rclcpp std_msgs) \u81ea\u52a8\u586b\u5145 -I&#xff08;\u5934\u6587\u4ef6&#xff09;\u3001-L&#xff08;\u5e93\u8def\u5f84&#xff09;\u3001-l&#xff08;\u94fe\u63a5\u5e93\u540d&#xff09;\u7f16\u8bd1\u53c2\u6570&#xff0c;\u65e0\u9700\u624b\u52a8\u5199 target_link_libraries\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b89\u88c5\u89c4\u5219&#xff1a;install(TARGETS talker DESTINATION lib\/${PROJECT_NAME}) \u6807\u8bb0\u8fd9\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u5728 make install \u65f6\u8981\u88ab\u590d\u5236\u5230 install\/lib\/cpp01_topic\/\u3002<\/p>\n<\/li>\n<p>\u6ce8\u610f&#xff1a;\u6b64\u65f6\u6ca1\u6709\u751f\u6210\u4efb\u4f55\u4e8c\u8fdb\u5236&#xff0c;CMake \u53ea\u662f\u5728 build\/ \u76ee\u5f55\u4e0b\u751f\u6210\u4e86 Makefile&#xff08;\u6216 build.ninja&#xff09;&#xff0c;\u91cc\u9762\u8bb0\u5f55\u4e86\u201c\u5982\u4f55\u7f16\u8bd1\u201d\u7684\u5b8c\u6574\u6307\u4ee4\u96c6\u3002<\/p>\n<h5>Step 4: \u7f16\u8bd1\u9636\u6bb5&#xff08;Build&#xff09;<\/h5>\n<p>colcon \u8c03\u7528 make&#xff08;\u6216 ninja&#xff09;&#xff1a;<\/p>\n<p><span class=\"token function\">make<\/span> -j8  <span class=\"token comment\"># 8 \u7ebf\u7a0b\u5e76\u884c\u7f16\u8bd1<\/span><\/p>\n<p>make \u8bfb\u53d6 Makefile&#xff0c;\u6267\u884c&#xff1a;<\/p>\n<ul>\n<li>\u7f16\u8bd1&#xff1a;g&#043;&#043; -c src\/demo01_talker_str.cpp -I\/opt\/ros\/humble\/include &#8230; -o CMakeFiles\/talker.dir\/demo01_talker_str.cpp.o<\/li>\n<li>\u94fe\u63a5&#xff1a;g&#043;&#043; CMakeFiles\/talker.dir\/demo01_talker_str.cpp.o -lrclcpp &#8230; -o talker<\/li>\n<\/ul>\n<p>\u751f\u6210\u7684 talker \u4e8c\u8fdb\u5236\u6587\u4ef6\u4f4d\u4e8e build\/cpp01_topic\/talker\u3002<\/p>\n<h5>Step 5: \u5b89\u88c5\u9636\u6bb5&#xff08;Install&#xff09;<\/h5>\n<p>colcon \u8c03\u7528 make install&#xff0c;CMake \u6839\u636e Step 3 \u7684 install() \u6307\u4ee4&#xff0c;\u628a\u4e8c\u8fdb\u5236\u62f7\u8d1d\u5230 install\/cpp01_topic\/lib\/cpp01_topic\/talker&#xff0c;\u5e76\u5904\u7406 package.xml \u7684\u5bfc\u51fa\u3002<\/p>\n<hr \/>\n<h4>3. \u4e3a\u4ec0\u4e48\u4e0d\u7528 colcon \u201c\u5168\u81ea\u52a8\u201d\u751f\u6210 CMakeLists&#xff1f;<\/h4>\n<p>\u56e0\u4e3a C&#043;&#043; \u662f\u9759\u6001\u7f16\u8bd1\u8bed\u8a00&#xff0c;\u7f16\u8bd1\u89c4\u5219\u9ad8\u5ea6\u4f9d\u8d56\u9879\u76ee\u7ed3\u6784&#xff0c;\u65e0\u6cd5\u50cf Python \u90a3\u6837\u76f4\u63a5\u89e3\u91ca\u6267\u884c\u3002CMakeLists.txt \u5fc5\u987b\u663e\u5f0f\u58f0\u660e&#xff1a;<\/p>\n<ul>\n<li>\u6e90\u6587\u4ef6\u5217\u8868&#xff1a;\u54ea\u4e9b cpp \u53c2\u4e0e\u7f16\u8bd1&#xff1f;<\/li>\n<li>\u5934\u6587\u4ef6\u8def\u5f84&#xff1a;\u4f60\u81ea\u5df1\u7684 include\/ \u76ee\u5f55\u5728\u54ea&#xff1f;<\/li>\n<li>\u4f9d\u8d56\u4f20\u9012&#xff1a;\u7528\u4e86\u81ea\u5b9a\u4e49\u6d88\u606f base_interfaces_demo&#xff0c;\u9700\u8981\u94fe\u63a5\u751f\u6210\u7684 .so \u6587\u4ef6\u3002<\/li>\n<li>\u7f16\u8bd1\u9009\u9879&#xff1a;\u5f00\u6ca1\u5f00 -std&#061;c&#043;&#043;17&#xff1f;\u6709\u6ca1\u6709 -Wall -Wextra&#xff1f;<\/li>\n<li>\u5b89\u88c5\u76ee\u6807&#xff1a;\u54ea\u4e9b\u6587\u4ef6\u8981\u6253\u5305\u8fdb install \u76ee\u5f55\u4f9b\u5176\u4ed6\u5305\u8c03\u7528&#xff1f;<\/li>\n<\/ul>\n<p>\u4e3e\u4f8b&#xff1a;\u5982\u679c\u4f60\u7684\u5305\u63d0\u4f9b\u4e86\u5e93&#xff08;.so&#xff09;\u4f9b\u5176\u4ed6\u5305\u94fe\u63a5&#xff0c;\u4f60\u9700\u8981\u5199&#xff1a;<\/p>\n<p>add_library(my_utils SHARED src\/utils.cpp)<br \/>\nament_target_dependencies(my_utils rclcpp)<br \/>\nament_export_targets(my_utils_targets HAS_LIBRARY_TARGET)  # \u5173\u952e&#xff1a;\u5bfc\u51fa\u7ed9\u522b\u7684\u5305\u7528<br \/>\ninstall(TARGETS my_utils EXPORT my_utils_targets &#8230;)<\/p>\n<p>\u8fd9\u7c7b\u8bed\u4e49\u65e0\u6cd5\u7531 colcon \u81ea\u52a8\u63a8\u65ad&#xff0c;\u5fc5\u987b\u7531\u5f00\u53d1\u8005\u7cbe\u786e\u63cf\u8ff0\u3002<\/p>\n<hr \/>\n<h4>4. \u5e38\u89c1\u8bef\u533a\u6f84\u6e05<\/h4>\n<h5>\u8bef\u533a 1&#xff1a;\u201c\u6211\u628a cpp \u653e\u5728 src\/ \u4e0b&#xff0c;colcon \u5e94\u8be5\u80fd\u81ea\u52a8\u627e\u5230\u201d<\/h5>\n<p>\u6b63\u89e3&#xff1a;C&#043;&#043; \u6ca1\u6709\u201c\u81ea\u52a8\u53d1\u73b0\u6e90\u6587\u4ef6\u201d\u673a\u5236&#xff08;\u4e0d\u50cf Python&#xff09;\u3002\u4f60\u5fc5\u987b\u5728 CMakeLists.txt \u91cc\u663e\u5f0f add_executable(xx src\/xx.cpp)\u3002\u9057\u6f0f\u4f1a\u5bfc\u81f4 undefined reference \u6216\u4f60\u622a\u56fe\u91cc\u90a3\u79cd target does not exist \u9519\u8bef\u3002<\/p>\n<h5>\u8bef\u533a 2&#xff1a;\u201cCMakeLists.txt \u53ea\u662f\u7528\u6765\u627e ROS \u5305\u7684\u201d<\/h5>\n<p>\u6b63\u89e3&#xff1a;find_package \u53ea\u662f 10% \u7684\u5de5\u4f5c\u3002\u66f4\u91cd\u8981\u7684\u662f\u5b9a\u4e49\u6784\u5efa\u56fe&#xff08;Build Graph&#xff09;&#xff1a;\u8c01\u4f9d\u8d56\u8c01\u3001\u7f16\u8bd1\u987a\u5e8f\u3001\u94fe\u63a5\u987a\u5e8f\u3002\u5982\u679c\u4f60\u7528\u4e86\u81ea\u5b9a\u4e49\u6d88\u606f&#xff08;.msg&#xff09;&#xff0c;CMake \u8fd8\u8981\u9a71\u52a8\u4ee3\u7801\u751f\u6210\u5668&#xff08;rosidl_generate_interfaces&#xff09;&#xff0c;\u628a .msg \u53d8\u6210 .hpp&#xff0c;\u8fd9\u4e9b\u90fd\u5728 CMakeLists.txt \u91cc\u7f16\u6392\u3002<\/p>\n<h5>\u8bef\u533a 3&#xff1a;\u201c\u6539\u4e86 CMakeLists.txt \u4e0d\u9700\u8981\u91cd\u65b0 colcon build\u201d<\/h5>\n<p>\u6b63\u89e3&#xff1a;\u4fee\u6539 CMakeLists.txt \u5c5e\u4e8e\u5143\u6784\u5efa\u7cfb\u7edf\u53d8\u66f4&#xff0c;\u5fc5\u987b\u91cd\u65b0\u8d70 Step 3&#xff08;cmake \u914d\u7f6e&#xff09;\u3002colcon \u4f1a\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u4fee\u6539\u5e76\u91cd\u65b0\u8fd0\u884c cmake&#xff0c;\u4f46\u5982\u679c\u4f60\u5728\u6784\u5efa\u76ee\u5f55\u624b\u52a8\u6539&#xff0c;\u4f1a\u5bfc\u81f4\u6784\u5efa\u72b6\u6001\u4e0d\u4e00\u81f4&#xff0c;\u5efa\u8bae rm -rf build\/ \u540e\u91cd\u65b0 colcon build\u3002<\/p>\n<hr \/>\n<h4>5. \u5b9e\u6218&#xff1a;\u6700\u5c0f\u53ef\u8fd0\u884c\u7684 CMakeLists.txt \u89e3\u5256<\/h4>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a ROS2 C&#043;&#043; \u8282\u70b9\u7684\u6807\u51c6\u6a21\u677f&#xff0c;\u9010\u884c\u6ce8\u91ca\u8bf4\u660e CMake \u7684\u4ecb\u5165\u65f6\u673a&#xff1a;<\/p>\n<p>cmake_minimum_required(VERSION 3.8)          # (1) \u58f0\u660e\u6700\u4f4e CMake \u7248\u672c&#xff0c;\u5fc5\u987b\u7b2c\u4e00\u884c<br \/>\nproject(cpp01_topic)                         # (2) \u5b9a\u4e49\u9879\u76ee\u540d&#xff0c;\u5f71\u54cd ${PROJECT_NAME}<\/p>\n<p># (3) \u7f16\u8bd1\u6807\u51c6\u58f0\u660e&#xff1a;C&#043;&#043;17&#xff08;\u5f71\u54cd g&#043;&#043; \u7684 -std \u53c2\u6570&#xff09;<br \/>\nif(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES &#034;Clang&#034;)<br \/>\n  add_compile_options(-Wall -Wextra -Wpedantic)<br \/>\nendif()<\/p>\n<p># (4) \u4f9d\u8d56\u53d1\u73b0\u9636\u6bb5&#xff1a;CMake \u4f1a\u53bb \/opt\/ros\/humble\/share\/rclcpp\/cmake \u627e\u914d\u7f6e\u6587\u4ef6<br \/>\nfind_package(ament_cmake REQUIRED)<br \/>\nfind_package(rclcpp REQUIRED)<br \/>\nfind_package(std_msgs REQUIRED)<br \/>\nfind_package(base_interfaces_demo REQUIRED)<\/p>\n<p># (5) \u6784\u5efa\u76ee\u6807\u58f0\u660e&#xff1a;\u544a\u8bc9 CMake \u751f\u6210\u4e00\u4e2a\u53eb talker \u7684\u53ef\u6267\u884c\u6587\u4ef6<br \/>\nadd_executable(talker src\/demo01_talker_str.cpp)<\/p>\n<p># (6) \u4f9d\u8d56\u5173\u8054&#xff1a;\u81ea\u52a8\u586b\u5145 -I \u548c -L \u8def\u5f84<br \/>\nament_target_dependencies(talker<br \/>\n  rclcpp<br \/>\n  std_msgs<br \/>\n  base_interfaces_demo<br \/>\n)<\/p>\n<p># (7) \u7f16\u8bd1\u7279\u6027&#xff1a;\u786e\u4fdd\u4f7f\u7528 C&#043;&#043;14\/17&#xff08;ROS2 Humble \u8981\u6c42 C&#043;&#043;17&#xff09;<br \/>\ntarget_compile_features(talker PUBLIC c_std_99 cxx_std_17)<\/p>\n<p># (8) \u5b89\u88c5\u9636\u6bb5&#xff1a;make install \u65f6\u628a talker \u62f7\u5230 install\/lib\/cpp01_topic\/<br \/>\ninstall(TARGETS talker<br \/>\n  DESTINATION lib\/${PROJECT_NAME}<br \/>\n)<\/p>\n<p># (9) ament \u5305\u6807\u8bb0&#xff1a;\u58f0\u660e\u8fd9\u662f\u4e00\u4e2a ROS2 \u5305&#xff0c;\u5fc5\u987b\u6700\u540e<br \/>\nament_package()<\/p>\n<p>\u7f16\u8bd1\u65f6\u53d1\u751f\u4e86\u4ec0\u4e48&#xff1a;<\/p>\n<li>cmake .. \u9636\u6bb5\u5904\u7406 (1)-(9)&#xff0c;\u751f\u6210 build.ninja\u3002<\/li>\n<li>ninja talker \u9636\u6bb5\u6839\u636e (5) \u7684 add_executable \u548c\u6e90\u6587\u4ef6\u8def\u5f84&#xff0c;\u8c03\u7528 g&#043;&#043; \u751f\u6210\u5bf9\u8c61\u6587\u4ef6\u3002<\/li>\n<li>\u94fe\u63a5\u9636\u6bb5\u6839\u636e (6) \u89e3\u6790\u51fa\u7684 -lrclcpp \u7b49\u53c2\u6570&#xff0c;\u751f\u6210\u6700\u7ec8\u4e8c\u8fdb\u5236\u3002<\/li>\n<li>ninja install \u9636\u6bb5\u6267\u884c (8)&#xff0c;\u4f7f ros2 run cpp01_topic talker \u80fd\u627e\u5230\u8be5\u4e8c\u8fdb\u5236\u3002<\/li>\n<hr \/>\n<h4>6. \u603b\u7ed3<\/h4>\n<ul>\n<li>colcon \u89e3\u51b3\u591a\u5305\u534f\u4f5c\u95ee\u9898&#xff1a;\u81ea\u52a8\u6392\u5e8f\u4f9d\u8d56\u3001\u5e76\u884c\u7f16\u8bd1\u3001\u73af\u5883\u9694\u79bb\u3002<\/li>\n<li>CMakeLists.txt \u89e3\u51b3\u5355\u5305\u6784\u5efa\u95ee\u9898&#xff1a;\u6e90\u6587\u4ef6\u7ec4\u7ec7\u3001\u4f9d\u8d56\u89e3\u6790\u3001\u7f16\u8bd1\u9009\u9879\u3001\u5b89\u88c5\u89c4\u5219\u3002<\/li>\n<li>CMake \u662f\u4e24\u8005\u4e4b\u95f4\u7684\u6865\u6881&#xff1a;\u628a\u58f0\u660e\u5f0f\u7684\u6784\u5efa\u63cf\u8ff0&#xff08;CMakeLists.txt&#xff09;\u8f6c\u5316\u4e3a\u547d\u4ee4\u5f0f\u7684\u7f16\u8bd1\u6307\u4ee4&#xff08;Makefile&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u8bb0\u5fc6\u53e3\u8bc0&#xff1a;<\/p>\n<p>colcon \u7ba1\u6392\u961f&#xff0c;CMake \u7ba1\u914d\u65b9&#xff0c;make \u7ba1\u7092\u83dc\u3002<\/p>\n<p>\u7406\u89e3\u4e86\u8fd9\u4e2a\u6d41\u7a0b&#xff0c;\u4f60\u5c31\u80fd\u770b\u61c2\u7f16\u8bd1\u9519\u8bef&#xff08;\u5982 \u201ctarget does not exist\u201d \u662f CMake \u914d\u7f6e\u9636\u6bb5\u95ee\u9898&#xff0c;\u201cundefined reference\u201d \u662f\u94fe\u63a5\u9636\u6bb5\u95ee\u9898&#xff09;&#xff0c;\u4e5f\u80fd\u6b63\u786e\u914d\u7f6e\u8de8\u5305\u4f9d\u8d56\u548c\u81ea\u5b9a\u4e49\u6d88\u606f\u63a5\u53e3\u3002\u4e0d\u518d\u4e3a\u201c\u4e3a\u4ec0\u4e48\u8981\u5199\u8fd9\u4e48\u591a CMake \u4ee3\u7801\u201d\u800c\u56f0\u60d1\u2014\u2014\u56e0\u4e3a C&#043;&#043; \u7684\u7f16\u8bd1&#xff0c;\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u573a\u7cbe\u786e\u7684\u5de5\u7a0b\u63a7\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b \u6587\u7ae0\u76ee\u5f55 \u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b1. \u4e09\u5c42\u67b6\u6784&#xff1a;\u8c01\u5728\u5e72\u6d3b&#xff1f;2. \u5b9e\u6218\u6d41\u7a0b&#xff1a;\u4e00\u6761\u6307\u4ee4\u7684\u80cc\u540eStep 1: \u5305\u53d1\u73b0\u4e0e\u62d3\u6251\u6392\u5e8fStep 2: \u521b\u5efa\u9694\u79bb\u7684\u6784\u5efa\u73af\u5883Step 3: CMake \u914d\u7f6e\u9636\u6bb5&#xff08;Configuration&#xff09;Step 4:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,174,190,1813,384,427,2399],"topic":[],"class_list":{"0":"post-70648","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-c","9":"tag-190","10":"tag-1813","11":"tag-384","12":"tag-427","13":"tag-2399"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/70648.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b \u6587\u7ae0\u76ee\u5f55 \u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b1. \u4e09\u5c42\u67b6\u6784&#xff1a;\u8c01\u5728\u5e72\u6d3b&#xff1f;2. \u5b9e\u6218\u6d41\u7a0b&#xff1a;\u4e00\u6761\u6307\u4ee4\u7684\u80cc\u540eStep 1: \u5305\u53d1\u73b0\u4e0e\u62d3\u6251\u6392\u5e8fStep 2: \u521b\u5efa\u9694\u79bb\u7684\u6784\u5efa\u73af\u5883Step 3: CMake \u914d\u7f6e\u9636\u6bb5&#xff08;Configuration&#xff09;Step 4:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/70648.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-01T23:33:27+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70648.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/70648.html\",\"name\":\"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-01T23:33:27+00:00\",\"dateModified\":\"2026-02-01T23:33:27+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/70648.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/70648.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70648.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/70648.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b \u6587\u7ae0\u76ee\u5f55 \u300aROS2 \u6784\u5efa\u7cfb\u7edf &#xff1a;colcon \u4e0eCMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b1. \u4e09\u5c42\u67b6\u6784&#xff1a;\u8c01\u5728\u5e72\u6d3b&#xff1f;2. \u5b9e\u6218\u6d41\u7a0b&#xff1a;\u4e00\u6761\u6307\u4ee4\u7684\u80cc\u540eStep 1: \u5305\u53d1\u73b0\u4e0e\u62d3\u6251\u6392\u5e8fStep 2: \u521b\u5efa\u9694\u79bb\u7684\u6784\u5efa\u73af\u5883Step 3: CMake \u914d\u7f6e\u9636\u6bb5&#xff08;Configuration&#xff09;Step 4:","og_url":"https:\/\/www.wsisp.com\/helps\/70648.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-01T23:33:27+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/70648.html","url":"https:\/\/www.wsisp.com\/helps\/70648.html","name":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-01T23:33:27+00:00","dateModified":"2026-02-01T23:33:27+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/70648.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/70648.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/70648.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300aROS2 \u6784\u5efa\u7cfb\u7edf dissect\uff1acolcon \u4e0e CMakeLists.txt \u662f\u5982\u4f55\u534f\u4f5c\u7684\u300b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=70648"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70648\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=70648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=70648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=70648"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=70648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}