Mutual reference between C and C ++ functions-an in-depth understanding of extern "C"

Source: Internet
Author: User
Mutual reference between C and C ++ functions extern "C" for an in-depth understanding

1. Introduction

C ++ language was originally created as "a better C ", however, this does not mean that the compilation and connection methods used by global variables and functions similar to C in C ++ are the same as those in C. As a language to be compatible with C, C ++ retains the features of some procedural languages (known as "not completely oriented objects "), therefore, it can define global variables and functions that do not belong to any class. However, C ++ is an object-oriented programming language after all. To support function overloading, C ++ treats global functions differently from C.

2. Start with the standard header file
An enterprise once gave the following interview question: why do standard header files have a structure similar to the following?

# Ifndef _ incvxworksh
# DEFINE _ incvxworksh
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*...*/
# Ifdef _ cplusplus
}
# Endif
# Endif/* _ incvxworksh */

Obviously, the compilation macro "# ifndef _ incvxworksh, # DEFINE _ incvxworksh, and # endif" in the header file is used to prevent the header file from being repeatedly referenced. So

# Ifdef _ cplusplus
Extern "C "{
# Endif
# Ifdef _ cplusplus
}
# Endif

What is the role? We will discuss it one by one below.

3. Deep encryption extern "C"

Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C. Let's explain these two meanings in detail.

The function or variable specified by extern "C" is of the extern type;

Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statements:

Extern int;

It is just a declaration of a variable. It does not define variable A and does not allocate memory space for variable. Variable A can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.

In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls a function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function in the target code generated by module A during the connection phase.

The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C.

Variables and functions modified by extern "C" are compiled and connected in C language;

Compilation Method without the extern "C" declaration

First, let's take a look at how C-like functions are compiled in C ++.

As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is:

Void Foo (int x, int y );

After the function is compiled by the C compiler, its name in the symbol library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, and the new name is called "mangled name ").

A name such as _ foo_int_int contains the function name, number of function parameters, and type information. c ++ relies on this mechanism to implement function overloading. For example, in C ++, the void Foo (int x, int y) and void Foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.

Similarly, variables in C ++ support both local variables and class member variables and global variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, the compiler uses a unique name for the variables in the class when compiling, similar to the function processing. This name is different from the global variable name with the same name in the user program.

Connection method when extern "C" is not added

Suppose in C ++, the header file of module A is as follows:

// Module A header file modulea. h
# Ifndef module_a_h
# Define module_a_h
Int Foo (int x, int y );
# Endif

Reference this function in Module B:

// Module B implements the file moduleb. cpp
# Include "modulea. H"
Foo (2, 3 );

In fact, in the connection phase, the connector looks for symbols such as _ foo_int_int from the target file modulea. OBJ generated by module!

Compilation and Connection Methods After the extern "C" clause is added

After the extern "C" statement is added, the header file of module A is changed:

// Module A header file modulea. h
# Ifndef module_a_h
# Define module_a_h
Extern "C" int Foo (int x, int y );
# Endif

In Module B's implementation file, Foo (2, 3) is still called. The result is:

(1) When module A compiles and generates the foo target code, it does not perform special processing on its name and uses the C language;

(2) When the connector looks for the Foo (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ Foo.

If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int Foo (INT X, int y ), module B cannot find the function in module A, and vice versa.

Therefore, we can summarize the true purpose of the statement "extern" C "in one sentence (the birth of any syntax feature in any language is not random and comes from the needs of the real world. When thinking about a problem, we should not just focus on how the language is made, but also ask why it is doing so and what the motivation is, so that we can better understand many problems ):
Realize mixed programming of C ++, C and other languages.
Understand the motivation for setting up extern "C" in C ++. Next we will analyze the common usage skills of extern "C.

4. Usage of extern "C"

(1) To reference functions and variables in C language in C ++, the following processing must be performed when the C Language header file (assumed as cexample. h) is included:

Extern "C"
{
# Include "cexample. H"
}

In the header file of the C language, the external function can only be specified as the extern type. The C language does not support the extern "C" declaration. when the c file contains extern "C", a compilation syntax error occurs.

The source code of the three files in the example project of C ++ referenced by the author is as follows:

/* C header file: cexample. H */
# Ifndef c_example_h
# Define c_example_h
Extern int add (int x, int y );
# Endif
/* C language implementation file: cexample. C */
# Include "cexample. H"
Int add (int x, int y)
{
Return X + Y;
}
// C ++ implementation file, call Add: cppfile. cpp
Extern "C"
{
# Include "cexample. H"
}
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}

