How to use the gtsam library in my ROS package?

asked 2017-06-04 10:17:14 -0500

CocoJambo gravatar image

updated 2017-06-06 09:18:28 -0500

Hello,

I am currently revisiting ROS as I want to build localization with the GTSAM library. It is important to mention that this is the first time I used an external library aside from OpenCV.

I cloned the repository into my /usr/lib folder and installed it as instructed. I then added two includes into my - otherwise functioning .h file - and tried to compile. The beginning of my .h file, the compile error and my CMakeLists.txt are shown below. Interestingly, if I comment out the second include and just include Pose2.h, compilation works. That should mean that the compiler at least finds some headers from the library, ergo it is correctly installed. The part of my cmake code which is supposed to link the library is extracted from an example project given here.

car_lib.h:

#ifndef CAR_LIB_H
#define CAR_LIB_H

// GTSAM headers
#include <gtsam/geometry/Pose2.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
using namespace gtsam;

// rest of file follows....

When compiling, I get the following error:

...
[100%] Linking CXX executable /home/marc/catkin_ws/devel/lib/car/car_node
[100%] Linking CXX shared library /home/marc/catkin_ws/devel/lib/libcar_lib.so
[100%] Built target car_lib
CMakeFiles/car_node.dir/src/car_node.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/local/include/gtsam/inference/Key.h:41: undefined reference to `gtsam::_defaultKeyFormatter[abi:cxx11](unsigned long)'
/usr/local/include/gtsam/inference/Key.h:52: undefined reference to `gtsam::_multirobotKeyFormatter[abi:cxx11](unsigned long)'
collect2: error: ld returned 1 exit status
car/CMakeFiles/car_node.dir/build.make:113: recipe for target '/home/marc/catkin_ws/devel/lib/car/car_node' failed
make[2]: *** [/home/marc/catkin_ws/devel/lib/car/car_node] Error 1
CMakeFiles/Makefile2:384: recipe for target 'car/CMakeFiles/car_node.dir/all' failed
make[1]: *** [car/CMakeFiles/car_node.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

My CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(car)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  message_generation
  nav_msgs
  roscpp
  sensor_msgs
  std_msgs
)

###### GTSAM STUFF STARTS HERE

# Include GTSAM CMake tools
find_package(GTSAMCMakeTools)
#include(GtsamBuildTypes) # Load build type flags and default to Debug mode
#include(GtsamTesting)    # Easy functions for creating unit tests and scripts
#include(GtsamMatlabWrap) # Automatic MATLAB wrapper generation

# Ensure that local folder is searched before library folders
#include_directories(BEFORE "${PROJECT_SOURCE_DIR}")

###################################################################################
# Find GTSAM components
find_package(GTSAM REQUIRED) # Uses installed package
include_directories(${GTSAM_INCLUDE_DIR})

###################################################################################
# Build static library from common sources
#set(CONVENIENCE_LIB_NAME ${PROJECT_NAME})
#add_library(${CONVENIENCE_LIB_NAME} STATIC include/car/car_lib.h src/car_lib.cpp)
#target_link_libraries(${CONVENIENCE_LIB_NAME} gtsam)

###### GTSAM STUFF ENDS HER

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES car_lib
  CATKIN_DEPENDS
  geometry_msgs
  message_runtime
  nav_msgs
  roscpp
  sensor_msgs
  std_msgs
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
add_library(car_lib
  include/${PROJECT_NAME}/car_lib.h
  src/car_lib.cpp
)

add_executable(car_node src/car_node)
target_link_libraries(car_node ${catkin_LIBRARIES})
target_link_libraries(car_lib
  ${catkin_LIBRARIES}
)
edit retag flag offensive close merge delete

Comments

fatal error: Eigen/Dense: No such file or directory

this is not a problem with 'libraries', but with your compiler not being able to find Eigen. For starters I would add ${catkin_INCLUDE_DIRS} to your include_directories(..) statement. And make CMake find Eigen.

gvdhoorn gravatar imagegvdhoorn ( 2017-06-04 11:14:34 -0500 )edit

Oops. Did not copy that code snippet. ${catkin_INCLUDE_DIRS} was part of my CMakeLists.txt. How do I go about finding Eigen? The library has local copies of third party libraries, including Eigen. I have the folder /usr/lib/gtsam/gtsam/3rdparty . How can I make CMake find all of this?

CocoJambo gravatar imageCocoJambo ( 2017-06-04 11:35:28 -0500 )edit

maybe I installed the library in the wrong folder? I notice that - while coding - my IDE can not find the imports from it.

CocoJambo gravatar imageCocoJambo ( 2017-06-04 12:17:39 -0500 )edit

Edit: I severely changed the original text and incorporated more info and a specific case.

CocoJambo gravatar imageCocoJambo ( 2017-06-06 09:16:05 -0500 )edit

did you made it work? I have the same problem

dalis gravatar imagedalis ( 2017-10-02 13:42:42 -0500 )edit

Yes, see here.

CocoJambo gravatar imageCocoJambo ( 2017-10-02 15:10:31 -0500 )edit