프로그래밍/C++ 문법

What is extern C and What is function name mangling?

studylida 2023. 1. 27. 21:00

Introduction

C++ is a popular programming language that is widely used in various fields, such as game development, finance, and robotics. One of the unique features of C++ is its ability to interact with C code, which is made possible through the use of extern C and function name mangling. In this article, we will explore what extern C and function name mangling are, and how they are used in C++ programming.

What is function name mangling?

Function name mangling is the process of modifying function names to include information about their parameters, return type, and other characteristics. This information is used to distinguish them from other functions with the same name. The purpose of function name mangling is to ensure that functions with the same name but different parameter lists can be distinguished from one another.

Function name mangling works by adding special characters or prefixes to the function name, which include information about the function's parameters, return type, and other characteristics. The mangling scheme used by the compiler is implementation-defined, which means that different compilers may use different methods of mangling.

For example, in C++, the function int add(int a, int b) will be mangled to something like _Z3addii by the compiler, where _Z3 is a prefix indicating a C++ function and addii is the name of the function with the argument types int and int appended.

 

How different compilers mangle the same functions

There isn't a standardized scheme by which even trivial C++ identifiers are mangled, and consequently different compilers (or even different versions of the same compiler, or the same compiler on different platforms) mangle public symbols in radically different (and thus totally incompatible) ways. Consider how different C++ compilers mangle the same functions:

Compiler void h(int) void h(int, char) void h(void)
Intel C++ 8.0 for Linux _Z1hi _Z1hic _Z1hv
HP aC++ A.05.55 IA-64
IAR EWARM C++
GCC 3.x and higher
Clang 1.x and higher[3]
GCC 2.9.x h__Fi h__Fic h__Fv
HP aC++ A.03.45 PA-RISC
Microsoft Visual C++ v6-v10 (mangling details) ?h@@YAXH@Z ?h@@YAXHD@Z ?h@@YAXXZ
Digital Mars C++
Borland C++ v3.1 @h$qi @h$qizc @h$qv
OpenVMS C++ v6.5 (ARM mode) H__XI H__XIC H__XV
OpenVMS C++ v6.5 (ANSI mode)   CXX$__7H__FIC26CDH77 CXX$__7H__FV2CB06E8
OpenVMS C++ X7.1 IA-64 CXX$_Z1HI2DSQ26A CXX$_Z1HIC2NP3LI4 CXX$_Z1HV0BCA19V
SunPro CC __1cBh6Fi_v_ __1cBh6Fic_v_ __1cBh6F_v_
Tru64 C++ v6.5 (ARM mode) h__Xi h__Xic h__Xv
Tru64 C++ v6.5 (ANSI mode) __7h__Fi __7h__Fic __7h__Fv
Watcom C++ 10.6 W?h$n(i)v W?h$n(ia)v W?h$n()v

What is extern C?

Extern C is a keyword used in C++ to indicate that the following function should use C-style linkage. This means that the function's name will not be mangled, and it can be called from C code without any issues. Extern C is used to prevent function name mangling by indicating to the compiler that the function should use C-style linkage.

For example, the following C++ function:

extern "C" int add(int a, int b) {
    return a + b;
}

Will be stored in the object file with the name add, and can be called from C code as:

int result = add(1, 2);

Why use extern C?

The primary purpose of using extern C is to allow C++ code to call functions written in C or to link with C libraries. This is particularly useful when working with legacy C code or libraries that have not been updated to work with C++.

Extern C can also be used to link C++ projects with C libraries. By wrapping the C library in C++ code, it can be made more user-friendly and easier to use. This is particularly useful when working with complex C libraries that have a large number of functions and parameters.

Advantages and disadvantages of using extern C

The main advantage of using extern C is that it allows C++ code to call functions written in C or to link with C libraries, which can be particularly useful when working with legacy code or libraries that have not been updated to work with C++. Additionally, extern C can be used to wrap C libraries in C++ code, making them more user-friendly and easier to use.

However, there are also some disadvantages to using extern C. One of the main disadvantages is that C++ features such as function overloading, templates, and exception handling are not available when using extern C. Additionally, the use of extern C can make the code more difficult to maintain, as it can be difficult to distinguish between C and C++ functions.

Conclusion

In conclusion, function name mangling and extern C are important features of C++ that allow it to interact with C code. Function name mangling is the process of modifying function names to include information about their parameters, return type, and other characteristics, while extern C is a keyword used to indicate that the following function should use C-style linkage, preventing function name mangling. While extern C has its advantages, it is important to consider the potential disadvantages as well, such as the loss of C++ features and difficulty in maintaining code.

In any case, understanding how extern C and function name mangling work is important for any C++ programmer working with legacy code or C libraries.

 

관련된 글

 

참고문헌