4

My project is mostly C and Fortran, but I had to use MPI from inside a C++ file. I don't want to use C++ wrapper nor link against libmpi_cxx.so, I use only the plain C interface. But just including mpi.h in my C++ file is enough for the linker to complain about missing references from libmpi_cxx.so:

h5pfc -g -lstdc++ *.o -o my_program
../bin/distance_to_wall.o: In function `MPI::Intracomm::Intracomm()':
/usr/lib/x86_64-linux-gnu/openmpi/include/openmpi/ompi/mpi/cxx/intracomm.h:25: undefined reference to `MPI::Comm::Comm()'
../bin/distance_to_wall.o: In function `MPI::Intracomm::Intracomm(ompi_communicator_t*)':
/usr/lib/x86_64-linux-gnu/openmpi/include/openmpi/ompi/mpi/cxx/intracomm_inln.h:23: undefined reference to `MPI::Comm::Comm()'
../bin/distance_to_wall.o: In function `MPI::Op::Init(void (*)(void const*, void*, int, MPI::Datatype const&), bool)':
/usr/lib/x86_64-linux-gnu/openmpi/include/openmpi/ompi/mpi/cxx/op_inln.h:121: undefined reference to `ompi_mpi_cxx_op_intercept'
../bin/distance_to_wall.o:(.data.rel.ro._ZTVN3MPI3WinE[_ZTVN3MPI3WinE]+0x48): undefined reference to `MPI::Win::Free()'
../bin/distance_to_wall.o:(.data.rel.ro._ZTVN3MPI8DatatypeE[_ZTVN3MPI8DatatypeE]+0x78): undefined reference to `MPI::Datatype::Free()'
collect2: error: ld returned 1 exit status

Adding -lmpi_cxx is enough to solve the problem, but this seems a case of paying for what I don't get (I do not use MPI C++ wrapper), nor it seems portable among different MPI implementations, because I have to explicitly list an OpenMPI dependency, which defeats the purpose of using the compiler wrapper in the first case.

Is there a MPI portable way to disable C++ interface upon including mpi.h on a C++ file?

lvella
  • 12,754
  • 11
  • 54
  • 106
  • 1
    Note the C++ bindings have been removed from the MPI standard a while ago. FWIW, the latest Open MPI does not build them by default. You might consider rebuilding Open MPI without the C++ bindings with `configure --disable-mpi-cxx ...` – Gilles Gouaillardet Oct 20 '18 at 00:17

1 Answers1

3

It is Open MPI specific, but you should be able to disable the C++ interface by defining the macro OMPI_SKIP_MPICXX.

See: https://github.com/open-mpi/ompi/blob/master/ompi/include/mpi.h.in#L2716

Zulan
  • 21,896
  • 6
  • 49
  • 109
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • And I agree (and the standard as well) that the C++ wrappers are basically useless. – Matthieu Brucher Oct 19 '18 at 20:55
  • Recent versions of CMake have `MPI_CXX_SKIP_MPICXX`, which I think it is used as `set(MPI_CXX_SKIP_MPICXX TRUE)` \\ `find_package(MPI REQUIRED)`. – alfC Feb 23 '23 at 07:51