Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Name Mangling and extern "C" in C++
Next article icon

Name Mangling and extern "C" in C++

Last Updated : 14 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ supports function overloading, i.e., there can be more than one function with the same name but, different parameters. How does the C++ compiler distinguish between different functions when it generates object code - it changes names by adding information about arguments. This technique of adding additional information to function names is called Name Mangling. C++ standard doesn't specify any particular technique for name mangling, so different compilers may append different information to function names. 

Consider the following example of Name Mangling, having the various declarations of function f(): 

CPP
// Function overloading in CPP to demonstrate // Name Mangling  int f(void) { return 1; }  int f(int) { return 0; }  void g(void) { int i = f(), j = f(0); } 


Some C++ compilers may mangle the above names to the following,  

CPP
// Function overloading to demonstrate // Name Mangling  int __f_v(void) { return 1; }  int __f_i(int) { return 0; }  void __g_v(void) { int i = __f_v(), j = __f_i(0); } 

Note: C does not support function overloading, So, when we link a C code in C++, we have to make sure that name of a symbol is not changed.

How to handle C symbols when linking from C++? 

In C, names may not be mangled as it doesn't support function overloading. So how to make sure that name of a symbol is not changed when we link a C code in C++. For example, see the following C++ program that uses printf() function of C. 
 

C++
// C Program to demonstrate it // doesn't support Name Mangling  int printf(const char* format, ...);  // Driver Code int main() {     printf("GeeksforGeeks");     return 0; } 

The above program generates an error.

Compiler Error:

In function `main':
f84cc4ebaf5b87bb8c6b97bc54245486.cpp:(.text+0xf): undefined reference to `printf(char const*, ...)'
collect2: error: ld returned 1 exit status

Explanation: The reason for compiler error is simple, the name of printf() is changed by the C++ compiler and it doesn't find the definition of the function with a new name.

Solution: Extern "C" in C++

When some code is put in the extern "C" block, the C++ compiler ensures that the function names are un-mangled - that the compiler emits a binary file with their names unchanged, as a C compiler would do.
If we change the above program to the following, the program works fine and prints "GeeksforGeeks" on the console(as shown below).
 

C++14
// CPP Program to demonstrate Extern "C"  extern "C" { int printf(const char* format, ...); }  // Driver Code int main() {     printf("GeeksforGeeks");     return 0; } 

Output
GeeksforGeeks

Therefore, all C style header files (stdio.h, string.h, etc) have their declarations in the extern "C" block.

CPP
#ifdef __cplusplus extern "C" { #endif // Declarations of this file #ifdef __cplusplus } #endif 


Following are the main points discussed above: 
1. Since C++ supports function overloading, additional information has to be added to function names (called Name mangling) to avoid conflicts in binary code. 
2. Function names may not be changed in C as it doesn't support function overloading. To avoid linking problems, C++ supports the extern "C" block. C++ compiler makes sure that names inside the extern "C" block are not changed.

 


Next Article
Name Mangling and extern "C" in C++

K

kartik
Improve
Article Tags :
  • C Language
  • C++
  • Extern "C"
Practice Tags :
  • CPP

Similar Reads

    Internal Linkage and External Linkage in C
    In C, linkage is a concept that describes how names/identifiers can or cannot refer to the same entity throughout the whole program or a single translation unit. The above sounds similar to scope, but it is not so. To understand what the above means, let us dig deeper into the compilation process.Be
    4 min read
    rename function in C
    The rename() function is used to rename a file in C. It changes the name of the file from old_name to new_name without modifying the content present in the file. It is defined inside <stdio.h> header file. In this article, we will learn how to rename a file using the rename() function in C pro
    2 min read
    Naming Convention in C++
    Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
    6 min read
    extern Keyword in C
    In C, the extern keyword is used to declare a variable or a function whose definition is present in some other file. Basically, it extends the visibility of the variables and functions in C to multiple source files.Before going deeper into the topic, we need to know about a few terms:Declaration: A
    4 min read
    namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
    We have introduced namespaces in below set 1.Namespace in C++ | Set 1 (Introduction) Defining a Namespace: A namespace definition begins with the keyword namespace followed by the namespace name as follows: namespace namespace_name {// code declarations i.e. variable (int a;)method (void add();)clas
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences