ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to utilize libserial with ROS -Ubuntu 12.04 - Hydro

asked 2014-02-10 08:06:35 -0600

ajr_ gravatar image

updated 2014-02-11 04:23:18 -0600

I have an old ROS Fuerte project which code i wanted to reuse in an another ROS Hydro node, this code uses libserial library. I have a problem to find out the correct way to include this library into my new node via CMakeList.txt

Here what i include in my header:

#include <SerialStream.h>
using namespace LibSerial;
SerialStream fd;

I'm not totally sure where the error comes from but doesn't the "undefined reference" mean that problem could be within the compiling and linking?

Here is the error code:

#### Running command: "make cmake_check_build_system" in "/home/x/Dropbox/catkin_ws/build"
#### Running command: "make -j2 -l2" in "/home/x/Dropbox/catkin_ws/build"
[  0%] [  0%] Built target std_msgs_generate_messages_cpp
Built target std_msgs_generate_messages_py
[ 20%] [ 20%] Built target std_msgs_generate_messages_lisp
Built target serial_interface_generate_messages_cpp
[ 60%] [ 80%] Built target serial_interface_generate_messages_py
Built target serial_interface_generate_messages_lisp
[ 80%] Built target serial_interface_generate_messages
Linking CXX executable /home/x/Dropbox/catkin_ws/devel/lib/serial_interface/serial_interface
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o: In function `CA::RoverInterface::RoverInterface(ros::NodeHandle)':
serial_interface.cpp:(.text+0x514): undefined reference to `LibSerial::SerialStream::Open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x48): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x50): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, long)'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x58): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x68): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x70): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, long)'
CMakeFiles/serial_interface.dir/src/serial_interface.cpp.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x78): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make[2]: *** [/home/x/Dropbox/catkin_ws/devel/lib/serial_interface/serial_interface] Error 1
make[1]: *** [serial_interface/CMakeFiles/serial_interface.dir/all] Error 2
make: *** [all] Error 2


I have updated my CMakeList.txt file to be as follows with the new information I got from an answer:

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS

include_directories(include ${catkin_INCLUDE_DIRS})



  INCLUDE_DIRS include
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime

add_executable(serial_interface src/serial_interface.cpp)
add_dependencies(serial_interface serial_interface_generate_messages_cpp)


 install(DIRECTORY include/${PROJECT_NAME}/

And if i run dpkg -L libserial0

I do get the following:


Therefore I should have the correct library installed on my system ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-02-10 08:23:10 -0600

ahendrix gravatar image

"Undefined reference" usually means that you're compiling against the headers for a library, but aren't linking to that library during your final link phase.

In CMake, you usually link an executable to a library using the target_link_libraries() command.

Depending on if or how you're finding the libserial package from CMake, you'll want to do different things.

If you're using find_package to find libserial, you'll want something like:

target_link_libraries(my_node ${libserial_LIBRARIES})

But if you're just assuming that the system has libserial installed, and you normally pass -lserial to link to it, you probably want to do:

target_link_libraries(my_node serial)
edit flag offensive delete link more


I updated my question and it seems I do not have the correct library name which to use for linking?

ajr_ gravatar image ajr_  ( 2014-02-11 04:24:00 -0600 )edit

Leave off the ``lib`` prefix. You probably want ``-lserial``, so put ``serial`` in your target_link_libraries(), as @ahendrix suggested.

joq gravatar image joq  ( 2014-02-11 04:43:34 -0600 )edit

ok this works, although there is another error now but its not related to this question anymore.

ajr_ gravatar image ajr_  ( 2014-02-11 05:29:30 -0600 )edit

the new problem is this error, not sure if its still related to linking: /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main'

ajr_ gravatar image ajr_  ( 2014-02-11 05:32:41 -0600 )edit

That sounds like a new question.

ahendrix gravatar image ahendrix  ( 2014-02-11 05:34:55 -0600 )edit

Ok, actually missing the main function & node. Beginners mistake... This problem is solved, thanks to all for the help!

ajr_ gravatar image ajr_  ( 2014-02-11 05:47:34 -0600 )edit

Question Tools



Asked: 2014-02-10 08:06:35 -0600

Seen: 2,869 times

Last updated: Feb 11 '14