3.13 Calls to C++ from C

You can call a C++ function from C by defining it with extern "C" and declaring it as extern in C.

Defining the C++ function to be called from C

To call a C++ function from C, define the C++ function with extern "C":

struct S {        // has no base classes or virtual functions
    S(int s) : i(s) { }
    int i;
};
extern "C" void cppfunc(S *p) {    
// Definition of the C++ function to be called from C.
// The function is written in C++, only the linkage is C.
    p->i += 5;
} 

Declaring and calling the function in C

In C, declare the C++ function with extern:

struct S {
    int i;
};
/* Declaration of the C++ function to be called from C */
extern void cppfunc(struct S *p);
int f(void) {
    struct S s;
    s.i = 2;                /* initialize 's' */
    cppfunc(&s);            /* call 'cppfunc' so it */
                            /* can change 's' */
    return s.i * 3;
}
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.