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

How do I link a library in cmake?

asked 2011-07-09 04:29:06 -0600

Anas gravatar image

updated 2014-01-28 17:10:01 -0600

ngrennan gravatar image


(excuse my english)

I am a new with ROS, I am trying to build a node for a driver, the problem is that I need to use an externe library that does the communication staff, how can tell the compiler to use this library in CMakeLists ? normally if I want to use this library I will do:

g++ -g -Wall main_foo.cpp other.cpp lib/dmclnx.a -Idmclnxlib -o run_foo

I have already read the tutorial for CMakeLists and Manifest . Thanks for help.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2011-07-09 07:36:25 -0600

Let's say you have a library called libfoo located in /usr/local/lib and you want to link it with a node you're building called my_node. Put the following in your CMakeLists.txt:

# set the path to the library folder

# link the libraries to the executable
target_link_libraries (my_node foo)

(/usr/local/lib is probably in your environment path already, so the first line might be unnecessary)

edit flag offensive delete link more


I included the lines suggested above and generated the lib in /usr/local/lib: `target_link_libraries(railDetect RailwayImageProcess) rosbuild_add_executable(railDetect src/main.cpp)` but i get this error for make . : `CLinking CXX executable bin/railDetect /usr/bin/ld: cannot find -lRailwayImageProcess collect2: ld returned 1 exit status make[2]: *** [bin/railDetect] Error 1 make[1]: *** [CMakeFiles/railDetect.dir/all] Error 2 make: *** [all] Error 2`

zweistein gravatar image zweistein  ( 2013-11-25 11:27:31 -0600 )edit

I think you first have to issue the add_executable directive and then link against any libraries.

bergercookie gravatar image bergercookie  ( 2016-04-05 07:32:03 -0600 )edit

Also, including the prefix (lib) or the suffix (.so/.a/.la) doesn't affect the link_directories or target_link_libraries commands.

bergercookie gravatar image bergercookie  ( 2016-04-05 09:28:29 -0600 )edit

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

Anas gravatar image

Thanks it's working

edit flag offensive delete link more

Question Tools


Asked: 2011-07-09 04:29:06 -0600

Seen: 81,509 times

Last updated: Jul 13 '11