ROS rosmake 错误

Posted

技术标签:

【中文标题】ROS rosmake 错误【英文标题】:ROS rosmake error 【发布时间】:2016-02-24 12:32:50 【问题描述】:

我目前正在使用 ROS 进行一个项目。我对 ROS 及其功能完全陌生。目前我正在做一个关于 ROS 的教程,并第一次开始使用代码。虽然我确实有一些 C++ 经验,但我不知道出了什么问题。在 ROS 中,我使用 rosmake 命令,但它无法编译 cpp 文件/代码。谁能帮我弄清楚为什么会发生错误? (以及可能如何解决它?)

下面是cpp文件代码:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char **argv)

ros::init(argc, argv, "example1_a");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_
msgs::String>("message", 1000);
ros::Rate loop_rate(10);
while (ros::ok())

std_msgs::String msg;
std::stringstream ss;
ss << " I am the example1_a node ";
msg.data = ss.str();
//ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();

return 0;

这里是构建日志:

mkdir -p bin
cd build && cmake -Wdev -DCMAKE_TOOLCHAIN_FILE=/opt/ros/indigo/share/ros/core/rosbuild/rostoolchain.cmake  ..
[rosbuild] Building package chapter2_tutorials
[rosbuild] using multiarch 'x86_64-linux-gnu' for finding Boost
-- Using CATKIN_DEVEL_PREFIX: /home/rospc/dev/rosbook/chapter2_tutorials/build/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/indigo
-- This workspace overlays: /opt/ros/indigo
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Skip enable_testing() for dry packages
-- Using CATKIN_TEST_RESULTS_DIR: /home/rospc/dev/rosbook/chapter2_tutorials/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.16
-- Using these message generators: gencpp;genlisp;genpy
[rosbuild] Including /opt/ros/indigo/share/roslisp/rosbuild/roslisp.cmake
[rosbuild] Including /opt/ros/indigo/share/roscpp/rosbuild/roscpp.cmake
[rosbuild] Including /opt/ros/indigo/share/rospy/rosbuild/rospy.cmake
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rospc/dev/rosbook/chapter2_tutorials/build
cd build && make -j8 -l8
make[1]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[2]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
[  0%] Built target rospack_genmsg_libexe
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
[  0%] Built target rosbuild_precompile
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
Scanning dependencies of target example1_a
Scanning dependencies of target example1_b
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[3]: Entering directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
[100%] [100%] Building CXX object CMakeFiles/example1_b.dir/src/example1_b.cpp.o
Building CXX object CMakeFiles/example1_a.dir/src/example1_a.cpp.o
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp: In function ‘int main(int, char**)’:
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:8:42: error: ‘std_’ was not declared in this scope
 ros::Publisher chatter_pub = n.advertise<std_
                                          ^
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:8:32: error: parse error in template argument list
 ros::Publisher chatter_pub = n.advertise<std_
                                ^
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:9:30: error: no matching function for call to ‘ros::NodeHandle::advertise(const char [8], int)’
 msgs::String>("message", 1000);
                              ^
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:9:30: note: candidates are:
In file included from /opt/ros/indigo/include/ros/ros.h:45:0,
                 from /home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:1:
/opt/ros/indigo/include/ros/node_handle.h:236:15: note: template<class M> ros::Publisher ros::NodeHandle::advertise(const string&, uint32_t, bool)
     Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)
               ^
/opt/ros/indigo/include/ros/node_handle.h:236:15: note:   template argument deduction/substitution failed:
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:9:30: error: template argument 1 is invalid
 msgs::String>("message", 1000);
                              ^
In file included from /opt/ros/indigo/include/ros/ros.h:45:0,
                 from /home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:1:
/opt/ros/indigo/include/ros/node_handle.h:302:13: note: template<class M> ros::Publisher ros::NodeHandle::advertise(const string&, uint32_t, const SubscriberStatusCallback&, const SubscriberStatusCallback&, const VoidConstPtr&, bool)
   Publisher advertise(const std::string& topic, uint32_t queue_size,
             ^
/opt/ros/indigo/include/ros/node_handle.h:302:13: note:   template argument deduction/substitution failed:
/home/rospc/dev/rosbook/chapter2_tutorials/src/example1_a.cpp:9:30: error: template argument 1 is invalid
 msgs::String>("message", 1000);
                              ^
make[3]: *** [CMakeFiles/example1_a.dir/src/example1_a.cpp.o] Error 1
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[2]: *** [CMakeFiles/example1_a.dir/all] Error 2
make[2]: *** Waiting for unfinished jobs....
Linking CXX executable ../bin/example1_b
make[3]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
[100%] Built target example1_b
make[2]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/rospc/dev/rosbook/chapter2_tutorials/build'
make: *** [all] Error 2

下一个代码来自第二个文件,它确实编译并且不会产生错误:

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)

