ROS 节点里调用自定义库类 无法启动构造函数现象及解决方法
程序员文章站
2024-03-26 11:24:35
...
前提是想模仿move_base_node的写法,单独写节点,然后在节点中定义自定义类对象实现功能
#include <move_base/move_base.h>
int main(int argc, char** argv){
ros::init(argc, argv, "move_base_node");
tf::TransformListener tf(ros::Duration(10));
move_base::MoveBase move_base( tf );
//ros::MultiThreadedSpinner s;
ros::spin();
return(0);
}
然后写了个测试
test.h
#include <ros/ros.h>
class Test{
public:
Test();
~Test();
};
test.cpp
#include <test/test.h>
Test::Test(){
ROS_INFO("hello");
}
Test::~Test(){
}
test_node.cpp
#include <test/test.h>
int main(int argc, char** argv){
ros::init(argc, argv, "test");
Test test();
ros::spin();
return 0;
}
cmakelist.txt
add_library(TEST
include/test/test.h
src/test.cpp
)
add_dependencies(TEST ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(TEST
${catkin_LIBRARIES}
)
add_executable(test_node
src/test_node.cpp
)
add_dependencies(test_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(test_node TEST
${catkin_LIBRARIES}
)
set_target_properties(test_node PROPERTIES OUTPUT_NAME test)
编译通过后运行rosrun test test后无法输出构造函数中的hello,修改cmakelist等无果
后来想move_base里定义对象时有输入tf作为实参,尝试了下,竟然成功了
##test.h
#include <ros/ros.h>
#include <tf/transform_listener.h>
class Test{
public:
Test(tf::TransformListener& tf);
~Test();
tf::TransformListener& tf_;
};
#test.cpp
#include <test/test.h>
Test::Test(tf::TransformListener& tf):
tf_(tf)
{
ros::NodeHandle nh;
ros::NodeHandle private_nh("~");
ROS_INFO("hello");
}
Test::~Test(){
}
##test_node.cpp
#include <test/test.h>
int main(int argc, char** argv){
ros::init(argc, argv, "test");
tf::TransformListener tf(ros::Duration(10));
Test test(tf);
ros::spin();
return 0;
}
原因暂时未知,如果大神,万望指点一二。
上一篇: C++构造函数相关……