c++ - Boost.MPI 给出架构 x86_64 的 undefined symbol

标签 c++ boost mpi

我正在尝试运行基本的 "Hello, World!" example :

#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() << " of " << world.size()
            << "." << std::endl;
  return 0;
}

我已经尝试了多种运行该程序的变体:

mpic++ -I /usr/local/include/ test.cpp -o test -lboost_system

还有:

mpic++ -I /usr/local/include/boost test.cpp -o test -lboost_system

并使用 mpiccclang++ 作为替代。每种组合都会出现以下错误:

Undefined symbols for architecture x86_64:
  "boost::mpi::environment::environment(bool)", referenced from:
      _main in test-b0215f.o
  "boost::mpi::environment::~environment()", referenced from:
      _main in test-b0215f.o
  "boost::mpi::communicator::communicator()", referenced from:
      _main in test-b0215f.o
  "boost::mpi::communicator::rank() const", referenced from:
      _main in test-b0215f.o
  "boost::mpi::communicator::size() const", referenced from:
      _main in test-b0215f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Homebrew 说同时安装了 MPICH2 和 boost1.63.0。我可以通过编译然后运行 ​​this program: 来确认 mpic++ 运行

   // required MPI include file  
   #include "mpi.h"
   #include <stdio.h>

   int main(int argc, char *argv[]) {
   int  numtasks, rank, len, rc; 
   char hostname[MPI_MAX_PROCESSOR_NAME];

   // initialize MPI  
   MPI_Init(&argc,&argv);

   // get number of tasks 
   MPI_Comm_size(MPI_COMM_WORLD,&numtasks);

   // get my rank  
   MPI_Comm_rank(MPI_COMM_WORLD,&rank);

   // this one is obvious  
   MPI_Get_processor_name(hostname, &len);
   printf ("Number of tasks= %d My rank= %d Running on %s\n", numtasks,rank,hostname);


        // do some work with message passing 


   // done with MPI  
   MPI_Finalize();
   }

产生正确的输出。

我还通过编译并成功运行 Boost "Hello, World!" 验证了(至少部分)Boost 已安装

//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
  t.wait();

  std::cout << "Hello, world!" << std::endl;

  return 0;
}

与:

clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system

如何运行 Boost.MPI 示例?

最佳答案

当您从 boost 获得链接器错误时,您通常会忘记链接到 boost 库。

还有一个boost_mpi库,所以你应该编译

clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system -lboost_mpi

请注意,您需要支持 mpi 的升级版本。

关于c++ - Boost.MPI 给出架构 x86_64 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461714/

相关文章:

c++ - __DATE__ 宏的不同格式

c++ - 我无法在以下代码中打印 double 值?

c++ - cppcheck vs clang-tidy : explict constructor initializer_list

c++ - 不断收到此段错误 :11 error

c++ - 写入以 boost 共享内存

linux - 在树莓派上使用 libboost 进行编译 - 未定义对 `boost::system::system_category() 的引用

c - 为什么函数参数可以在C中包含地址或变量

python - 如何从 python 脚本执行 mpirun 或 mpiexec 命令?

c++ - 如何遍历 boost::mpl::list?

c - MPI Gatherv 不工作(消息被截断)