external c

The main function of extern "C" is to correctly implement C++ code to call other C language code. After adding extern "C", it will instruct the compiler to compile this part of the code according to the C language , not C++. Since C++ supports function overloading, the compiler will add the parameter type of the function to the compiled code during the process of compiling the function, not just the function name; and the C language does not support function overloading, so compiling C The function of the language code does not carry the parameter type of the function, generally including the function name.

     This function is very useful, because before the emergence of C++, many codes were written in C language, and the underlying libraries were also written in C language. In order to better support the original C code and the already written C language library, It is necessary to support C as much as possible in C++, and extern "C" is one of the strategies.

This function is mainly used in the following situations:

1. C++ code calls C language code

2. Use in C++ header files

3. When multiple people develop collaboratively, some people may be good at C language, while some people are good at C++, it will also be useful in this case

To give an example of my design:

There are two modules, moduleA and moduleB. B calls the code in A, where A is implemented in C language, and B is implemented in C++. An implementation method is given below:

//moduleA header file

#ifndef __MODULE_A_H //For module A, this macro is to prevent repeated references to header files

#define __MODULE_A_H

int fun(int, int);

#endif

//moduleA implementation file moduleA.C //The implementation part of module A has not changed

#include"moduleA"

int fun(int a, int b)

{

return a+b;

}

//moduleB header file

#idndef __MODULE_B_H //Obviously this part is also to prevent repeated references

#define __MODULE_B_H

#ifdef __cplusplus //And this part is to tell the compiler that if __cplusplus is defined (that is, if it is a cpp file, extern "C"{ //because the cpp file defines this macro by default), it will be compiled in C language

#include"moduleA.h"

#endif

... //other code

#ifdef __cplusplus

}

#endif

#endif

//moduleB implementation file moduleB.cpp //The implementation of module B has not changed, but the design of the header file has changed

#include"moduleB.h"

intmain()

{

cout<<fun(2,3)<<endl;

}

The following is a detailed introduction:

Since the C and C++ compilers do not compile functions exactly the same, especially for C++, they support function overloading, and the compiled functions are generally named after the function name and formal parameter type.

For example, the function void fun(int, int) may be compiled (different compilers have different results) _fun_int_int (different compilers may be different, but they all use a similar mechanism, using the function name and parameter type to name the compiled function name ); and C language does not have a similar overloading mechanism. Generally, the function name is used to indicate the compiled function name, and the corresponding function above may be a name such as _fun.

Take a look at one of the following interview questions: Why do standard header files have a similar structure?

#ifndef __INCvxWorksh /*Prevent this header file from being referenced repeatedly*/

#define __INCvxWorksh

#ifdef __cplusplus //Tell the compiler that this part of the code is compiled in C language format, not C++

extern "C"{

#endif

/*…*/

#ifdef __cplusplus

}

#endif

#endif /*end of __INCvxWorksh*/

analyze:

  • Obviously, the function of compiling macros "#ifndef __INCvxWorksh, #define __INCvxWorksh, #endif" (that is, the blue part in the above code) in the header file is to prevent the header file from being repeatedly referenced
  • So

#ifdef __cplusplus (where __cplusplus is a custom macro in cpp!!!)

extern "C"{

#endif

#ifdef __cplusplus

}

#endif

What is the role of?

extern "C" contains double meanings. Literally, it can be known that, firstly, the target modified by it is "extern"; secondly, the target code modified by it is "C".

  • A function or variable qualified by extern "C" is of type extern

extern is a keyword in the C/C++ language that indicates the scope of functions and global variables. This keyword tells the compiler that the declared functions and variables can be used in this module or other modules.

Remember, the following statement:

extern int a; is just a declaration of a variable, it does not define a variable a, and does not allocate space for a. Variable a can only be defined once as a global variable in all modules, otherwise an error will occur.

Generally speaking, in the header file of the module, the functions and global variables provided by this module to other modules are given the keyword extern. 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 A. In this way, when module B calls a function in module A, in the compilation phase, although module B cannot find the function, it will not report an error; it will find the function from the object code compiled by module A in the link phase.

The keyword corresponding to extern is static. Static indicates that the variable or function can only be used in this module. Therefore, the variable or function modified by static cannot be modified by extern C.

  • Variables and functions decorated with extern "C" are compiled and linked in the C way: this is important! ! ! !

As mentioned above, since C++ supports function overloading, but C language does not, the name of the function in the symbol library after C++ compilation is different from that of C language; the C++ compiled function needs to add the type of the parameter to The only way to demarcate the overloaded function, and after adding extern "C", is to indicate to the compiler that this code is compiled according to the C language.

Linking method without extern "C" declaration:

//module A header file moduleA.h

#idndef _MODULE_A_H

#define _MODULE_A_H

int foo(int x, int y);

#endif

Call the function in module B:

//Module B implements the file moduleB.cpp

#include"moduleA.h"

