3.11 Calls to C from C++

You can call a C function from C++ by declaring it with extern "C".

Defining the function in C

The following example is a C function that is to be called from C++:

struct S {
    int i;
};
void cfunc(struct S *p) {
/* the definition of the C function to be called from C++ */
    p->i += 5;
}

Calling a C function from C++

To call this C function from C++, declare it with extern "C":

struct S {            // has no base classes 
                      // or virtual functions
    S(int s) : i(s) { }
    int i;
};
// declare the C function to be called from C++
extern "C" void cfunc(S *); 
int f(){
    S s(2);           // initialize 's'
    cfunc(&s);        // call 'cfunc' 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.