Undefined reference to ros::init on Jade [closed]
I'm a freshman on ROS. And recently I come up with a problem when I learn the Tutorials(WritingPublisherSubscriber(c++)).
When I add the talker.cpp to the project, It appear I have problem with Undefined reference
Here is my talker.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
return 0;
}
The CMakeLists.txt file has following content
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation)
## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)
## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)
## Declare a catkin package
catkin_package()
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
and Package.xml is here
<?xml version="1.0"?>
<package>
<name>beginner_tutorials</name>
<version>0.0.0</version>
<description>The beginner_tutorials package</description>
<maintainer email="lizhuo@todo.todo">lizhuo</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
<run_depend>roscpp</run_depend>
<run_depend>rospy</run_depend>
<run_depend>std_msgs</run_depend>
</package>
While compiling the above code using catkin_make, the error is
Base path: /home/lizhuo/catkin_lz
Source space: /home/lizhuo/catkin_lz/src
Build space: /home/lizhuo/catkin_lz/build
Devel space: /home/lizhuo/catkin_lz/devel
Install space: /home/lizhuo/catkin_lz/install
####
#### Running command: "make cmake_check_build_system" in "/home/lizhuo/catkin_lz/build"
####
####
#### Running command: "make -j4 -l4" in "/home/lizhuo/catkin_lz/build"
####
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] [ 0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
Built target std_msgs_generate_messages_eus
[ 0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 33%] Built target beginner_tutorials_generate_messages_py
[ 50%] [ 66%] Built target beginner_tutorials_generate_messages_cpp
Built target beginner_tutorials_generate_messages_lisp
[ 91%] Built target beginner_tutorials_generate_messages_eus
[ 91%] Linking CXX executable ../devel/lib/beginner_tutorials/talker
Built target beginner_tutorials_generate_messages
CMakeFiles/talker.dir/src/talker.cpp.o:in the function ‘main’:
talker.cpp:(.text+0x55):对‘ros::init(int&, char**, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)’未定义的引用 (it means undefined reference)
collect2: error:ld return 1
beginner_tutorials/CMakeFiles/talker.dir/build.make:102: recipe for target 'devel/lib/beginner_tutorials/talker' failed
make[2]: *** [devel/lib/beginner_tutorials/talker] Error 1
CMakeFiles/Makefile2:1091: recipe for target 'beginner_tutorials/CMakeFiles/talker.dir/all' failed
make[1]: *** [beginner_tutorials/CMakeFiles/talker.dir/all] Error 2
Makefile:117: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
I look though many answers, but cannot solve my problem. So I really need help. Thanks!
I don't know if it will work but you can try to write
<ros/ros.h>
instead of"ros/ros.h"
I have try it. But it doesn't work. @Ifr
Why do you have your
add_dependencies
aftertarget_link_libraries
?Just follow the tutorial. The problem is discovered, that the version of GCC is changed from 4.9 to 5. When I switch the version back to 4.9, It works! @alecive