如何在我的项目包中使用c ++库gtsam?

时间:2017-06-06 17:03:03

标签: c++ compiler-errors cmake ros

这是我第一次使用除OpenCV之外的外部c ++库。在代码中我使用了很多ROS功能,但我相信我的问题与ROS无关。我想用GTSAM library构建一个项目。

因此,我将存储库克隆到我的/usr/lib文件夹中,并按照说明进行安装。 然后我编写了CMakeLists.txt,将两个包含添加到我的 - 运行正常的.h文件中 - 并尝试编译。 我的.h文件的开头,编译错误和我的CMakeLists.txt如下所示。有趣的是,如果我注释掉第二个包含并且只包括Pose2.h,则编译有效。这应该意味着编译器至少从库中找到一些头文件,因为它已正确安装。我的cmake代码中应该链接库的部分是从给定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....

编译时,出现以下错误:

...
[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

我的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}
)

2 个答案:

答案 0 :(得分:2)

尝试用{/ 1>替换## Declare a C++ library下的位

## Declare a C++ library
add_library(car_lib src/car_lib.cpp)
target_link_libraries(car_lib 
  gtsam
  ${catkin_LIBRARIES}
)

add_executable(car_node src/car_node)
target_link_libraries(car_node 
  car_lib
  gtsam
  ${catkin_LIBRARIES}
)

答案 1 :(得分:1)

对于那些尝试使用makefile做同样事情的人来说,这是解决方案。 [注意这是针对没有ROS的简单c ++程序]。

all: main.cpp
    @g++ main.cpp \
    -std=c++11 \
    -I /usr/include/eigen3 \
    -lboost_system -lboost_filesystem \
    -lgtsam \
    -o main
clear:
    @rm -rf main