Boost,ros和catkin - 链接器错误

时间:2017-02-02 12:39:14

标签: c++ boost ubuntu-14.04 ros catkin

我在Ubuntu 14.04上安装了Boost库,我试图在catkin工作区中构建一个ros包。 该软件包是用于无人机自主导航的系统,由三个组件组成:其中一个组件允许创建云点的3D地图。 我的目标是修改此包,以便保存和加载我得到的云点。

我想使用Boost库来序列化包含3D地图的数据结构。

这是我的地图数据结构(我添加了序列化功能):

#include <vector>
#include <TooN/se3.h>
#include <cvd/image.h>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

struct MapPoint;
struct KeyFrame;

struct Map
{
  Map();
  inline bool IsGood() {return bGood;}
  void Reset();

  void MoveBadPointsToTrash();
  void EmptyTrash();

  template<class Archive>
  void serialize(Archive & ar, const unsigned int version);

  std::vector<MapPoint*> vpPoints;
  std::vector<MapPoint*> vpPointsTrash;
  std::vector<KeyFrame*> vpKeyFrames;

  bool bGood;
};

这是我对函数序列化的实现:

template<class Archive>
void Map::serialize(Archive & ar, const unsigned int version) {
    ar & vpPoints;
    ar & vpPointsTrash;
    ar & vpKeyFrames;
}

序列化由另一个处理ros主题命令的文件(PTAMwrapper.cpp)调用:

//..
Map *mpMap; 
//..

bool PTAMWrapper::handleCommand(std::string s)
    {
        if(s.length() == 4 && s.substr(0,4) == "save")
            {
                //save into file
                const char *path="/home/user/Desktop/aGreatMap";
                std::ofstream file(path); //open in constructor
                // save data to archive
                {
                    Map tMap = *mpMap;
                    boost::archive::text_oarchive oa(file);
                    // write class instance to archive
                    oa << tMap;
                    // archive and stream closed when destructors are called
                }
            }

    //...

当我发出catkin_make时,操作失败,给我错误:

Linking CXX executable /home/user/catkin_ws/devel/lib/tum_ardrone/drone_stateestimation
CMakeFiles/drone_stateestimation.dir/src/stateestimation/PTAMWrapper.cpp.o: in function "serialize<boost::archive::text_oarchive, Map>":
/usr/local/include/boost/serialization/access.hpp:116: undefined reference to "void Map::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)"
CMakeFiles/drone_stateestimation.dir/src/stateestimation/PTAMWrapper.cpp.o: in function "void boost::serialization::throw_exception<boost::archive::archive_exception>(boost::archive::archive_exception const&)":
/usr/local/include/boost/serialization/throw_exception.hpp:36: undefined reference to "boost::archive::archive_exception::archive_exception(boost::archive::archive_exception const&)"
collect2: error: ld returned 1 exit status
make[2]: *** [/home/user/catkin_ws/devel/lib/tum_ardrone/drone_stateestimation] Error 1
make[1]: *** [tum_ardrone/CMakeFiles/drone_stateestimation.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

我认为这是一个链接问题。这是正确的?有什么建议可以解决吗?

编辑:这是我的CMakeLists.txt文件:

//...
find_package(catkin REQUIRED COMPONENTS
  ardrone_autonomy
  cv_bridge
  dynamic_reconfigure
  geometry_msgs
  sensor_msgs
  std_msgs
  std_srvs
  message_generation
  roscpp
  rospy
)

find_package(Boost COMPONENTS serialization REQUIRED)

//...

add_executable(drone_stateestimation ${STATEESTIMATION_SOURCE_FILES} ${STATEESTIMATION_HEADER_FILES})
set_target_properties(drone_stateestimation PROPERTIES COMPILE_FLAGS "-D_LINUX -D_REENTRANT -Wall  -O3 -march=nocona -msse3") 
target_link_libraries(drone_stateestimation ${PTAM_LIBRARIES} ${catkin_LIBRARIES} ${Boost_SERIALIZATION_LIBRARY})
add_dependencies(drone_stateestimation thirdparty ${PROJECT_NAME}_gencpp ${PROJECT_NAME}_gencfg)

//...
add_executable(drone_autopilot ${AUTOPILOT_SOURCE_FILES} ${AUTOPILOT_HEADER_FILES})
target_link_libraries(drone_autopilot ${catkin_LIBRARIES} ${Boost_SERIALIZATION_LIBRARY})
add_dependencies(drone_autopilot thirdparty ${PROJECT_NAME}_gencpp ${PROJECT_NAME}_gencfg)

//...
add_executable(drone_gui ${GUI_SOURCE_FILES} ${GUI_RESOURCE_FILES_CPP} ${GUI_UI_FILES_HPP} ${GUI_HEADER_FILES_HPP})
target_link_libraries(drone_gui ${QT_LIBRARIES} cvd ${catkin_LIBRARIES} ${Boost_SERIALIZATION_LIBRARY})
add_dependencies(drone_gui thirdparty ${PROJECT_NAME}_gencpp ${PROJECT_NAME}_gencfg)

1 个答案:

答案 0 :(得分:0)

问题实际上在于你使用C ++。您可以在.cpp文件中实现模板功能。

以下stackoverflow问题解决了为什么这不起作用: Why can templates only be implemented in the header file?