Ask Your Question
0

How to utilize libserial with ROS -Ubuntu 12.04 - Hydro

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

ajr_ gravatar image

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

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

EDIT 1:

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)
project(serial_interface)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)

include_directories(include ${catkin_INCLUDE_DIRS})

 add_message_files(
   FILES
   Control.msg
 )

generate_messages(
   DEPENDENCIES
   std_msgs
 )

catkin_package(
  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)

target_link_libraries(serial_interface
   ${catkin_LIBRARIES}
   libserial0
 )

 install(DIRECTORY include/${PROJECT_NAME}/
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
   FILES_MATCHING PATTERN "*.h"
   PATTERN ".svn" EXCLUDE
 )

And if i run dpkg -L libserial0

I do get the following:

/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libserial0
/usr/share/doc/libserial0/copyright
/usr/share/doc/libserial0/changelog.Debian.gz
/usr/lib
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0

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
2

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

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

Comments

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

ajr_ gravatar imageajr_ ( 2014-02-11 04:24:00 -0500 )edit

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

joq gravatar imagejoq ( 2014-02-11 04:43:34 -0500 )edit

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

ajr_ gravatar imageajr_ ( 2014-02-11 05:29:30 -0500 )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 imageajr_ ( 2014-02-11 05:32:41 -0500 )edit

That sounds like a new question.

ahendrix gravatar imageahendrix ( 2014-02-11 05:34:55 -0500 )edit

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

ajr_ gravatar imageajr_ ( 2014-02-11 05:47:34 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 1,752 times

Last updated: Feb 11 '14