如何用boost_mpi发送字符串类型?

How to send string type with boost_mpi?

我正在尝试使用 boost mpi 传递更复杂的数据类型。我正在 http://theboostcpplibraries.com/boost.mpi-simple-data-exchange

中实现示例代码

首先,我尝试将字符串作为字符数组发送,这在上述教程示例 47.5 中有效。代码是:

#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] = '[=11=]';
    std::cout << buffer << '\n';
  }
  else
  {
    const char *c = "Hello, world!";
    world.send(0, 16, c, 13);
  }
}

我可以编译 运行 使用以下命令就可以了:

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

mpiexec -np 3 ./3

然后,我尝试将类型更改为字符串(来自同一教程示例 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);
  }
}

当我编译 link 这段代码时,我得到了以下错误:

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

如有任何帮助,我们将不胜感激。

您可以添加编译器选项:

-lboost_serialization