Definition of "#ifdef __cplusplus extern" C "{#endif"

Source: Internet
Author: User

there are always "#ifdef __cplusplus when you look at some programs.extern "C" {#endif"The definition, figuring out what's going on:Microsoft-specific predefined macros__cplusplus Defined forC++programs only. That means, if it's C.++program, you useextern "C"{And this stuff, refers to the C that is not used in the function below++The name is decorated, but the C's the following code shows a header file which can be used by C and C++Client applications://MyCFuncs.h#ifdef __cplusplusextern "C"{//Only need to export C interface if//used by C + + source code#endif__declspec (dllimport)voidMycfunc (); __declspec (dllimport)voidAnothercfunc (); #ifdef __cplusplus}#endifwhen we want to get from C+ + in the call C library, (note, the driver is written in C, even new, delete can not be used, depressed) can not just explain an external function, because the call C function of the compiled code and call C + + functions of the compiled code is different. If you only describe an external function, C + +The compiler assumes that it is a C++The function was compiled successfully, but when you connect you will find a lovely error. The workaround is to specify that it is a C function:extern "C"function Description Specifies a group of functions:extern "C"{N Function description} if you want C and C++mixed words: #ifdef _cplusplusextern "C"{ #endif n Description of functions #ifdef _cplusplus}#endifextern "C"represents the built-in symbol name for compilation using the C convention. C+ + supports function overloading, and C does not, and the compilation rules for both are not the same. Functions are compiled in C + + with different names in the symbol library than in the C language. For example, suppose a function is prototyped as:voidFoointXinty); The function is compiled by the C compiler and the name in the symbol library may be _foo, while the C + + compiler produces names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, and the resulting new name is called "Mangled Name"). _foo_int_int such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading. Here's an example of how to do this in C + +Use C in the C function, or C++the function. //examples of C + + reference functions//test.c#include <stdio.h>voidmytest () {printf ("mytest in. c File ok\n");}//main.cppextern "C"{voidmytest ();}intMain () {mytest ();return 0;}//to reference C + + functions in CWhen referencing functions and variables in the C + + language, a C + + function or variable is declared in the extern"C"{}, but you cannot use extern in the C language"C", or compile an error. //Test.cpp#include <stdio.h>extern "C"{voidmytest () {printf ("mytest in. cpp file ok\n");}}//main.cvoidmytest ();intMain () {mytest ();return 0;}//Integrated UseIn general, we put the function declaration in the header file, when our function may be used by C or C + +, we cannot determine whether to declare the function in the extern"C", so we should add #ifdef __cplusplusextern "C"{#endif//function Declaration#ifdef __cplusplus}#endifIf we notice that many header files have this kind of usage, such as string.h, and so on. //test.h#ifdef __cplusplus#include<iostream>using namespacestd;extern "C"{#endifvoidmytest (); #ifdef __cplusplus}#endifIn this way, the implementation of the mytest () can be placed in a. C or. cpp file, which can be contained in a. C or. cpp file"test.h"after using the function inside the header file, there is no compilation error. //test.c#include"test.h"voidmytest () {#ifdef __cpluspluscout<<"cout mytest extern OK"<<Endl;#elseprintf ("printf mytest extern ok n");#endif}//main.cpp#include"test.h"intMain () {mytest ();return 0;}extern "C"the intention of the previous days, the program is used to write a long time ago the C program, want to use the inside of the function, the connection found that there is no specific function error: The following is the assumption of the old C library C header file/*-----------c.h--------------*/#ifndef _c_h_#define_c_h_extern intAdd (intXinty);#endifsource file for C/*-----------c.c--------------*/intAddintXintY) {returnx+y;} C++the call/*-----------Cpp.cpp--------------*/#include"c.h"voidMain () {Add (1,0);} Compiling this will produce an error cpp.obj:error lnk2001:unresolved external symbol"int __cdecl Add (int,int)"([email Protected]@[email protected]), because the target module of add is not found this reminds me of C + + overloaded function naming and C function naming, let's review: C, after the function is compiled, the name will be"_", such as the actual name _add when the Add function was compiled into an obj file, and C + +naming is different, in order to implement function overloading the same function name as the Add argument will be compiled into different names such asintAddint,int) ==>[email protected]@[email protected],floatAddfloat,float) ==>[email Protected]@[email protected], the above is the name of VC6, different compilers will be different, in short, different parameters of the same function name compiled into different target names, so that the function overload is called the specific function. Compiler cpp.cpp found in the CPP file Add (1,0); The invocation of the function is declared as externintAddintXinty); The compiler decided to look for [email protected]@[email protected], but he couldn't find it, because C's source files put externintAddintXint(y), compiled into _add; C + + uses extern to solve this problem"C", this is our theme, want to take advantage of the previous C library, then you have to learn it, we can look at the following standard header file you will find that many header files have the following structure #ifndef __h#define__h#ifdef __cplusplusextern "C" {#endifextern int f1 (int, int), extern int F2 (int, int), extern int f3 (int, int);#ifdef __cplusplus}#endif#endif/*__h*/If we copy this header file we can get#ifndef _c_h_#define_c_h_#ifdef __cplusplusextern "C" {#endifexternintAddint,int); #ifdef __cplusplus}#endif#endif/* _c_h_ * * compile like this/*-----------c.c--------------*/intAddintXinty) {returnx+y;} When the source file is*.c,__cplusplus is not defined,extern "C"{} This did not take effect for C he saw just externintAddint,int); The Add function compiles into _add (int,int); and compile C++source File/*-----------Cpp.cpp--------------*/#include"c.h"voidMain () {Add (1,0);} When the source file is*.cpp,__cplusplus is defined, for C + + he sees the extern"C"{extern intAddint,int);} The compiler will know that add (1,0); Call the C-style function and you will know to go to c.obj for _add (int,intinstead of [email protected]@[email protected]; This is why the DLL often sees extern"C"{},windows is in the C language he first has to consider that C can call these DLLs correctly, while users may use C + + and extern"C"{} will take effect
Reprinted from: http://www.cnblogs.com/HappyXie/archive/2011/01/07/1929369.html

Definition of "#ifdef __cplusplus extern" C "{#endif"

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.