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

Source: Internet
Author: User

When you look at some programs, there's always
"#ifdef __cplusplus
extern "C" {
#endif "definition, figuring out what's going on:

Microsoft-specific predefined Macros
__cplusplus Defined for C + + programs only.
This means that if you are a C + + program, use
extern "C" {
And this thing, refers to the following function does not use the C + + name decoration, but with C's

The following code shows a header file which can be used by C and C + +

Client applications:
MyCFuncs.h
#ifdef __cplusplus
extern "C" {//only need to export C interface if
Used by C + + source code
#endif

__declspec (dllimport) void Mycfunc ();
__declspec (dllimport) void Anothercfunc ();

#ifdef __cplusplus
}
#endif

When we want to call C's Library from C + +, (note that the driver is written in C, even new, delete is not used, depressed) can not just explain an external function, because call C function of the compiled code and call C + + function compiled code is different. If you only describe an external function, the C + + compiler assumes that it is

c++ 's function was compiled successfully, but when you connect you will find a lovely error.
The solution is to specify that it is a C function:  
extern "C" function description  
Specify a group of functions:  
extern "C" { < BR style= "margin:0px; padding:0px ">n function description  

If you want C and C + + mixed words:  
#ifdef _cplusplus 
#endif  
n function description &NBSP;
#ifdef _cplusplus 
}&NBSP;
#endif

extern "C" means that the internal symbol name compiled by the build uses the C convention.

C + + supports function overloading, which is not supported, 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: void foo (int x, int y); 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 use C's function in C + +,

Or, you use C + + functions in C.
Examples of C + + reference functions
TEST.c
#include <stdio.h>
void MyTest ()
{
printf ("MyTest in. c file ok\n");
}
Main.cpp
extern "C"
{
void MyTest ();
}
int main ()
{
MyTest ();
return 0;
}

To reference C + + functions in C
In C, when referencing functions and variables in the C + + language, a C + + function or variable is declared in the extern "C" {}, but cannot use extern "C" in the C language, otherwise the compilation error occurs.
Test.cpp
#include <stdio.h>
extern "C"
{
void MyTest ()
{
printf ("MyTest in. cpp file ok\n");
}
}
Main.c
void MyTest ();
int main ()
{
MyTest ();
return 0;
}
Integrated use
In general, we put the function declaration in the header file, when our function may be used by C or C + +, we can not determine whether to declare the function in extern "C", so we should add
#ifdef __cplusplus
extern "C"
{
#endif
function declaration
#ifdef __cplusplus
}
#endif
If 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 namespace Std;
extern "C"
{
#endif
void MyTest ();
#ifdef __cplusplus
}
#endif
In this way, you can place the implementation of the MyTest () in a. C or. cpp file, and you can use the functions inside the header file after the. C or. cpp file contains "test.h" without compiling errors.
TEST.c
#include "test.h"
void MyTest ()
{
#ifdef __cplusplus
cout << "cout mytest extern ok" << Endl;
#else
printf ("printf mytest extern ok n");
#endif
}
Main.cpp
#include "test.h"
int main ()
{
MyTest ();
return 0;
}

The intent of extern "C"
A few days ago, 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 to find the error:

The following is an assumption of the old C library

Header file for C

/*-----------c.h--------------*/#ifndef _c_h_#define _c_h_extern int

Add (int x, int y); #endif
source file for C

/*-----------c.c--------------*/int Add (int x, int y) {return x+y;}
Calls to C + +

/*-----------cpp.cpp--------------*/#include "c.h" void Main () {add

(1, 0);}
Compiling this will produce an error cpp.obj:error lnk2001:unresolved external symbol "int __cdecl Add (int,int)" (Email protected]@[email protecte D]), because the target module of add is not found this reminds me of C + + overloaded function naming and C function naming, let us recall: C in the function after the compilation of the name will be "_" before the function name, such as the Add function compiled into the obj file is actually named _add, and C + + Naming is different, in order to implement function overloading the same function name as the add parameter will be compiled into a different name

For example
int add (int, int) ==>[email Protected]@[email protected],

Float Add (float, float) ==>[email Protected]@[email protected],

The above is the naming 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.

The compiler found the Add (1, 0) in the CPP file, and the function was declared as extern int add (int x, int y), and the compiler decided to look for [email protected]@[email protected], cpp.cpp. Unfortunately he couldn't find it because C's source file put extern int add (int x, int y), compiled into _add, to solve this problem C + + uses extern "C", this is our theme, want to use the previous C library, then you have to learn it, We can look at the following standard header files 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 (in t, int);

#ifdef __cplusplus} #endif #endif/*__h*/If we copy this header file we can get

#ifndef _c_h_#define _c_h_#ifdef __cplusplusextern "C" {#endifextern

int add (int, int); #ifdef __cplusplus} #endif #endif/* _c_h_ * * compile like this

/*-----------C.C--------------*/
int add (int x, int y) {
return x+y;
}


At this point the source file for *.c,__cplusplus is not defined, and extern "C" {} does not take effect for C he sees just extern int add (int, int); Add function compiles into _add (int, int);

While compiling C + + source files

/*-----------cpp.cpp--------------*/
#include "c.h"
void Main ()
{
Add (1, 0);
}
At this point the source file is defined as *.cpp,__cplusplus, and for C + + he sees the extern "C" {extern int add (int, int);} The compiler will know that add (1, 0), call the C-style function, will know to go to C.obj to find _add (int, int) instead of [email protected]@[email protected], which is why the DLL often see the extern "C" {},windows is in C language he first had to take into account that C could call these DLLs correctly, and that the user might use C + + and extern "C" {} would be useful

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

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.