3.4 Including your own C header files from C++

To include your own C header files, you must wrap the #include directive in an extern "C" statement.

You can do this in the following ways:

  • when you #include the file, as shown in the following example:

    // C++ code
    extern "C" {
    #include "my-header1.h"
    #include "my-header2.h"
    int main()
        // ...
        return 0;
  • by adding the extern "C" statement to the header file, as shown in the following example:

    /* C header file */
    #ifdef __cplusplus    /* Insert start of extern C construct */
    extern "C" {
    /* Body of header file */
    #ifdef __cplusplus  /* Insert end of extern C construct. */
    }                   /* The C header file can now be      */
    #endif              /* included in either C or C++ code. */
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.