0
$\begingroup$

Rosanswers logo

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? Anyway i still receive this error while trying to run catkin_make.

####
#### 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
[  0%] [ 20%] Built target std_msgs_generate_messages_lisp
Built target serial_interface_generate_messages_cpp
[ 40%] [ 80%] Built target serial_interface_generate_messages_lisp
Built target serial_interface_generate_messages_py
Linking CXX executable /home/x/Dropbox/catkin_ws/devel/lib/serial_interface/serial_interface
[ 80%] Built target serial_interface_generate_messages
/usr/bin/ld: cannot find -llibserial0
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
Invoking "make" failed

Originally posted by ajr_ on ROS Answers with karma: 97 on 2014-02-10

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

"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)

Originally posted by ahendrix with karma: 47576 on 2014-02-10

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by ajr_ on 2014-02-11:
I updated my question and it seems I do not have the correct library name which to use for linking?

Comment by joq on 2014-02-11:
Leave off the lib prefix. You probably want -lserial, so put serial in your target_link_libraries(), as @ahendrix suggested.

Comment by ajr_ on 2014-02-11:
ok this works, although there is another error now but its not related to this question anymore.

Comment by ajr_ on 2014-02-11:
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'

Comment by ahendrix on 2014-02-11:
That sounds like a new question.

Comment by ajr_ on 2014-02-11:
Ok, actually missing the main function & node. Beginners mistake... This problem is solved, thanks to all for the help!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.