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

a problem of linking external libraries

asked 2011-09-23 05:10:44 -0600

HZ gravatar image

Hi All,

I encountered a problem while linking a ROS program against some external libraries. I am trying to link my ROS program against Xenomai libraries. To make sure Xenomai libraries have been successfully installed in my OS (Ubuntu 10.10), I built a simple program (non-ROS) with gcc like this:

gcc -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -Wall -pipe -lnative -L/usr/xenomai/lib -lxenomai -lpthread -lrtdk hellworld.c -o helloworld

No problems arise here, and I got the executable.

To build my ROS programs (take as an example the tutorial program at the ROS website ), I add the following commands to CMakeLists.txt:

rosbuild_add_executable(listener src/listener.cpp)
include_directories(/usr/xenomai/include)
add_definitions(-D_GNU_SOURCE -D_REENTRANT -D__XENO__)
rosbuild_add_compile_flags(listener -Wall -pipe)
link_directories(/usr/xenomai/lib)
target_link_libraries(listener native xenomai pthread rtdk)

That says, I want to link the “listener” node against some Xenomai libraries, like “native”, “xenomai”, etc. However, when I rosmake’d the whole package, I got this error:

  Linking CXX executable ../bin/listener
  /usr/bin/ld: cannot find -lnative
  /usr/bin/ld: cannot find -lxenomai
  collect2: ld returned 1 exit status

It seems rosbuild did not search libraries in “/usr/xenomai/lib” which is supposed to specified by link_directories(/usr/xenomai/lib). Did I miss something to tell rosbuild to search libraries in “/usr/xenomai/lib”?

With many thanks,

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2011-09-23 10:44:08 -0600

HZ gravatar image

After some googling, I found this. I think I can now answer the problem myself: It turned out that I need to place link_directories(/usr/xenomai/lib) before rosbuild_add_executable(listener src/listener.cpp), that is,

link_directories(/usr/xenomai/lib)
rosbuild_add_executable(listener src/listener.cpp)
include_directories(/usr/xenomai/include)
add_definitions(-D_GNU_SOURCE -D_REENTRANT -D__XENO__)
rosbuild_add_compile_flags(listener -Wall -pipe)
target_link_libraries(listener native xenomai pthread rtdk)

Is this a bug, or a restriction of CMake? Is it documented somewhere?

edit flag offensive delete link more

Comments

/usr/xenomai/lib is not a default location to look for libraries, so you have to add it to the search path.
tfoote gravatar image tfoote  ( 2011-09-25 17:07:34 -0600 )edit
In the problematic CMakeLists.txt, I tried to add "/usr/xenomai/lib" to the search path by adding "link_directories(/usr/xenomai/lib)". However, I added this command after "rosbuild_add_executable(listener src/listener.cpp)", and it turned out that I need to reverse the order of these two commands.
HZ gravatar image HZ  ( 2011-09-26 04:02:08 -0600 )edit
Is this requirement of CMake documented somewhere? I came from gcc, and the order should not cause a compilation error in gcc.
HZ gravatar image HZ  ( 2011-09-26 04:02:51 -0600 )edit

I am going to use one external library (VISION - a private lib) with my ROS package (which already uses opencv). Shall I use the external library, by just including the above lines? Here I didn't understand the last three lines - add_definitions, rosbuild_add_compile_flags and target_link_libraries?

Sudhan gravatar image Sudhan  ( 2013-05-06 05:35:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2011-09-23 05:10:44 -0600

Seen: 4,289 times

Last updated: Sep 23 '11