CMakeList.txt出错
-
当我试着单独构建一个c++功能包时colcon build可以构建成功,也能正常运行
但是当我希望同时构建与oop.cpp同目录src下的test.cpp时
使用colcon build却出现以下报错:yozora@yozoracomputer:~/cpp_oop_ws/src$ colcon build
Starting >>> oop
--- stderr: oop
In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/c++allocator.h:33,
from /usr/include/c++/11/bits/allocator.h:46,
from /usr/include/c++/11/memory:64,
from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:153,
from /home/yozora/cpp_oop_ws/src/oop/src/test.cpp:1:
/usr/include/c++/11/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = rclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}; _Tp = rclcpp::Node]’:
/usr/include/c++/11/bits/alloc_traits.h:516:17: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = rclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}; _Tp = rclcpp::Node; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocatorrclcpp::Node]’
/usr/include/c++/11/bits/shared_ptr_base.h:519:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {int, const char (&)[8], const char (&)[8]}; _Tp = rclcpp::Node; _Alloc = std::allocatorrclcpp::Node; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/11/bits/shared_ptr_base.h:650:16: required from ‘std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = rclcpp::Node; _Alloc = std::allocatorrclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/11/bits/shared_ptr_base.h:1342:14: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocatorrclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}; _Tp = rclcpp::Node; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/11/bits/shared_ptr.h:409:59: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocatorrclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}; _Tp = rclcpp::Node]’
/usr/include/c++/11/bits/shared_ptr.h:862:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = rclcpp::Node; _Alloc = std::allocatorrclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}]’
/usr/include/c++/11/bits/shared_ptr.h:878:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = rclcpp::Node; _Args = {int, const char (&)[8], const char (&)[8]}]’
/home/yozora/cpp_oop_ws/src/oop/src/test.cpp:5:45: required from here
/usr/include/c++/11/ext/new_allocator.h:162:11: error: no matching function for call to ‘rclcpp::Node::Node(int, const char [8], const char [8])’
162 | { ::new((void *)__p) _Up(std::forward<_Args>(_args)...); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/ros/humble/include/rclcpp/rclcpp/executors/single_threaded_executor.hpp:28,
from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:22,
from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155,
from /home/yozora/cpp_oop_ws/src/oop/src/test.cpp:1:
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:1294:3: note: candidate: ‘rclcpp::Node::Node(const rclcpp::Node&, const string&)’
1294 | Node(
| ^~~~
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:1294:3: note: candidate expects 2 arguments, 3 provided
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:101:12: note: candidate: ‘rclcpp::Node::Node(const string&, const string&, const rclcpp::NodeOptions&)’
101 | explicit Node(
| ^~~~
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:102:25: note: no known conversion for argument 1 from ‘int’ to ‘const string&’ {aka ‘const std::cxx11::basic_string<char>&’}
102 | const std::string & node_name,
| ~~~~~~~~~~~^
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:89:12: note: candidate: ‘rclcpp::Node::Node(const string&, const rclcpp::NodeOptions&)’
89 | explicit Node(
| ^
/opt/ros/humble/include/rclcpp/rclcpp/node.hpp:89:12: note: candidate expects 2 arguments, 3 provided
gmake[2]: *** [CMakeFiles/test.dir/build.make:76: CMakeFiles/test.dir/src/test.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:165: CMakeFiles/test.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake: *** [Makefile:146: all] Error 2Failed <<< oop [15.6s, exited with code 2]
Summary: 0 packages finished [15.9s]
1 package failed: oop
1 package had stderr output: oop版本:WSL(windows11)Ubuntu22.04 ROS2humble 当我再尝试学习时发现在import Node上出了问题,请问有什么解决方法吗。我只能搜索到引用rclpy失败的却无法搜索到引用Node失败的。
-
从错误信息来看,问题出现在
test.cpp
文件中尝试使用rclcpp::Node
类时,提供的构造函数参数与rclcpp::Node
类所期望的不匹配。错误信息提示rclcpp::Node
没有接受三个int, const char[8], const char[8]
类型参数的构造函数,而你尝试在test.cpp
的第5行调用这样一个构造函数。在 ROS 2 中,
rclcpp::Node
的常见构造函数包括:- 接受节点名称和命名空间(以及可选的
rclcpp::NodeOptions
)。 - 接受另一个
rclcpp::Node
实例和节点名称(用于节点克隆,但不常用)。
错误日志中列出了可能的构造函数,例如:
Node(const std::string &node_name, const std::string &namespace_, const rclcpp::NodeOptions &options = rclcpp::NodeOptions())
explicit Node(const std::string &node_name, const rclcpp::NodeOptions &options = rclcpp::NodeOptions())
explicit Node(const rclcpp::Node &other, const std::string &node_name)
解决方案
-
检查
test.cpp
中的rclcpp::Node
实例化代码:- 确保你传递的参数类型和数量与
rclcpp::Node
的某个构造函数匹配。 - 通常,你只需要节点名称和可选的命名空间。
例如,如果你的代码类似于:
auto node = std::make_shared<rclcpp::Node>(1, "node_name", "namespace");
应改为:
auto node = std::make_shared<rclcpp::Node>("node_name", "namespace");
- 确保你传递的参数类型和数量与
-
如果需要使用特定的
rclcpp::NodeOptions
,确保你正确构造了rclcpp::NodeOptions
对象并传递给Node
构造函数。 -
重新编译并测试:
- 修改代码后,使用
colcon build
重新编译你的包。 - 确保没有其他编译错误。
- 修改代码后,使用
-
查阅文档:
- 如果不确定如何正确使用
rclcpp::Node
,查阅 ROS 2 文档 可能会有帮助。
- 如果不确定如何正确使用
通过上述步骤,你应该能够解决编译错误,并成功构建和运行你的 ROS 2 节点。如果问题仍然存在,请检查其他可能的代码错误或配置问题。
以上为生成回答,仅供参考~
- 接受节点名称和命名空间(以及可选的
-
@小伊 在 CMakeList.txt出错 中说:
你尝试在 test.cpp 的第5行调用这样一个
@小伊 在 CMakeList.txt出错 中说:
构造函数参数与 rc
但是在test.cpp文件下的不是rclcpp::Node类吗,这个不是只需要接受一个变量(即节点名)啊;而需要接受三个参数的是我在oop.cpp下自己创造的继承rclcpp::Node的PersonNode类啊,这二者有什么联系
-
从你的问题描述来看,似乎存在一个混淆点,即关于
rclcpp::Node
类的构造函数使用,以及你在oop.cpp
中自定义的PersonNode
类的构造函数之间的区别。这里有几个关键信息需要澄清和检查:-
rclcpp::Node 构造函数:
rclcpp::Node
是ROS 2中的一个核心类,用于创建和管理节点。它的基本构造函数通常只需要一个字符串参数,即节点的名称。例如:
或者如果你使用C++11之前的风格,可能会看到:auto node = rclcpp::Node::make_shared("my_node_name");
但请注意,在ROS 2中,更常见的是使用智能指针(rclcpp::Node node("my_node_name");
std::shared_ptr
)来管理节点,因为这样可以更好地管理生命周期。
-
自定义的PersonNode类:
- 你提到在
oop.cpp
中有一个继承自rclcpp::Node
的PersonNode
类,这个类的构造函数可能需要三个参数。这意味着你在PersonNode
的构造函数中除了节点名之外,还添加了其他两个参数。例如:class PersonNode : public rclcpp::Node { public: PersonNode(std::string node_name, int some_param, double another_param) : Node(node_name), some_member_var_(some_param), another_member_var_(another_param) {} private: int some_member_var_; double another_member_var_; };
- 你提到在
-
错误分析:
- 如果错误信息提示
test.cpp
的第5行有问题,并且提到了构造函数参数与rc
(可能是“related to constructor”的缩写或者部分信息丢失),那么很可能是在test.cpp
中,你尝试以错误的方式创建了一个rclcpp::Node
或PersonNode
的实例。 - 检查
test.cpp
的第5行,看看是否错误地尝试创建了一个PersonNode
实例而没有提供足够的参数,或者错误地使用了rclcpp::Node
的构造函数(比如尝试传递额外的不需要的参数给rclcpp::Node
)。
- 如果错误信息提示
-
解决方案:
- 确保在
test.cpp
中,如果你创建rclcpp::Node
实例,只传递节点名。 - 如果你创建
PersonNode
实例,确保传递所有必要的参数(包括节点名和其他自定义参数)。
- 确保在
-
代码审查:
- 回顾
test.cpp
和oop.cpp
的相关代码,确保构造函数的使用与类的定义相匹配。
- 回顾
希望这些信息能帮助你解决问题!如果问题仍然存在,可能需要更详细的代码片段来进一步分析。
以上为生成回答,仅供参考~
-
-
-
在解决您提到的关于使用
colcon build
构建 ROS 2 工作空间时只显示构建一个包而不是两个的问题时,我们首先需要确认几个关键点:-
CMakeLists.txt 的内容:确保您的两个包都正确设置了 CMake 配置。每个 ROS 2 包都应该有其自己的
CMakeLists.txt
文件,并且这些文件应该位于各自包的根目录下。检查这些文件是否包含正确的find_package()
和ament_package()
调用,以及任何必要的组件定义。 -
工作空间布局:ROS 2 工作空间通常包含一个
src
目录,所有的包都应该位于这个src
目录下。确保您的两个包都正确地放置在了工作空间的src
目录中。 -
智能指针问题:您提到的错误信息中提到了智能指针(
std::shared_ptr
),但这通常与编译错误相关,而不是构建过程中包的数量问题。不过,如果 CMake 配置中有语法错误或者某些依赖项未能正确解析(可能是因为智能指针相关的库或头文件未能正确找到),这可能会影响 CMake 的配置过程,进而影响到包的识别和构建。检查是否有编译错误或警告,并确保所有依赖都已正确安装和链接。 -
colcon build 的输出:当您运行
colcon build
时,它通常会列出它正在构建的所有包。如果只看到一个包被列出,这可能是因为:- 只有一个
CMakeLists.txt
文件被正确识别。 - 另一个包的
CMakeLists.txt
存在语法错误或其他问题,导致 CMake 无法处理它。 - 工作空间布局有误,导致
colcon
无法找到另一个包。
- 只有一个
-
检查环境变量:确保
COLCON_PREFIX_PATH
和AMENT_PREFIX_PATH
环境变量包含了正确的路径,这些变量帮助colcon
和ament
找到依赖的包。 -
清理和重新构建:有时候,旧的构建文件可能会导致问题。尝试删除
build
和install
目录(如果它们存在的话),然后再次运行colcon build
。
如果以上步骤仍然无法解决问题,您可以尝试以下操作来获取更多信息:
- 增加
colcon build
的详细输出,使用--packages-select <your_package>
来单独构建每个包,看看是否有特定的错误出现。 - 检查每个包的
CMakeLists.txt
和package.xml
文件,确保它们符合 ROS 2 的标准格式和要求。 - 在构建过程中查看 CMake 的输出和日志,寻找可能的错误或警告信息。
希望这些信息能帮助您解决问题!如果问题依旧存在,您可能需要提供更详细的错误信息和相关配置文件的内容。
以上为生成回答,仅供参考~
-