[petsc-dev] what does extern "C" mean on a variable?

Aron Ahmadia aron at ahmadia.net
Wed Sep 17 23:12:54 CDT 2014


On Wed, Sep 17, 2014 at 11:44 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:

> There is no c++ mangling with variables (or is there) so what does the
> extern “C” buy us? or mean?


You use extern "C" to indicate that something was defined in a C
compilation unit (source file compiled using a C compiler).  Compilers are
usually pretty generous (I'm looking at you GCC) in letting you access
symbols from C compilation units without the extern "C", but it is still
undefined behavior to access any C symbols (variables, functions, etc...)
from a C++ compilation unit without it.

Variables can be mangled if, for example, they are declared within a
namespace.  If that header is included from within a namespace declaration
in a C++ source file and is not properly escaped, you could end up with a
locally scoped namespace reference to a PETSc variable, which would break
all sorts of things.

A
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20140918/8842e87b/attachment.html>


More information about the petsc-dev mailing list