foo(2,3);

In fact, during the linking phase, the linker will look for symbols such as _foo_int_int in the object file moduleA.obj generated by module A! ! ! , which is obviously impossible to find, because the foo() function is compiled into a symbol of _foo, so there will be a link error.

A common practice can refer to one of the following implementations:

There are two modules, moduleA and moduleB. B calls the code in A, where A is implemented in C language, and B is implemented in C++. An implementation method is given below:

//moduleA header file

#ifndef __MODULE_A_H //For module A, this macro is to prevent repeated references to header files

#define __MODULE_A_H

int fun(int, int);

#endif

//moduleA implementation file moduleA.C //The implementation part of module A has not changed

#include"moduleA"

int fun(int a, int b)

{

return a+b;

}

//moduleB header file

#idndef __MODULE_B_H //Obviously this part is also to prevent repeated references

#define __MODULE_B_H

#ifdef __cplusplus //And this part is to tell the compiler that if __cplusplus is defined (that is, if it is a cpp file, extern "C"{ //because the cpp file defines this macro by default), it will be compiled in C language

#include"moduleA.h"

#endif

... //other code

#ifdef __cplusplus

}

#endif

#endif

//moduleB implementation file moduleB.cpp //The implementation of module B has not changed, but the design of the header file has changed

#include"moduleB.h"

intmain()

{

cout<<fun(2,3)<<endl;

}

Points for using extern "C"

1. Can be a single statement

    extern "C" double sqrt(double);

2. It can be a compound statement, which is equivalent to adding extern "C" to the declarations in the compound statement

    extern "C"

   {

      double sqrt(double);

      int min(int, int);

  }

3. The header file can be included, which is equivalent to adding extern "C" to the declarations in the header file

   extern "C"

  {

    #i nclude <cmath>

  }

4. You cannot add extern "C" inside a function

5. If the function has multiple declarations, you can add extern "C", or only appear in the first declaration, and subsequent declarations will accept the rules of the first link indicator.

6. In addition to extern "C", there are extern "FORTRAN" and so on.

 

Reprint............

Reference article:

extern C use

The role of extern c【Turn】_Look at the snow..

Points for using extern C

http://blog.chinaunix.net/u/29619/showart_230148.html

http://blog.csdn.net/weiqubo/archive/2009/10/16/4681813.aspx

http://hi.baidu.com/sundl2268/blog/item/4969453d2258bae53c6d970a.html

“I find that the harder i work,the more luck i seem to have!”

The main function of extern "C" is to correctly implement C++ code to call other C language code. After adding extern "C", it will instruct the compiler to compile this part of the code according to the C language , not C++. Since C++ supports function overloading, the compiler will add the parameter type of the function to the compiled code during the process of compiling the function, not just the function name; and the C language does not support function overloading, so compiling C The function of the language code does not carry the parameter type of the function, generally including the function name.

     This function is very useful, because before the emergence of C++, many codes were written in C language, and the underlying libraries were also written in C language. In order to better support the original C code and the already written C language library, It is necessary to support C as much as possible in C++, and extern "C" is one of the strategies.

This function is mainly used in the following situations:

1. C++ code calls C language code

2. Use in C++ header files

3. When multiple people develop collaboratively, some people may be good at C language, while some people are good at C++, it will also be useful in this case

To give an example of my design:

There are two modules, moduleA and moduleB. B calls the code in A, where A is implemented in C language, and B is implemented in C++. An implementation method is given below:

//moduleA header file

#ifndef __MODULE_A_H //For module A, this macro is to prevent repeated references to header files

#define __MODULE_A_H

int fun(int, int);

#endif

//moduleA implementation file moduleA.C //The implementation part of module A has not changed

#include"moduleA"

int fun(int a, int b)

{

return a+b;

}

//moduleB header file

#idndef __MODULE_B_H //Obviously this part is also to prevent repeated references

#define __MODULE_B_H

#ifdef __cplusplus //And this part is to tell the compiler that if __cplusplus is defined (that is, if it is a cpp file, extern "C"{ //because the cpp file defines this macro by default), it will be compiled in C language

#include"moduleA.h"

#endif

... //other code

#ifdef __cplusplus

}

#endif

#endif

//moduleB implementation file moduleB.cpp //The implementation of module B has not changed, but the design of the header file has changed

#include"moduleB.h"

intmain()

{

cout<<fun(2,3)<<endl;

}

The following is a detailed introduction:

Since the C and C++ compilers do not compile functions exactly the same, especially for C++, they support function overloading, and the compiled functions are generally named after the function name and formal parameter type.

For example, the function void fun(int, int) may be compiled (different compilers have different results) _fun_int_int (different compilers may be different, but they all use a similar mechanism, using the function name and parameter type to name the compiled function name ); and C language does not have a similar overloading mechanism. Generally, the function name is used to indicate the compiled function name, and the corresponding function above may be a name such as _fun.

