当使用cmake进行构建时,会出现对`boost :: archive :: archive_exception :: .....的未定义引用

时间:2019-07-12 01:04:27

标签: c++ c++11 serialization boost cmake

我一直在尝试编写一个简单的程序,该程序简单地存储向量和整数的结构,然后将其读回。代码如下所示:

#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <vector>

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

using namespace std;
struct  testS
{
    int num1;
    vector<int> num2;

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & num1;
        ar & num2;

    }
};

int main()
{

    std::queue<std::string> files;

    for(int i = 0;i<50;i++){
        testS t1 = testS();
        t1.num1=i;
        for(int k=0;k<10;k++){
            t1.num2.push_back(k);
        }
        std::string fileName = std::to_string(i)+".test";
        std::ofstream tempSaveFile(fileName);
        boost::archive::text_oarchive oa(tempSaveFile);
        oa<<t1;
        files.push(fileName);
    }

    for (int i=0;i<50;i++){
        std::string curRecName =files.front();
        std::cout<<"file name: "<<curRecName<<std::endl;

        std::ifstream input_file(curRecName);
        testS curRec;
        boost::archive::text_iarchive ia(input_file);
        ia>>curRec;
        for(int k=0;k<10;k++){
            std::cout<<curRec.num2[k];
        }
        std::cout<<'\n';
        std::remove(&curRecName[0]);
        files.pop();
    }
}

但是,由于我的CMakeLists.txt出现问题,以上代码甚至无法编译

cmake_minimum_required(VERSION 3.0.0)
project(RecorderTest)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -pthread -lboost_serialization" ) #you can set with add_definitions("-Wall -std=c+11, etc")
set(Boost_INCLUDE_DIR /home/lowlimb/Downloads/boost_1_70_0)
set(Boost_LIBRARY_DIR /home/lowlimb/Downloads/boost_1_70_0/stage/lib)
find_package(Boost 1.70.0 COMPONENTS system filesystem REQUIRED)

include_directories(
    include
    src
    ${Boost_INCLUDE_DIR}
)
add_library(
    recLib 
    include/Recorder.hpp
    include/Recorder.cpp

)
link_directories(${Boost_LIBRARY_DIR})

enable_testing()
add_executable(rec_o src/main.cpp )

target_link_libraries(rec_o PRIVATE recLib ${Boost_LIBRARIES})

我得到的结果是

CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:6 (find_package)


CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:6 (find_package)


-- Boost version: 1.70.0
-- Found the following Boost libraries:
--   system
--   filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lowlimb/cdrive/UCLA/lab/Exoskeleton/Controller/Test/TestPtr/Final/build
[ 25%] Building CXX object CMakeFiles/recLib.dir/include/Recorder.cpp.o
[ 50%] Linking CXX static library librecLib.a
[ 50%] Built target recLib
[ 75%] Building CXX object CMakeFiles/rec_o.dir/src/main.cpp.o
[100%] Linking CXX executable rec_o
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int)':
main.cpp:(.text._ZN5boost7archive13text_oarchiveC2ERSoj[_ZN5boost7archive13text_oarchiveC5ERSoj]+0x25): undefined reference to `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&, unsigned int)'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_iarchive::text_iarchive(std::istream&, unsigned int)':
main.cpp:(.text._ZN5boost7archive13text_iarchiveC2ERSij[_ZN5boost7archive13text_iarchiveC5ERSij]+0x25): undefined reference to `boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::text_iarchive_impl(std::istream&, unsigned int)'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()':
main.cpp:(.text._ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED2Ev[_ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED5Ev]+0x32): undefined reference to `boost::archive::basic_text_oprimitive<std::ostream>::~basic_text_oprimitive()'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::~text_iarchive_impl()':

这只是输出的一部分,其余部分的模式相似。我想知道我在CMakeLists.txt中是否犯了任何错误?我可以确保路径是100%正确的,那里没有错字。

1 个答案:

答案 0 :(得分:0)

您的find_package语句缺少必需组件中的boost :: serialization:

find_package(Boost 1.70.0 COMPONENTS system filesystem serialization REQUIRED)