boost.mpi给出了架构x86_64的未定义符号

Boost.MPI gives Undefined symbols for architecture x86_64

本文关键字:未定义 符号 x86 mpi boost      更新时间:2023-10-16

我正在尝试运行基本的"你好,世界!"示例:

#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

并使用mpicc,而clang++作为替代品。每种组合都会给出以下错误:

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都安装了。我可以确认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 %sn", numtasks,rank,hostname);

        // do some work with message passing 

   // done with MPI  
   MPI_Finalize();
   }

产生正确的输出。

我还验证了(至少一部分)Boost是通过编译和成功运行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;
}

with:

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的Boost版本。

  • 通常考虑http://www.boost.org/doc/libs/1_58_0/doc/html/mpi/mpi/getting_started.html
  • 特别是使用自制的人,您可以检查如何在Homebrew上使用MPI支持?