Ask Your Question

How do I link a library in cmake?

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

Anas gravatar image

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

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 -0500

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 -0500 )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 -0500 )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 -0500 )edit

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

Anas gravatar image

Thanks it's working

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


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

Seen: 76,409 times

Last updated: Jul 13 '11