extern "C"

Source: Internet
Author: User

Often see the following code:

#ifdef _cplusplus

extern "C" {

#endif

#include "XXX.h"

#ifdef _cplusplus

}

#endif

Explain:

The purpose of extern "C" is to tell the C + + compiler that the following code compiles according to C, and plainly, do not name the functions (function name mangling). This function is typically used when using C functions or modules in C + + programs.

Reason:

C + + in order to support function overloading, the compiled function name is restructured (mangled name), such as the following function

int add (int a, int b);

The name compiled in C is add, and in C + +, the compilation becomes add_int_int (for example, it actually varies by compiler), so you can distinguish different overloaded functions by adding the type of the arguments after the name of the function, for example, another function.

Float Add (float A, float b);

In C + +, it will be compiled into Add_float_float, which is the mechanism of C + + differentiating overloaded functions

But the problem comes with it.

C + + carries out a name reorganization, and C. does not restructure. When a C + + program refers to the function of C, it will go to the target file (. obj) to find the corresponding function in the modified name, and the target file is the C version of the function, the name is not on, so it is not found at all!

What do we do?

That's one reason the extern "C" exists.

It tells C + +, which is included in extern "C" {//...} The thing in the block is the C version, you do not make the name reorganization when compiling, otherwise you cannot find me when you link!

So the above code is not difficult to understand, the light said no practice is the nonsense, on the code

We simply define a C header file and an implementation file that contains only one add function

CClass.h content is as follows

#ifndef __cclass_h__

#define __cclass_h__

extern int Add (int a, int b);

#endif//End __cclass_h__

CCLASS.C content is as follows

#include "CClass.h"

int add (int a, int b)

{

return a + B;

}

Below we use a C + + program to reference this C file

Main.cpp content is as follows

#define _cplusplus//In order to test, impose a sentence

#ifdef _cplusplus

extern "C" {

#endif

#include "CClass.h"

#ifdef _cplusplus

}

#endif

#include <iostream>

using namespace Std;

int main (void)

{

int result = Add (1, 2);

cout << result << Endl;

System ("pause");

return 0;

}

If you don't have the # include <iostream> before that code and just contain

#include the word "CClass.h"

You're going to get the following mistake.

Error lnk2019:unresolved external symbol "int __cdecl Add (int,int)" ([email protected]@[email protected]) referenced in Fu Nction _main

Obviously this is a link error because the corresponding function definition cannot be found

Of course, you can also write the following form, directly in the extern "C" block containing the function you want to call

extern "C"

{

int add (int a, int b);

};

#include <iostream>

using namespace Std;

int main (void)

{

int result = Add (1, 2);

cout << result << Endl;

System ("pause");

return 0;

}

This is not a problem in a C + + program, but if it is in a C program, a compilation error occurs because extern "C" is not allowed in C

Another scenario that requires extern "C" is when C programs call C + +

Follow the steps below.

1. Use extern "C" {} to declare functions that will be used by C programs in the. h file of C + +

2. Implement the above functions in a. cpp file in C + +

3. Use extern to declare C + + functions to use in. c Files

4. You can use

Note: You must not include the C + +. h file in the. c file, so the compilation cannot pass

On the code:

Declaring the Add function in CPPClass.h

#ifndef __cppclass_h__

#define __cppclass_h__

extern "C"

{

int add (int a, int b);

};

#endif//End __cppclass_h__

CPPClass.cpp Implementing the Add function

#include "CPPClass.h"

int add (int a, int b)

{

return a + B;

}

MAIN.C content is as follows

#include <stdio.h>

#include "CPPClass.h"//Do not include header files, or compile

extern int Add (int a, int b); Simply display the function that declares the call to

int main (void)

{

int result = Add (1, 2); Using functions

printf ("%d", result);

return 0;

}

extern "C"

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.