How to use extern “C” in C++

The extern “C” keyword is a special keyword that ensures compatibility between C and C++. How exactly does extern “C” work, and how to use it is what we will discuss in this tutorial.


Understanding Name Mangling

Before showing any examples on extern “C”, we need to explain the concept of name mangling and C Linkage first.

Name mangling is a concept used by C++ and other languages that support Function Overloading. As you all know, that means that there can be multiple functions with the same name, as long as they have different parameters.

The need for Name mangling arises because C++ has overloading of functions while C does not. The C++ compiler cannot just use the function name as a unique id to link to, because multiple functions could have the same name. But at a same time, it needs a unique name to address the function.

As a solution, it mangles the name by adding information about the arguments, known as Name mangling. This always results in a unique name, as the number and type arguments can never be the same.

A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.

So to summarize,

extern "C" makes a function-name in C++ have C linkage (compiler does not mangle the name) so that client C code can link to (use) your function using a C compatible header file that contains just the declaration of your function. 


Using extern “C” to prevent Name Mangling

Let’s take a look at how we can use extern “C” to prevent name mangling from occurring with C++ functions.

In this example, we are going to try and use a function from C in a C++ program. We won’t be doing it the normal way however, which is to use the #include <stdio.h> statement. We are going to define the function declaration ourselves. (Just the function declaration, not the definition)

The reason this we can actually do this, is because at the end of they day, <stdio.h> is just a header file which contains a bunch of function declarations (the name of the function + the parameters is the function declaration).

These function declarations link back to the function definitions. So if we just provide the function declaration ourselves, then there won’t be any problems. Let’s try and provide the function definition for printf() function from C, and see what happens. (Remember that we can use C functions and libraries in C++)

// CPP Program 
int printf(const char* format, ...);

int main()  {
    printf("Hello World");
    return 0;
}
error: undefined reference to `printf(char const*, ...)'

As you can see, the above code returns an error. Why though? It’s not because we made the wrong declaration, rather the problem is, that this is a C function. Our C++ program is performing name mangling on the C function declaration, which is causing an error.

Luckily, we can easily fix this using the extern “C” keyword as shown below.

extern "C" {
int printf(const char* format, ...);
}
 
int main()  {
    printf("Hello World");
    return 0;
}
Hello World

The reason for why this is working, is because extern “C” prevents name mangling from taking place on any functions within it’s curly brackets. Due to this, the function is called correctly.

You may have wondered though, why you don’t have to do this normally when including the header file. That’s because the extern “C” is included within the library, in which all the functions are declared. So you don’t have to do it on your end.


This marks the end of the extern “C” in C++ Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments