4

I am having a compile-time issue which I have reduced to the following test case. I wish to call a C++ routine from fortran and have the C++ routine be MPI aware.

Consider the following sample code,

Fortran main:

! -- main.f90
program main
   implicit none
   external return_three
   integer return_three

   write(*,*) return_three()
end program main

C++ subroutine:

// -- subs.cpp
#include <mpi.h>

extern "C"
{
   int return_three_();
}

int return_three_()
{
   return 3;
}

Note that, for the problem to reproduce, I only need to include mpi.h.

Compiling with GCC 5.3 and OpenMPI 1.10.1 (I checked GCC 4.8 and PGI 15.10 too) gives the following problem during linking:

% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main subs.o main.o -lstdc++ -lgcc_s
subs.o: In function `MPI::Intracomm::Intracomm()':
subs.cpp:(.text._ZN3MPI9IntracommC2Ev[_ZN3MPI9IntracommC5Ev]+0x14): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Intracomm::Intracomm(ompi_communicator_t*)':
subs.cpp:(.text._ZN3MPI9IntracommC2EP19ompi_communicator_t[_ZN3MPI9IntracommC5EP19ompi_communicator_t]+0x19): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Op::Init(void (*)(void const*, void*, int, MPI::Datatype const&), bool)':
subs.cpp:(.text._ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb[_ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb]+0x24): undefined reference to `ompi_mpi_cxx_op_intercept'
subs.o:(.rodata._ZTVN3MPI3WinE[_ZTVN3MPI3WinE]+0x48): undefined reference to `MPI::Win::Free()'
subs.o:(.rodata._ZTVN3MPI8DatatypeE[_ZTVN3MPI8DatatypeE]+0x78): undefined reference to `MPI::Datatype::Free()'
collect2: error: ld returned 1 exit status

It seems to me like mpifort is missing some C++-related libraries. It's my understanding that mpifort should be used to compile a fortran main program, though. The problem doesn't occur with Intel 16.0 compiled against OpenMPI 1.10.1.

My questions are:

  • What's going on here? Why is Intel able to handle this sample code and PGI/GCC is not?
  • Is there a portable way to include C++ subroutines with MPI in a fortran code?
  • (if possible) Is there an easy way to fix my current problem? I'm trying to compile a package on my machine, so it'd be best if I could just add -lmagicfix or something.
0

1 Answer 1

6

I was able to compile your code with GCC 5.3.0 and openMPI 1.10.2 by adding -lmpi_cxx in the final step:

% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main main.o subs.o -lstdc++ -lmpi_cxx

The reason is that the openMPI wrapper compilers mpifort and mpic++ link to different MPI libraries. You can check this with the -showme:libs option:

% mpifort -showme:libs
mpi_usempif08 mpi_usempi_ignore_tkr mpi_mpifh mpi
% mpic++ -showme:libs
mpi_cxx mpi

So in order to use the C++ MPI library, you have to tell mpifort explicitly to link to it.

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be exactly what I'm looking for, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.