错误:编译使用MPI的共享库时,架构x86_64的未定义符号

时间:2017-10-25 19:40:49

标签: c++ compiler-errors mpi

我想编译一个使用mpi的库。编译下面给出的一个非常简单的例子时,我遇到了一个错误:

#ifndef __MPIWRAPPER_HPP__
#define __MPIWRAPPER_HPP__

#include <ostream>
#include "mpi.h"

class MPIWrapper
{
   public:

   MPIWrapper(MPI_Comm comunicator);
   void sayhello();

   private:

   int rank_;
   int n_processes_;
};
#endif

源文件

#include "MPIWrapper.hpp"
#include "mpi.h"
#include <iostream>
#include <cstdlib>

MPIWrapper::MPIWrapper(MPI_Comm comunicator){
   MPI_Comm_size(comunicator, &n_processes_);   
   MPI_Comm_rank(comunicator, &rank_); 
}

void MPIWrapper::sayhello(){
   std::cout << "Hello, World! " << "I am process "<< rank_ <<" of "<< 
   n_processes_<<"\n";
}

和CMakeList.txt

cmake_minimum_required(VERSION 3.2)
PROJECT( mpi_wrapper )
set(CMAKE_BUILD_TYPE release)

find_package(MPI REQUIRED)

ADD_LIBRARY( wrapper SHARED MPIWrapper.cpp)
TARGET_INCLUDE_DIRECTORIES( wrapper PUBLIC "${MPI_CXX_INCLUDE_PATH}")
TARGET_LINK_LIBRARIES( wrapper "${MPI_CXX_LIBRARIES_PATH}")

当我尝试编译代码时,我的链接器出错了。我想知道问题是否与我的编译器有关,或者我是否缺少我需要指定的任何属性。我在MAC OS X上使用gnu编译器4.9和mpich-3.2

运行cmake 3.2

和make

的输出
Scanning dependencies of target wrapper
[100%] Building CXX object CMakeFiles/wrapper.dir/MPIWrapper.cpp.o
Linking CXX shared library libwrapper.dylib
Undefined symbols for architecture x86_64:
  "_MPI_Comm_rank", referenced from:
      MPIWrapper::MPIWrapper(int) in MPIWrapper.cpp.o
      MPIWrapper::MPIWrapper(int) in MPIWrapper.cpp.o
  "_MPI_Comm_size", referenced from:
      MPIWrapper::MPIWrapper(int) in MPIWrapper.cpp.o
      MPIWrapper::MPIWrapper(int) in MPIWrapper.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [libwrapper.dylib] Error 1
make[1]: *** [CMakeFiles/wrapper.dir/all] Error 2
make: *** [all] Error 2

我感谢任何帮助。 感谢

0 个答案:

没有答案