mercredi 15 décembre 2021

Boost Serialization Library in MEX

I am trying to convert my MEX/MATLAB/C++ program into solely C++. And the first big step is getting rid of saving files as mat file. I am saving various structs so from my understanding serialization is going to be involved. Boost looks to be the most easy to implement (to me anyway) but I am having trouble compiling. I found example code from codeguru and tried to implement that into a simple MEX function:

#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "mex.h"

using namespace std;

class Employee {
private:
    friend class boost::serialization::access;
    int id;
    string name;
    float salary;
    template<class Archive>
            void serialize(Archive &a, const unsigned version){
        a & id & name & salary;
    }
public:
    Employee(){}
    Employee(int i, string n, float s):id(i),name(n),salary(s)
    {}
};

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    const string filename = "emp.dat";
    Employee e1(11,"Harry",4500.00f);
    Employee e2(22,"Ravi",8800.00f);
    Employee e3(33,"Tim",6800.00f);
    Employee e4(44,"Rajiv",3400.00f);
    // Serialize and persist the object
    {
        std::ofstream outfile(filename);
        boost::archive::text_oarchive archive(outfile);
        archive << e1 << e2 << e3 << e4;
    }
}

When I try to compile I use:

mex -v COMPFLAGS='$COMPFLAGS /std:c++11' COPTIMFLAGS="-O3 -fwrapv -DNDEBUG" CFLAGS="$CFLAGS -fopenmp -march=native" -IC/boost_1_78_0 read_testing.cpp

And I get a long list of errors. The first few lines are:

Error using mex
/usr/bin/ld: /tmp/mex_30338390276068_4258/read_testing.o: in function `mexFunction':
read_testing.cpp:(.text+0x332): undefined reference to
`boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&,
unsigned int)'

I've never had to use boost before so there is a good chance I am missing something simple. It does compile if I comment out the serialization section at the end. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire