【问题标题】:Unable to compile simple Boost MPI example无法编译简单的 Boost MPI 示例
【发布时间】:2017-03-26 21:47:24
【问题描述】:

我尝试使用以下代码将 MPI 与 C++ Boost 一起使用:

#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
namespace mpi = boost::mpi;

int main()
{
  mpi::environment env;
  mpi::communicator world;
  std::cout << "I am process " << world.rank() << "on " << world.size() << "." << std::endl;
  return 0;
}

我已经编译并安装了 boost mpi:

 ~ ls /usr/local/include/boost | grep mpi
mpi
mpi.hpp
~ ls /usr/local/lib | grep mpi   
libboost_mpi.a
libboost_mpi.so
libboost_mpi.so.1.62.0

 ~ ls /usr/local/lib | grep serialization                                                                                 
libboost_serialization.a
libboost_serialization.so
libboost_serialization.so.1.62.0
libboost_wserialization.a
libboost_wserialization.so
libboost_wserialization.so.1.62.0

编译使用

mpic++ -L/usr/local/lib -I/usr/local/include/boost/mpi -lboost_mpi-gcc-mt-1_35 -lboost_serialization MPIBoostBindingExample.cpp -o MPIBoostBindingExample

但仍然有错误提示:

/tmp/ccKVwnKR.o: In function `main':
MPIBoostBindingExample.cpp:(.text+0x27): undefined reference to `boost::mpi::environment::environment(bool)'
MPIBoostBindingExample.cpp:(.text+0x33): undefined reference to `boost::mpi::communicator::communicator()'
MPIBoostBindingExample.cpp:(.text+0x3f): undefined reference to `boost::mpi::communicator::size() const'
MPIBoostBindingExample.cpp:(.text+0x4d): undefined reference to `boost::mpi::communicator::rank() const'
MPIBoostBindingExample.cpp:(.text+0xb8): undefined reference to `boost::mpi::environment::~environment()'
MPIBoostBindingExample.cpp:(.text+0xeb): undefined reference to `boost::mpi::environment::~environment()'
collect2: error: ld returned 1 exit status

有什么帮助吗?

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    如果我只添加-lboost_mpi,对我有用(Ubuntu 16.04)。

    您的代码(模数小修改):

    edd@max:/tmp$ cat boostmpi.cpp 
    #include <boost/mpi/environment.hpp>
    #include <boost/mpi/communicator.hpp>
    #include <iostream>
    namespace mpi = boost::mpi;
    
    int main() {
      mpi::environment env;
      mpi::communicator world;
      std::cout << "I am process " << world.rank() 
                << " on " << world.size() << "." << std::endl;
      return 0;
    }
    edd@max:/tmp$ 
    

    我们只是使用上述库进行编译(默认情况下mpic++ 不知道)

    edd@max:/tmp$ mpic++ -o boostmpi boostmpi.cpp -lboost_mpi
    edd@max:/tmp$ orterun ./boostmpi
    I am process 2 on 4.
    I am process 3 on 4.
    I am process 0 on 4.
    I am process 1 on 4.
    edd@max:/tmp$ 
    

    这得益于 Boost 头文件和其他库具有“系统”状态这一事实,即可以通过额外的 -I-L 标志访问。

    【讨论】:

    • 它起作用了——这花了我一整天的时间来编译、删除和重新安装boost...也许我根本不适合成为软件开发人员...
    • 不要绝望,我们都经历过。关键是要从编译器的错误消息中学习......,或者,在这种情况下,链接器
    猜你喜欢
    • 2018-12-13
    • 2012-07-26
    • 2011-07-23
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多