我已经在Ubuntu14.04上安装了Boost库,我正试图在catkin工作区中构建一个ros包。该软件包是一个无人机自主导航系统,由三个组件组成:其中一个组件允许创建云点的三维地图。我的目标是修改这个包,以便保存和加载我得到的云点。
我想使用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)发布于 2017-03-12 23:15:12
问题实际上在于您对C++的使用。在.cpp文件中有模板函数的实现。
下面的堆栈溢出问题解决了为什么这不起作用:Why can templates only be implemented in the header file?
https://stackoverflow.com/questions/42002515
复制相似问题