If C ++ calls a. dll written in C language, when it includes the header file of. dll or the declared interface function, it should add extern "C "{}.

(2) When referencing functions and variables in C ++ in C, the header file of C ++ needs to add extern "C ", however, you cannot directly reference this header file that declares extern "C" in C. You should only declare the extern "C" function defined in C ++ as the extern type.
The source code of the three files contained in the example project of C ++ is as follows:

// C ++ header file cppexample. h
# Ifndef cpp_example_h
# Define cpp_example_h
Extern "C" int add (int x, int y );
# Endif
// C ++ implementation file cppexample. cpp
# Include "cppexample. H"
Int add (int x, int y)
{
Return X + Y;
}
/* C implementation file cfile. c
/* Compilation errors: # include "cexample. H "*/
Extern int add (int x, int y );
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}

If you thoroughly understand the role of extern "C" described in section 3rd in the compilation and connection phases, you can really understand the usage of referencing c Functions and C ++ functions from C ++ described in this section. Pay special attention to the sample code given in section 4th.

 

 

Extern C in C ++

 

This article mainly discusses the use of extern C in C ++.
Author: tyc611, 2006-11-06

 

When using extern "C" Declaration, there are two different forms: one is extern "C" followed by the function (or variable) Declaration; in addition, extern "C" {function (or variable) Declaration }. The first form is declared as an external connection and a C-style connection at the same time, while the second form is declared as a C-style connection only (not with an external Declaration ). For example:

// The first form
Extern "C" int Foo; // external variable declaration and C style connection
Extern "C" Void bar (); // external function declaration and C-style connection, cannot be static

// Second form
Extern "C "{
Int Foo; // C-style join and define the variable (not an external connection Declaration !)
Void bar ();
}

Therefore, when the second form is rewritten to the following form, the two forms are equivalent:

// Second form Modification
Extern "C "{
Extern int Foo; // extern declares that the object is an external connection.
Extern void bar ();
}

For a function declared by extern "C", its function only changes the function connection form (using the C style, that is, the symbol name in the target file is the same as the function name ), it does not change the nature of the function (that is, the modified C ++ function can also use the C ++ language features as usual. In addition to the general C ++ function, in terms of appearance, there is no other difference ). You can see this point more clearly in the following example program:

// A.h

#ifndef MY_A_H
#define MY_A_H

#include <iostream>

class A
{
public:
 void print()
 {
  std::cout<<"Message from class A"<<std::endl;
 }
};

#endif //MY_A_H

////////////////////////////////////////////////////

// func.cpp

#include "A.h"

extern "C"
void func(A& a)
{
 int* p=new int[23];  // test new operator
 *p=1;

 a.print();

 delete [] p;
}

////////////////////////////////////////////////
// main.cpp

#include "A.h"

extern "C" void func(A& a);

int main()
{
 A a;
 func(a);

 return 0;
}

In addition, the standard stipulates that this form is illegal:
Extern "C" static void F (); // Error
Because extern "C" indicates that it is an external connection and is in conflict with static (multiple Storage types are declared incorrectly ). However, there is no error in such declaration on vc6.0 and mingw2.05, and I don't know why. However, when defining:
Extern "C" static void F () {...} // Error
There is no compilation error in vc6.0, but there will be a connection error (func definition cannot be found in main); While mingw2.05 has a compilation error (multiple storage type declaration errors ). It seems that mingw works better.

An important factor in using extern "C" is the need to extract functions from a dynamic library. For more information, see the references below [2].

 

References:
2017110000iso-iec-14882-1998.pdf, 7.5
[2] about extracting objects from dynamic library: http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html#seealso

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.