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

How to include a library in CMakeLists

asked 2016-07-17 13:18:31 -0600

Cerin gravatar image

What's the proper way to include external libraries in a C++ ROS package?

I've written a C++ program that reads data from an I2C device using the I2Cdevlib, and now I'm trying to re-package it as a ROS node and build it via catkin.

I'm building the non-catkin code via:

gcc -o mpu6050_reader ${PATH_I2CDEVLIB}RaspberryPi_bcm2835/MPU6050/examples/MPU6050_example_1.cpp \
 -I ${PATH_I2CDEVLIB}RaspberryPi_bcm2835/I2Cdev ${PATH_I2CDEVLIB}RaspberryPi_bcm2835/I2Cdev/I2Cdev.cpp \
 -I ${PATH_I2CDEVLIB}Arduino/MPU6050/ ${PATH_I2CDEVLIB}Arduino/MPU6050/MPU6050.cpp -l bcm2835 -l m

I've initialized a C++ ROS package, and refactored MPU6050_example_1.cpp into the ROS-equivalent mpu6050_node.cpp, and now I'm trying to translate the above call to CMakeLists.txt so it compiles with catkin_make. What's the best way to do this?

Should I include the i2cdevlib and bcm2835 libraries in my package, or require the user to download and install them separately?

I've read through the documentation on CMakeLists.txt, but it's unclear to me which parameters I should use. Should I use add_library() or include_directories() or add_dependencies() or target_link_libraries()?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
9

answered 2016-07-18 05:53:27 -0600

cagatay gravatar image

updated 2016-07-18 06:08:00 -0600

compile your i2c library similar to

add_library(mylib src/mylib.cpp)

then compile your node then link your library to your node

add_executable(mynode src/mynode.cpp)
target_link_libraries(mynode mylib)

If it is an external library

find_package( PkgConfig REQUIRED)
pkg_check_modules( mylib REQUIRED mylib )

or

FIND_LIBRARY(mylib_LIBRARIES mylib /path/to/lib)

then link the library to your node

add_executable(mynode src/mynode.cpp)
target_link_libraries(mynode ${mylib_LIBRARIES})

hope this helps

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2016-07-17 13:18:31 -0600

Seen: 13,631 times

Last updated: Jul 18 '16