GeneralPurpose-LinkageMismatch

Do not include C header files into C++ without ‘extern “C”’

Required inputs: IR

Declarations included in C and C++ (without extern "C") may have incompatible meanings, i.e. functions have different calling conventions.
Bad code (mixed linkage):
// foo.h
int foo(int);  // ERROR: This declaration has different meanings in C and C++
               // (name mangling used only when included from C++)

// foo.cpp
#include "foo.h"

// foo.c
#include "foo.h"
Good code:
// foo.h
#ifdef __cplusplus
extern "C" {
#endif
int foo(int);  // OK: extern "C" ensures that this declaration has the same meaning
               // in C and C++
#ifdef __cplusplus
}
#endif
Good code:
// foo.h
int foo(int);

// foo.cpp
extern "C" {
    // OK: extern "C" ensures that this declaration has the same meaning in C and C++
    #include "foo.h"
}

// foo.c
#include "foo.h"

Possible Messages

Key

Text

Severity

Disabled

linkage_mismatch

This declaration has different meanings in C and C++ (name mangling used only when included from C++. Did you forget to add ‘extern “C”’ when including C headers into C++?

None

False

Options