c++serializationboostmpiboost-mpi

How to send string type with boost_mpi?


I am trying to pass more complex data types using boost mpi. I am implementing the sample codes in http://theboostcpplibraries.com/boost.mpi-simple-data-exchange

First I try to send a string as an array of chars which works from the aforementioned tutorial Example 47.5. The code is:

#include <boost/mpi.hpp>
#include <iostream>

int main(int argc, char *argv[])
{
  boost::mpi::environment env{argc, argv};
  boost::mpi::communicator world;
  if (world.rank() == 0)
  {
    char buffer[14];
    world.recv(boost::mpi::any_source, 16, buffer, 13);
    buffer[13] = '\0';
    std::cout << buffer << '\n';
  }
  else
  {
    const char *c = "Hello, world!";
    world.send(0, 16, c, 13);
  }
}

I could compile and run it fine with the following commands:

mpic++ -std=c++0x 3.cpp -o 3 -lboost_mpi

mpiexec -np 3 ./3

Then, I tried to change the type to string (from same tutorial Example 47.5):

#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <string>
#include <iostream>

int main(int argc, char *argv[])
{
  boost::mpi::environment env{argc, argv};
  boost::mpi::communicator world;
  if (world.rank() == 0)
  {
    std::string s;
    world.recv(boost::mpi::any_source, 16, s);
    std::cout << s << '\n';
  }
  else
  {
    std::string s = "Hello, world!";
    world.send(0, 16, s);
  }
}

When I compile and link this code, I got the following error:

> /usr/bin/ld: /tmp/ccRNu1AY.o: undefined reference to symbol '_ZTIN5boost7archive6detail14basic_iarchiveE'
> //usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.54.0: error adding symbols: DSO missing from command line
> collect2: error: ld returned 1 exit status

Any help would be greatly appreciated.


Solution

  • You could add the compiler option:

    -lboost_serialization