{"id":350,"date":"2024-10-12T16:26:49","date_gmt":"2024-10-12T08:26:49","guid":{"rendered":"https:\/\/www.armfun.cn\/?p=350"},"modified":"2024-10-12T16:26:49","modified_gmt":"2024-10-12T08:26:49","slug":"ros1-%e7%9a%84cmakelists-txt%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.armfun.cn\/?p=350","title":{"rendered":"ROS1 \u7684CMakeLists.txt\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 <strong>ROS1<\/strong> \u4e2d\uff0c<strong><code>CMakeLists.txt<\/code><\/strong> \u662f\u4e00\u4e2a\u5173\u952e\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5982\u4f55\u6784\u5efa\u548c\u7f16\u8bd1\u4f60\u7684 ROS \u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5178\u578b\u7684 <strong>ROS1 <code>CMakeLists.txt<\/code><\/strong> \u6587\u4ef6\u7684\u4e3b\u8981\u90e8\u5206\u53ca\u5176\u4f5c\u7528\u8bf4\u660e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>cmake_minimum_required<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cmake_minimum_required(VERSION 3.0.2)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u6307\u5b9a\u6240\u9700\u7684\u6700\u5c0f <strong>CMake<\/strong> \u7248\u672c\u3002ROS1 Noetic \u901a\u5e38\u4f7f\u7528 3.0.2 \u6216\u66f4\u9ad8\u7248\u672c\u3002\u786e\u4fdd CMake \u6784\u5efa\u7cfb\u7edf\u5728\u5408\u9002\u7684\u7248\u672c\u4e0a\u8fd0\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>project<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>project(my_ros_pkg)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u58f0\u660e\u9879\u76ee\u7684\u540d\u79f0\u3002\u8fd9\u91cc\u7684\u9879\u76ee\u540d <code>my_ros_pkg<\/code> \u901a\u5e38\u4e0e\u5305\u540d\u4fdd\u6301\u4e00\u81f4\u3002CMake \u5c06\u4f7f\u7528\u8be5\u540d\u79f0\u4f5c\u4e3a\u751f\u6210\u6784\u5efa\u7cfb\u7edf\u7684\u6807\u8bc6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>find_package<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find_package(catkin REQUIRED COMPONENTS<br>  roscpp<br>  std_msgs<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u67e5\u627e\u5e76\u52a0\u8f7d\u6307\u5b9a\u7684 <strong>catkin<\/strong> \u5305\u53ca\u5176\u4f9d\u8d56\u9879\u3002<code>REQUIRED<\/code> \u8868\u793a\u8fd9\u4e9b\u7ec4\u4ef6\u662f\u5fc5\u9700\u7684\uff0c<code>COMPONENTS<\/code> \u5173\u952e\u5b57\u540e\u5217\u51fa\u7684\u662f\u4f60\u9879\u76ee\u4f9d\u8d56\u7684 ROS \u5305\u3002\u5e38\u7528\u7684\u7ec4\u4ef6\u6709 <code>roscpp<\/code>\uff08\u7528\u4e8e C++ \u5f00\u53d1\uff09\uff0c<code>std_msgs<\/code>\uff08\u6807\u51c6\u6d88\u606f\u7c7b\u578b\u5305\uff09\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>catkin_package<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>catkin_package(<br>  CATKIN_DEPENDS roscpp std_msgs<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5b9a\u4e49\u4f60\u7684\u9879\u76ee\u4f5c\u4e3a <strong>catkin<\/strong> \u5305\uff0c\u5e76\u58f0\u660e\u8be5\u5305\u5bf9\u5176\u4ed6\u5305\u7684\u4f9d\u8d56\u3002<code>CATKIN_DEPENDS<\/code> \u4e2d\u5217\u51fa\u7684\u5305\u8868\u660e\u6b64\u5305\u5728\u8fd0\u884c\u65f6\u6216\u7f16\u8bd1\u65f6\u4f9d\u8d56\u4e8e\u8fd9\u4e9b ROS \u5305\u3002<code>catkin_package()<\/code> \u8fd8\u4f1a\u81ea\u52a8\u4e3a\u8be5\u5305\u751f\u6210\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u4f7f\u5f97\u5176\u4ed6 ROS \u5305\u53ef\u4ee5\u4f9d\u8d56\u8be5\u5305\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>include_directories<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>include_directories(<br>  include<br>  ${catkin_INCLUDE_DIRS}<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5b9a\u4e49\u7f16\u8bd1\u65f6\u9700\u8981\u5305\u542b\u7684\u5934\u6587\u4ef6\u76ee\u5f55\u3002<code>include<\/code> \u662f\u4f60\u7684\u5305\u7684\u5934\u6587\u4ef6\u8def\u5f84\uff0c<code>${catkin_INCLUDE_DIRS}<\/code> \u662f <code>catkin<\/code> \u548c\u5176\u4ed6\u4f9d\u8d56\u5305\u7684\u5934\u6587\u4ef6\u8def\u5f84\uff0c\u5b83\u4eec\u5728\u8fd0\u884c <code>find_package(catkin ...)<\/code> \u540e\u4f1a\u81ea\u52a8\u83b7\u53d6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong><code>add_executable<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>add_executable(${PROJECT_NAME}_node src\/main.cpp)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5b9a\u4e49\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002<code>add_executable<\/code> \u7528\u4e8e\u5c06\u6e90\u6587\u4ef6\uff08\u5982 <code>src\/main.cpp<\/code>\uff09\u7f16\u8bd1\u4e3a\u4e00\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>${PROJECT_NAME}_node<\/code> \u4f1a\u89e3\u6790\u4e3a <code>my_ros_pkg_node<\/code>\uff0c\u5373\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong><code>target_link_libraries<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>target_link_libraries(${PROJECT_NAME}_node<br>  ${catkin_LIBRARIES}<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5c06\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0e\u6240\u9700\u7684\u5e93\u8fdb\u884c\u94fe\u63a5\u3002<code>${catkin_LIBRARIES}<\/code> \u662f\u6240\u6709\u7531 <code>catkin_package()<\/code> \u548c <code>find_package()<\/code> \u4e2d\u5217\u51fa\u7684\u4f9d\u8d56\u5305\u6240\u63d0\u4f9b\u7684\u5e93\u3002\u5b83\u786e\u4fdd\u4f60\u7684 ROS \u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u80fd\u591f\u8bbf\u95ee\u6240\u6709\u9700\u8981\u7684\u5e93\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong><code>add_dependencies<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u6307\u5b9a\u8be5\u8282\u70b9\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5982\u679c\u4f60\u6709\u81ea\u52a8\u751f\u6210\u7684\u6d88\u606f\u3001\u670d\u52a1\u6216\u52a8\u4f5c\u6587\u4ef6\uff0c\u6216\u8005\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5305\u7684\u751f\u6210\u76ee\u6807\uff0c<code>add_dependencies<\/code> \u53ef\u4ee5\u786e\u4fdd\u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u4f9d\u8d56\uff0c\u4fdd\u8bc1\u751f\u6210\u7684\u76ee\u6807\u6587\u4ef6\u6309\u987a\u5e8f\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u6d88\u606f\u3001\u670d\u52a1\u548c\u52a8\u4f5c\u751f\u6210<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684\u5305\u5b9a\u4e49\u4e86\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u3001\u670d\u52a1\u6216\u52a8\u4f5c\u6587\u4ef6\uff0c\u4f60\u8fd8\u9700\u8981\u5728 <code>CMakeLists.txt<\/code> \u4e2d\u8fdb\u884c\u7279\u6b8a\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>find_package<\/code> \u90e8\u5206\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>find_package(catkin REQUIRED COMPONENTS<br>  roscpp<br>  std_msgs<br>  message_generation<br>)<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6dfb\u52a0\u6d88\u606f\/\u670d\u52a1\u6587\u4ef6\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>add_message_files(<br>  FILES<br>  MyMessage.msg<br>)<br><br>add_service_files(<br>  FILES<br>  MyService.srv<br>)<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u751f\u6210\u6d88\u606f\u3001\u670d\u52a1\u548c\u52a8\u4f5c\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>generate_messages(<br>  DEPENDENCIES<br>  std_msgs<br>)<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6dfb\u52a0\u4f9d\u8d56\u9879\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>catkin_package(<br>  CATKIN_DEPENDS message_runtime roscpp std_msgs<br>)<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u5b89\u88c5\u53ef\u6267\u884c\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u5b89\u88c5\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>install(TARGETS ${PROJECT_NAME}_node<br>  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5c06\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5b89\u88c5\u5230 <code>catkin<\/code> \u7684\u4e8c\u8fdb\u5236\u76ee\u5f55\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>\u7f16\u8bd1\u9009\u9879<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u9700\u8981\u8bbe\u7f6e\u7f16\u8bd1\u5668\u7684\u9009\u9879\uff08\u5982\u4f7f\u7528 C++11 \u6807\u51c6\uff09\uff0c\u53ef\u4ee5\u6dfb\u52a0\u5982\u4e0b\u7f16\u8bd1\u9009\u9879\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>add_compile_options(-std=c++11)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u8fd9\u91cc\u662f\u8ba9\u4f60\u7684\u7f16\u8bd1\u5668\u652f\u6301 C++11 \u6807\u51c6\u3002\u5982\u679c\u4f60\u4f7f\u7528\u73b0\u4ee3 C++\uff0c\u6bd4\u5982 ROS Noetic\uff0c\u5219\u5e94\u8bbe\u7f6e\u4e3a <code>C++14<\/code> \u6216 <code>C++17<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>\u81ea\u5b9a\u4e49\u5b89\u88c5\u8def\u5f84<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u9700\u8981\u5b89\u88c5\u7684\u914d\u7f6e\u6587\u4ef6\u6216\u5176\u4ed6\u8d44\u6e90\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>install(DIRECTORY launch\/<br>  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}\/launch<br>)<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>: \u5c06 <code>launch<\/code> \u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5b89\u88c5\u5230\u6307\u5b9a\u8def\u5f84\u4e2d\u3002\u5e38\u7528\u4e8e\u5c06\u542f\u52a8\u6587\u4ef6\u5b89\u88c5\u5230 <code>catkin<\/code> \u7684\u5171\u4eab\u76ee\u5f55\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u5b8c\u6574\u7684 <code>CMakeLists.txt<\/code> \u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cmake_minimum_required(VERSION 3.0.2)<br>project(my_ros_pkg)<br><br>find_package(catkin REQUIRED COMPONENTS<br>  roscpp<br>  std_msgs<br>)<br><br>catkin_package(<br>  CATKIN_DEPENDS roscpp std_msgs<br>)<br><br>include_directories(<br>  include<br>  ${catkin_INCLUDE_DIRS}<br>)<br><br>add_executable(${PROJECT_NAME}_node src\/main.cpp)<br><br>target_link_libraries(${PROJECT_NAME}_node<br>  ${catkin_LIBRARIES}<br>)<br><br>add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})<br><br>install(TARGETS ${PROJECT_NAME}_node<br>  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}<br>)<br><br>install(DIRECTORY launch\/<br>  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}\/launch<br>)<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cmake_minimum_required<\/code> \u548c <code>project<\/code> \u7528\u4e8e\u5b9a\u4e49\u57fa\u672c\u7684 CMake \u9879\u76ee\u3002<\/li>\n\n\n\n<li><code>find_package<\/code> \u67e5\u627e\u5e76\u52a0\u8f7d ROS \u4f9d\u8d56\u5305\u3002<\/li>\n\n\n\n<li><code>catkin_package<\/code> \u58f0\u660e\u6b64\u5305\u662f\u4e00\u4e2a catkin \u5305\uff0c\u5e76\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u3002<\/li>\n\n\n\n<li><code>add_executable<\/code> \u548c <code>target_link_libraries<\/code> \u7528\u4e8e\u751f\u6210\u5e76\u94fe\u63a5 ROS \u8282\u70b9\u3002<\/li>\n\n\n\n<li><code>add_dependencies<\/code> \u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u751f\u6210\u76ee\u6807\u6309\u987a\u5e8f\u7f16\u8bd1\u3002<\/li>\n\n\n\n<li>\u5b89\u88c5\u89c4\u5219 <code>install<\/code> \u7528\u4e8e\u5b89\u88c5\u53ef\u6267\u884c\u6587\u4ef6\u548c\u5176\u4ed6\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u58f0\u660e\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684 <strong>ROS1<\/strong> \u5305\uff0c\u5e76\u4e14\u80fd\u591f\u4e0e <strong>ROS<\/strong> \u5e93\u548c\u5176\u4ed6\u5de5\u5177\u6b63\u786e\u94fe\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 ROS1 \u4e2d\uff0cCMakeLists.txt \u662f\u4e00\u4e2a\u5173\u952e\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-350","post","type-post","status-publish","format-standard","hentry","category-8"],"views":1703,"_links":{"self":[{"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=350"}],"version-history":[{"count":1,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/www.armfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.armfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}