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: Patrik Jonsson (code_at_[hidden])
Date: 2010-09-03 11:09:20

Hi everyone,

Thanks for the suggestions.

On Thu, Sep 2, 2010 at 6:41 AM, Jeff Squyres <jsquyres_at_[hidden]> wrote:
> 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.

Ah, I see. That seems unfortunate, but I guess it's out of your hands.

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

I tried the suggestion of simply including mpi.h in C++-mode before
including c-code.h, and that works. I should have thought of that.
(c-code.h still needs to include mpi.h because it's also a standalone
code that uses mpi.)

> 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" {
> #endif
> ...all your C declarations...
> #ifdef __cplusplus
> }
> #endif
> 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>).

This is also a good suggestions, but I have only scant control over
what's in c-code.h so it's a bit invasive.

In any case I can live with including mpi.h myself first, so I'll go
with that solution.