Undefined reference to ros::xx even after linking to ${catkin_LIBRARIES} [closed]
Hey everyone!
I'm pretty much used to ROS by now, but I got an error while catkin_make-ing a package that I can't really solve although it appears simple. I get all these errors while trying to link with the libraries:
CMakeFiles/data_input_output.dir/src/data_input_output.cpp.o: In function `main':
data_input_output.cpp:(.text+0x71): undefined reference to `ros::init(int&, char**, std::string const&, unsigned int)'
data_input_output.cpp:(.text+0xc5): undefined reference to `ros::NodeHandle::NodeHandle(std::string const&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
data_input_output.cpp:(.text+0x498): undefined reference to `ros::Rate::Rate(double)'
data_input_output.cpp:(.text+0x4ee): undefined reference to `ros::NodeHandle::getParam(std::string const&, int&) const'
data_input_output.cpp:(.text+0x54e): undefined reference to `ros::NodeHandle::getParam(std::string const&, double&) const'
data_input_output.cpp:(.text+0x56c): undefined reference to `ros::spinOnce()'
data_input_output.cpp:(.text+0x57a): undefined reference to `ros::Rate::sleep()'
data_input_output.cpp:(.text+0x586): undefined reference to `ros::ok()'
data_input_output.cpp:(.text+0x5af): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x5bd): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x5cb): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x5d9): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x5e7): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x5f5): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x600): undefined reference to `ros::NodeHandle::~NodeHandle()'
data_input_output.cpp:(.text+0x64c): undefined reference to `ros::NodeHandle::~NodeHandle()'
data_input_output.cpp:(.text+0x672): undefined reference to `ros::NodeHandle::~NodeHandle()'
data_input_output.cpp:(.text+0x69b): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x6cd): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x6f4): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x726): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x74d): undefined reference to `ros::Subscriber::~Subscriber()'
CMakeFiles/data_input_output.dir/src/data_input_output.cpp.o:data_input_output.cpp:(.text+0x77f): more undefined references to `ros::Subscriber::~Subscriber()' follow
CMakeFiles/data_input_output.dir/src/data_input_output.cpp.o: In function `main':
data_input_output.cpp:(.text+0x7ff): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x835): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x8aa): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x8bc): undefined reference to `ros::Publisher::~Publisher()'
data_input_output.cpp:(.text+0x8ce): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x8e0): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x8f2): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x904): undefined reference to `ros::Subscriber::~Subscriber()'
data_input_output.cpp:(.text+0x913): undefined reference to `ros::NodeHandle::~NodeHandle()'
and some more... The problem appears to be with the linker that doesn't identify the catkin libraries. However, inside my CMakeLists/txt I have the following:
add_executable(data_input_output src/data_input_output.cpp)
target_link_libraries(data_input_output ${catkin_LIBRARIES})
add_dependencies(data_input_output localization_gencpp)
add_library(field_model src/field_model.cpp) # adding the field_model as a library
target_link_libraries(field_model ${catkin_LIBRARIES})
add_dependencies(field_model localization_gencpp)
add_executable(particle_filter_localization src/particle_filter_localization.cpp)
target_link_libraries(particle_filter_localization ${catkin_LIBRARIES})
target_link_libraries(particle_filter_localization ${gsl_LIBRARIES} ${GSLCBLAS_LIBRARY})
target_link_libraries(particle_filter_localization field_model) #link ...
I had the same error. For me, the problem was the following missing line in my CMakeList.txt:
target_link_libraries(your_node_name ${catkin_LIBRARIES})
It seems that the catkin_create_pkg command, under indigo, doesn't add this line automatically.
you need to add roscpp as required component, see accepted answer. If you want
caktin_create_pkg
to do this for you you must add roscpp as dependency (catkin_create_pkg your_package_name roscpp std_msgs <other dependencies>
)