Take a look at one of the following interview questions: Why do standard header files have a similar structure?

#ifndef __INCvxWorksh /*Prevent this header file from being referenced repeatedly*/

#define __INCvxWorksh

#ifdef __cplusplus //Tell the compiler that this part of the code is compiled in C language format, not C++

extern "C"{

#endif

/*…*/

#ifdef __cplusplus

}

#endif

#endif /*end of __INCvxWorksh*/

analyze:

  • Obviously, the function of compiling macros "#ifndef __INCvxWorksh, #define __INCvxWorksh, #endif" (that is, the blue part in the above code) in the header file is to prevent the header file from being repeatedly referenced
  • So

#ifdef __cplusplus (where __cplusplus is a custom macro in cpp!!!)

extern "C"{

#endif

#ifdef __cplusplus

}

#endif

What is the role of?

extern "C" contains double meanings. Literally, it can be known that, firstly, the target modified by it is "extern"; secondly, the target code modified by it is "C".

  • A function or variable qualified by extern "C" is of type extern

extern is a keyword in the C/C++ language that indicates the scope of functions and global variables. This keyword tells the compiler that the declared functions and variables can be used in this module or other modules.

Remember, the following statement:

extern int a; is just a declaration of a variable, it does not define a variable a, and does not allocate space for a. Variable a can only be defined once as a global variable in all modules, otherwise an error will occur.

Generally speaking, in the header file of the module, the functions and global variables provided by this module to other modules are given the keyword extern. 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 A. In this way, when module B calls a function in module A, in the compilation phase, although module B cannot find the function, it will not report an error; it will find the function from the object code compiled by module A in the link phase.

The keyword corresponding to extern is static. Static indicates that the variable or function can only be used in this module. Therefore, the variable or function modified by static cannot be modified by extern C.

  • Variables and functions decorated with extern "C" are compiled and linked in the C way: this is important! ! ! !

As mentioned above, since C++ supports function overloading, but C language does not, the name of the function in the symbol library after C++ compilation is different from that of C language; the C++ compiled function needs to add the type of the parameter to The only way to demarcate the overloaded function, and after adding extern "C", is to indicate to the compiler that this code is compiled according to the C language.

Linking method without extern "C" declaration:

//module A header file moduleA.h

#idndef _MODULE_A_H

#define _MODULE_A_H

int foo(int x, int y);

#endif

Call the function in module B:

//Module B implements the file moduleB.cpp

#include"moduleA.h"

foo(2,3);

In fact, during the linking phase, the linker will look for symbols such as _foo_int_int in the object file moduleA.obj generated by module A! ! ! , which is obviously impossible to find, because the foo() function is compiled into a symbol of _foo, so there will be a link error.

A common practice can refer to one of the following implementations:

There are two modules, moduleA and moduleB. B calls the code in A, where A is implemented in C language, and B is implemented in C++. An implementation method is given below:

//moduleA header file

#ifndef __MODULE_A_H //For module A, this macro is to prevent repeated references to header files

#define __MODULE_A_H

int fun(int, int);

#endif

//moduleA implementation file moduleA.C //The implementation part of module A has not changed

#include"moduleA"

int fun(int a, int b)

{

return a+b;

}

//moduleB header file

#idndef __MODULE_B_H //Obviously this part is also to prevent repeated references

#define __MODULE_B_H

#ifdef __cplusplus //And this part is to tell the compiler that if __cplusplus is defined (that is, if it is a cpp file, extern "C"{ //because the cpp file defines this macro by default), it will be compiled in C language

#include"moduleA.h"

#endif

... //other code

#ifdef __cplusplus

}

#endif

#endif

//moduleB implementation file moduleB.cpp //The implementation of module B has not changed, but the design of the header file has changed

#include"moduleB.h"

intmain()

{

cout<<fun(2,3)<<endl;

}

Points for using extern "C"

1. Can be a single statement

    extern "C" double sqrt(double);

2. It can be a compound statement, which is equivalent to adding extern "C" to the declarations in the compound statement

    extern "C"

   {

      double sqrt(double);

      int min(int, int);

  }

3. The header file can be included, which is equivalent to adding extern "C" to the declarations in the header file

   extern "C"

  {

    #i nclude <cmath>

  }

4. You cannot add extern "C" inside a function

5. If the function has multiple declarations, you can add extern "C", or only appear in the first declaration, and subsequent declarations will accept the rules of the first link indicator.

6. In addition to extern "C", there are extern "FORTRAN" and so on.

 

Reprint............

Reference article:

extern C use

The role of extern c【Turn】_Look at the snow..

Points for using extern C

http://blog.chinaunix.net/u/29619/showart_230148.html

http://blog.csdn.net/weiqubo/archive/2009/10/16/4681813.aspx

http://hi.baidu.com/sundl2268/blog/item/4969453d2258bae53c6d970a.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325014469&siteId=291194637