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_directories(/usr/local/lib)
# 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)
Originally posted by Ivan Dryanovski with karma: 4954 on 2011-07-09
This answer was ACCEPTED on the original site
Post score: 11
Original comments
Comment by zweistein on 2013-11-25:
I included the lines suggested above and generated the RailwayImageProcess.so 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
Comment by bergercookie on 2016-04-05:
I think you first have to issue the add_executable directive and then link against any libraries.
Comment by bergercookie on 2016-04-05:
Also, including the prefix (lib) or the suffix (.so/.a/.la) doesn't affect the link_directories or target_link_libraries commands.