Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

C language linkage: `extern "C"`

`extern "C"` specifies language linkage for interoperability between C and C++. It does not switch the compiler to C or guarantee binary compatibility on its own.

Version: ISO C++23; implementation support may vary

Syntax

extern "C" int cktis_answer(void);

extern "C" {
    typedef int cktis_answer_fn(void);
    int cktis_other_answer(void);
}

In C++23, extern "C" is a linkage specification. It can apply to one declaration or to a group of declarations in braces. Use it only at namespace scope; the braces do not create a scope. If specifications are nested, the innermost one applies. The default language linkage is C++.

A name's language linkage and a function type's language linkage are separate. Name linkage here concerns externally linked function and variable names and can affect how the linker sees their symbols. Type linkage can affect the calling convention and is part of the function type a callback pointer points to. The standard treats otherwise identical C and C++ function types as distinct. Not all compilers distinguish them, so do not rely on overloading based only on that difference.

Two functions with C linkage on their names cannot form an overload set under the same name; different parameter lists or namespaces do not make that possible. C++ name lookup still respects namespaces, but same-named C-linkage function declarations in different namespaces can denote the same function. A redeclaration without a linkage specification inherits the linkage; conflicting specifications for the same entity are ill-formed. No diagnostic is required if neither declaration is reachable from the other.

extern "C" does not switch to C mode, turn classes into C types, or change object layout. In a shared C/C++ header, guard the extern "C" syntax with #ifdef __cplusplus so C sees only C-compatible declarations. Use matching signatures and a compatible ABI (the rules for how binaries work together) on both sides. The specification alone does not guarantee an exact symbol spelling or export from a shared library.

At a C boundary, keep C++ exceptions on the C++ side: catch them there and report errors through, for example, a status code. If the interface passes pointers to data, specify who owns the data and how long the pointers remain valid.

Examples

#include <array>
#include <iostream>

namespace api {
extern "C" {
    typedef int cktis_answer_fn(void);
    int cktis_answer(void);
}

int cktis_answer(void) {
    const std::array<int, 2> parts{19, 23};
    return parts[0] + parts[1];
}
}

int main() {
    api::cktis_answer_fn* callback = &api::cktis_answer;
    std::cout << callback() << '\n';
}

Result

42

The definition of `api::cktis_answer` inherits C linkage and uses `std::array` in its body. `callback` points to the function `api::cktis_answer`. The typedef `api::cktis_answer_fn` names that function's C-linkage type. The expected output is `42`; this example does not link with a separately compiled C translation unit.

Related lessons

  • C++ · Lesson 56 · Advanced · Version: 3

Sources