OpenMPI: error: ‘MPI’ has not been declared

Viewed 2689

Always get the following errors while running the OpenMPI command "mpicxx hello_cxx.cc -o hello"

hello_cxx.cc: In function ‘int main(int, char**)’: 
hello_cxx.cc:25:5: error: ‘MPI’ has not been declared
     MPI::Init();
     ^~~ hello_cxx.cc:26:12: error: ‘MPI’ has not been declared
     rank = MPI::COMM_WORLD.Get_rank();
            ^~~ hello_cxx.cc:27:12: error: ‘MPI’ has not been declared
     size = MPI::COMM_WORLD.Get_size();
            ^~~ hello_cxx.cc:31:5: error: ‘MPI’ has not been declared
     MPI::Finalize();
     ^~~
2 Answers

Your application is using MPI C++ bindings. Keep in mind C++ bindings have been removed from the MPI standard.

From an Open MPI point of view, C++ bindings are still here, but they are no more built be default since Open MPI 2.0.0.

So if you are using Open MPI 2.0.0 or later, you first need to make sure C++ bindings were built. If not, you need to rebuild Open MPI, and configure with the --enable-mpi-cxx option.

On the long term, you should really move away from MPI C++ bindings. You can either use plain C bindings, or explore alternate C++ bindings such as the popular Boost:MPI library.

Related