Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: Re: [OMPI users] Problem including C MPI code from C++ using C linkage
From: Jeff Squyres (jsquyres_at_[hidden])
Date: 2010-09-02 08:41:31

On Aug 31, 2010, at 5:39 PM, Patrik Jonsson wrote:

> It seems a bit presumptuous of mpi.h to just include mpicxx.h just
> because __cplusplus is defined, since that makes it impossible to link
> C MPI code from C++.

The MPI standard requires that <mpi.h> work in both C and C++ applications. It also requires that <mpi.h> include all the C++ binding prototypes when relevant. Hence, there's not much we can do here.

> I've had to resort to something like
> #ifdef __cplusplus
> #undef __cplusplus
> #include <mpi.h>
> #define __cplusplus
> #else
> #include <mpi.h>
> #endif

As you noted, that doesn't seem like a good idea.

> in c-code.h, which seems to work but isn't exactly smooth. Is there
> another way around this, or has linking C MPI code with C++ never come
> up before?

Just to be clear: this isn't a linking issue; it's a compiling issue.

As Lisandro noted, it's probably best to separate <mpi.h> outside of your <c-code.h> file.

Or, you can make your <c-code.h> file be safe for C++ by doing something like in c-code.h:

#include <mpi.h>

#ifdef __cplusplus
#extern "C" {
...all your C declarations...
#ifdef __cplusplus

This is probably preferable because then your <c-code.h> is safe for both C and C++, and you keep <mpi.h> contained inside it (assumedly preserving some abstraction barriers in your code by keeping the MPI prototypes bundled with <c-code.h>).

Jeff Squyres
For corporate legal information go to: