Open MPI logo

Open MPI User's Mailing List Archives

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

Subject: [OMPI users] Problem including C MPI code from C++ using C linkage
From: Patrik Jonsson (code_at_[hidden])
Date: 2010-08-31 17:39:16


Hi all,

I'm have a C MPI code that I need to link into my C++ code. As usual,
from my C++ code, I do

extern "C" {
#include "c-code.h"
}

where c-code.h includes, among other things, mpi.h.

This doesn't work, because it appears mpi.h tries to detect whether
it's being compiled as C or C++ and includes mpicxx.h if the language
is C++. The problem is that that doesn't work in C linkage, so the
compilation dies with errors like:

mpic++  -I. -I$HOME/include/libPJutil -I$HOME/code/arepo -m32
arepotest.cc -I$HOME/include -I/sw/include -L/sw/lib
-L$HOME/code/arepo -larepo -lhdf5  -lgsl -lgmp -lmpi
In file included from /usr/include/c++/4.2.1/map:65,
                from /sw/include/openmpi/ompi/mpi/cxx/mpicxx.h:36,
                from /sw/include/mpi.h:1886,
                from /Users/patrik/code/arepo/allvars.h:23,
                from /Users/patrik/code/arepo/proto.h:2,
                from arepo_grid.h:36,
                from arepotest.cc:3:
/usr/include/c++/4.2.1/bits/stl_tree.h:134: error: template with C linkage
/usr/include/c++/4.2.1/bits/stl_tree.h:145: error: declaration of C
function 'const std::_Rb_tree_node_base* std::_Rb_tree_increment(const
std::_Rb_tree_node_base*)' conflicts with
/usr/include/c++/4.2.1/bits/stl_tree.h:142: error: previous
declaration 'std::_Rb_tree_node_base*
std::_Rb_tree_increment(std::_Rb_tree_node_base*)' here
/usr/include/c++/4.2.1/bits/stl_tree.h:151: error: declaration of C
function 'const std::_Rb_tree_node_base* std::_Rb_tree_decrement(const
std::_Rb_tree_node_base*)' conflicts with
/usr/include/c++/4.2.1/bits/stl_tree.h:148: error: previous
declaration 'std::_Rb_tree_node_base*
std::_Rb_tree_decrement(std::_Rb_tree_node_base*)' here
/usr/include/c++/4.2.1/bits/stl_tree.h:153: error: template with C linkage
/usr/include/c++/4.2.1/bits/stl_tree.h:223: error: template with C linkage
/usr/include/c++/4.2.1/bits/stl_tree.h:298: error: template with C linkage
/usr/include/c++/4.2.1/bits/stl_tree.h:304: error: template with C linkage
/usr/include/c++/4.2.1/bits/stl_tree.h:329: error: template with C linkage
etc. etc.

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++.

I've had to resort to something like

#ifdef __cplusplus
#undef __cplusplus
#include <mpi.h>
#define __cplusplus
#else
#include <mpi.h>
#endif

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?

Thanks,

/Patrik Jonsson