我在 MacOS 上安装了 OpenMPI 和 gcc。 OpenMPI 的 C 语法可以完美运行。但是,OpenMPI 的 c++ 语法始终无法识别。例如,void MPI::Init(int &argc, char**&argv);
行显示 use of undeclared identifier 'MPI'
错误。
谁能给出一些详细的步骤来解决这个问题?
#include "mpi.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
void MPI::Init(int &argc, char**&argv);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
//int Comm::Get_size() const
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
最佳答案
我在 Mac OS X 中使用自制软件安装 open-mpi 时遇到了同样的问题(我是来自 Debian/Ubuntu Linux 的 Mac OS X 新手)。幸运的是,我解决了它。正如 Zulan 所说,您的 open-mpi 安装是在没有 C++ 绑定(bind)的情况下构建的。但是,我认为最好的解决方案是直接从源代码重建它。我直接从它的主页下载了 .tar 文件,如果你仔细观察,你会发现默认情况下 C++ 绑定(bind)是禁用的(在 untar 文件夹中执行以下操作):
$ ./configure -help
--enable-mpi-cxx enable C++ MPI bindings (default: disabled)
所以,基本上我重建 open-mpi 直接添加该标志,如下所示:
$ ./configure --enable-mpi-cxx
然后,只需确保您知道安装它的位置(如果您使用了 --prefix= 或默认值)以正确地将它链接到您的 makefile。
有关此问题的更多信息,请查看此处:https://github.com/open-mpi/ompi/issues/3173 .
关于c++ - 在 MacOS 上对 OpenMPI 使用 C++ 语法时使用未声明的标识符 'MPI',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680530/