升压、ros 和 catkin - 链接器错误

Boost, ros and catkin - linker error

本文关键字:链接 错误 catkin ros 升压      更新时间:2023-10-16

我已经在 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;
}

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

//..
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)

问题实际上在于您对C++的使用。您在.cpp文件中实现了模板函数。

以下堆栈溢出问题解决了为什么这不起作用:为什么模板只能在头文件中实现?