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

时间:2016-05-31 17:05:39

标签: c++ serialization boost mpi 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] = '\0';
    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);
  }
}

当我编译并链接此代码时,我收到以下错误:

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以添加编译器选项:

-lboost_serialization