ROS_INFO("I heard: [%s]", msg->data.c_str());


int main(int argc, char **argv)

ros::init(argc, argv, "example1_b");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("message", 1000, chatterCallback);
ros::spin();
return 0;

【问题讨论】:

代码中有一个奇怪的换行符,不应该存在。 C++ 对此类错误可能非常敏感,并且会产生很多后续错误,这些错误不是实际错误,而是由于更早的错误而导致的问题。修复第一个错误,剩下的大部分(甚至可能全部)错误应该会消失。另请注意,错误消息会告诉您确切的位置(文件名、行号,甚至是在行中遇到错误的“箭头”),使用它来定位源中的错误。 另外,与问题无关,编译器不需要,但缩进使代码更易于人类阅读。请以某种方式缩进您的代码,特别是如果其他人会看到它(对于“其他人”,您应该在一个月、一年或更长时间内包含您自己)。 谢谢,这解决了问题。不明白为什么我没有弄清楚。 附注:您使用rosmake 而不是catkin 是否有特定原因? rosmake 已过时,一般不应用于新软件包。 我不知道,谢谢。今天开始用catkin,效果很好。 【参考方案1】:

C++ 代码中的换行只能通过 3 种方式完成:

cout<<"Error:This is a really long error "
"message that exceeds the maximum permitted length.\n";

cout<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

c\
o\
u\
t<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

除此之外,它会给你带来错误......

【讨论】:

【参考方案2】:

变化:

ros::Publisher chatter_pub = n.advertise<std_
msgs::String>("message", 1000);

作者:

ros::Publisher chatter_pub = n.advertise<std_msgs::String>("message", 1000);

PS : 你不应该使用 rosmake。这是构建工作空间的旧工具。

使用catkin_makecatkin build

注意:您不能在同一个工作区上同时使用 catkin_makecatkin build。如果你想切换,你需要清理你的工作区。为此,您必须删除工作区文件夹中的构建、开发和安装目录。

$ cd ~/catkin_ws
$ sudo rm -r devel build install

替代方法:

如果您使用 catkin_make 并想清理工作区以切换到 catkin build 使用

$ catkin_make clean
$ catkin build

如果您使用 catkin build 并想清理工作区以切换到 catkin_make 使用

$ catkin clean
$ catkin_make

rosmake vs catkin:

由于 ROS Groovy,不推荐使用 rosmake,以下是我们使用 catkin 的原因:

更好地利用 Cmake 提供的所有工具, Cmake 的 C 表示跨平台。这样做的目的是让 ROS 在不同的架构 (ARM) 和操作系统(OS X 和 Windows)上更具可移植性。 使用 rosbuild 每个包都有自己的 Cmake,包依赖管理不太稳定。 Cmake 可以通过并行化依赖项来更智能地编译。 通过优化依赖关系减少内存空间。 一个单一的安装目标,允许公司在 ROS 上运行封闭的专有软件。

catkin_make 与 catkin 构建

完全一样,但最新的catkin build。现在,有些人建议从 catkin_make 切换到 catkin build。两者的主要区别在于catkin build有更好的隔离包。它将为每个包构建一个工作区。

【讨论】:

【参考方案3】:

使用 C++11 制作您的项目。 为此,请在您的命令中使用 cmake 标志:

catkin_make -DCMAKE_CXX_FLAGS=-std=c++11

或者

catkin build -DCMAKE_CXX_FLAGS=-std=c++11

【讨论】:

以上是关于ROS rosmake 错误的主要内容,如果未能解决你的问题,请参考以下文章

ROS: catkin和rosmake创建和编译功能包执行节点

ROS2 colcon 构建未在 Windows 中显示任何错误消息

ROS环境中的构建错误(catkin)

ROS错误及其解决方法

ROS错误及其解决方法

ROS/c++常见段错误以及排查