0

I have the following example

#include <iostream>
#include <mpi.h>

int main(int argc, char** argv){
    int process_Rank, size_Of_Cluster;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size_Of_Cluster);
    MPI_Comm_rank(MPI_COMM_WORLD, &process_Rank);

    for(int i = 0; i < size_Of_Cluster; i++){
        if(i == process_Rank){
      std::cout << "Hello World from process " << process_Rank<< " of " <<
        size_Of_Cluster << std::endl;
        }
        MPI_Barrier(MPI_COMM_WORLD);
    }
    
    MPI_Finalize();
    return 0;
}

When I execute <some_dir>/mpic++ parallel_ex.cpp it runs fine.

However, if I execute <some_dir>/mpic++ -show

I am getting:

g++ -Wl,-Bsymbolic-functions -Wl,-z,relro -I/usr/include/x86_64-linux-gnu/mpich -L/usr/lib/x86_64-linux-gnu -lmpichcxx -lmpich

and now If I compile like this

g++ -Wl,-Bsymbolic-functions -Wl,-z,relro -I/usr/include/x86_64-linux-gnu/mpich -L/usr/lib/x86_64-linux-gnu -lmpichcxx -lmpich parallel_ex.cpp

I am getting:

parallel_ex.cpp:(.text+0x31): undefined reference to `MPI_Init' /usr/bin/ld: parallel_ex.cpp:(.text+0x42): undefined reference to `MPI_Comm_size' /usr/bin/ld: parallel_ex.cpp:(.text+0x53): undefined reference to `MPI_Comm_rank' /usr/bin/ld: parallel_ex.cpp:(.text+0xcb): undefined reference to `MPI_Barrier' /usr/bin/ld: parallel_ex.cpp:(.text+0xd6): undefined reference to `MPI_Finalize' collect2: error: ld returned 1 exit status

Why is this happening? It doesn't make sense, does it?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Angelos
  • 169
  • 10
  • 1
    you compile `myexample.cpp` but the error message is about `parallel_ex.cpp`. are you sure about your command and logs? – Gilles Gouaillardet Jan 28 '21 at 08:55
  • 3
    The order of options is important. Source files (and libraries) that use libraries must be listed before those libraries, or else the library will be discarded. [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – Yksisarvinen Jan 28 '21 at 09:01
  • @GillesGouaillardet I am editing it – Angelos Jan 28 '21 at 09:02

0 Answers0