Calls to C++ from C

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

Example 21. Defining the C++ function to be called from 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;
} 

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

Example 22. Declaring and calling the function in C

struct S {
    int i;
}; extern void cppfunc(struct S *p);
/* Declaration of the C++ function to be called from C */
int f(void) {
    struct S s;
    s.i = 2;                /* initialize 's' */
    cppfunc(&s);            /* call 'cppfunc' so it */
                            /* can change 's' */
    return s.i * 3;
}

Show/hideSee also

Copyright © 2010-2012 ARM. All rights reserved.ARM DUI 0471G
Non-ConfidentialID021412