On the mutual reference _c language of C and C + + functions

Source: Internet
Author: User
Tags function examples

1. Introduction
The C + + language was originally created "A better C", but this does not mean that the C-language-like global variables and functions in C + + are compiled and connected in exactly the same way as the C language. As a language intended to be compatible with C, C + + retains some of the characteristics of the procedural language (known as "not completely object-oriented"), so it can define global variables and functions that do not belong to any class. But, after all, C + + is an object-oriented programming language, in order to support the overloading of functions, C + + for global functions of the processing mode and C are significantly different.

2. From the standard header document
An enterprise has given the following interview question: Why standard header files have similar structure?

#ifndef __incvxworksh
#define __incvxworksh
#ifdef __cplusplus
extern "C" {
#endif
/*...*/
#ifdef __cplusplus
}
#endif
#endif/* __incvxworksh * *

Obviously, the role of the compiled macro "#ifndef __incvxworksh, #define __incvxworksh, #endif" In the header file is to prevent the header file from being repeatedly referenced. So

#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif

What is the role of it? We will come in the following one by one ways.

3. Deep disclosure of extern "C"

The extern "C" contains a double meaning, literally: First, the object modified by it is "extern"; second, the target it modifies is "C". Let's take a detailed interpretation of this twofold meaning.

A function or variable that is qualified by extern "C" is an extern type;

extern is a keyword that indicates the scope (visibility) of functions and global variables in the C + + language, which tells the compiler that its declared functions and variables can be used in this module or in other modules.

Remember the following statement:
extern int A;

is simply a declaration of a variable that is not defining variable A and allocating no memory space for a. Variable A can only be defined once as a global variable in all modules, otherwise there will be a connection error.

Typically, the module's header file provides the keyword extern declaration for functions and global variables that are referenced by this module to other modules. For example, if module B wants to refer to the global variables and functions defined in module A, just include the header file for module A. In this way, when a function in module A is called in Module B, module B cannot find the function in the compile phase, but it does not complain; it will find this function in the connection phase from the object code generated by module a compilation.

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

The variables and functions that are decorated by extern "C" are compiled and connected according to the C language;

How to compile without extern "C" declaration

First look at C + + in C-like functions are compiled.

As an object-oriented language, C + + supports function overloading, while programming language C does not support it. A function is compiled by C + + in a symbol library with a different name than the C language.

For example, suppose the prototype of a function is:
void foo (int x, int y);

The function is compiled by the C compiler in the symbol library with the name _foo, and the C + + compiler produces names like _foo_int_int (different compilers may produce different names, but all use the same mechanism, the resulting new name is called "Mangled Name").

_foo_int_int Such a name contains the function name, the number of function parameters and type information, C + + is to rely on this mechanism to implement the function overload. For example, in C + +, the function void foo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float.

Similarly, variables in C + + support class member variables and global variables in addition to local variables. The class member variable of the program that the user writes may have the same name as the global variable, and we use the "." to differentiate. In essence, the compiler, when compiling, is similar to the processing of a function, and a unique name for a variable in a class that is different from the name of a global variable with the same name as the user program.

Connection mode without extern "C" declaration

Suppose in C + +, the header file for 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 implementation file ModuleB.cpp
#include "ModuleA.h"
Foo (2,3);

In fact, in the connection phase, the connector looks for symbols like _foo_int_int from the target file Modulea.obj generated by module A!

How to compile and connect after the extern "C" declaration

After adding the extern "C" declaration, module A's header file becomes:

Module a header file moduleA.h
#ifndef module_a_h
#define Module_a_h
extern "C" int foo (int x, int y);
#endif

The Foo (2,3) is still called in the implementation file of Module B, and the result is:

(1) When the module a compiles the object code of Foo, it does not specially deal with its name, and adopts the method of C language;

(2) The connector is looking for an unmodified symbol name _foo when looking for the Foo (2,3) call for the target code of Module B.

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

So, you can generalize the true purpose of the declaration of extern "C" in one sentence (the birth of any grammatical feature in any language is not random, it comes from the demand-driven of the real world.) When we think about problems, we can't just stay in the language, and ask why it does it, what the motivation is, so that we can understand a lot more deeply:
Implement mixed programming with C + + and other languages.
Understand the C + + in the establishment of extern "C" motivation, we are below to specifically analyze the extern "C" the usual use of skills.

4.extern "C" idiomatic method

(1) in C + + reference to the functions and variables, in the C language header file (assumed to be cExample.h), you need to do the following processing:

extern "C"
{
#include "cExample.h"
}

In the C language header file, the external function can only be specified as an extern type, and the extern "C" declaration is not supported in C language, and a compilation syntax error occurs when the. c file contains extern "C".

The author of C + + reference to quote the example of the project contains three files of the source code as follows:

/* C Language 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, calling 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, you should add extern "C" {} When you include the header file for the. dll or when declaring an interface function.

(2) When referencing functions and variables in C + + language, the header file of C + + must be added extern "C", but in C language it is not possible to refer directly to the header file that declares extern "C", only the extern "C" defined in C + + should be function is declared as an extern type.
The author of the C reference + + function Examples of the project included in the three files of the source code are 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
/* This will compile an error: #include "cExample.h" */
extern int Add (int x, int y);
int main (int argc, char* argv[])
{
Add (2, 3);
return 0;
}

Related Article

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.