How to call C + + C in C code

Source: Internet
Author: User

Note here that C calls C + + or C + + calls the code in the. cpp file in the. c file, or vice versa.

The integrated development environment, such as vc++6.0 or VS, is a file suffix that distinguishes between C-code and C + + code that is currently being compiled, followed by a response compilation, invocation protocol, and so on.

The extern "C" is used mainly because the C compiler compiles the function without the type information of the parameter, and contains only the symbolic name of the function. Such as
int foo (float x)
The C compiler compiles this function into a _foo-like symbol, and the C connector finds the connection to be successful as long as it finds the symbol of the calling function.
In order to implement the function overloading, the C + + compiler takes the parameter information of the function at compile time. If it can compile the above function into a symbol similar to _foo_float.
SoC calls C + +, using extern "C" is to tell the compiler to compile the encapsulated interface in accordance with C, of course, the C + + syntax inside the interface function is compiled in C + +.
Example: 1 Common functions
C + + Code
extern "C" int foo (int x);
int foo (int x)
{
//...
}
In this way, the compiler compiles the Foo function into a similar _foo notation without compiling a similar _foo_int symbol
C can call C + + functions like this
C Code
int foo (int x);
void cc (int x)
{
Foo (x);
//...
}
2 If you want to invoke overloaded C + + functions, you must encapsulate a separate interface for a common C call.
Such as
C + + Code
void foo (int x);
void foo (float x);

extern "C" void foo_i (int x)
{
Foo (x);
}
extern "C" void Foo_f (float x)
{
Foo (x);
}
You can call this in C
C Code
void foo_i (int x);
void Foo_f (float x);
void CCC (int x1, float x2)
{
Foo_i (x1);
Foo_f (x2);
// ...
}
3 C to invoke member functions (including virtual functions) in C + +, you need to provide a simple wrapper (wrapper).

For example://C + + code:

Class C

{

...

Virtual double f (int);

};

extern "C" double Call_c_f (c* p, int i)//wrapper function

{

return p->f (i);

}

Then you can call C::f () like this:

C Code

Double call_c_f (struct c* p, int i);//Declaration

void CCC (struct c* p, int i)

{

Double D=call_c_f (p,i);

...

}

Problem: The parameter struct c* p from where, that is how to define C + + object, in fact, just said the idea, the real C use C + + classes need to be the original class are encapsulated, see the following article

http://blog.csdn.net/caspiansea/article/details/9676153


C + + calls C,extern "C" for the C + + connector to find the symbol that invokes the function in the form of the following:
C Code
void foo (int x);

C + + This calls the
C + + Code
extern "C" void foo (int x);

is to have the C + + connector look similar to _foo to find this function, rather than a symbol like _foo_int.

Often in the CPP code to see such code: in particular, C + + in the introduction of C's header files, these C-header files appear many of the following code.

#ifdef __cplusplus extern "C" {#endif

A section of code

#ifdef __cplusplus} #endif  

Where __cplusplus is a reserved macro definition for the C + + compiler. This means that the C + + compiler thinks the macro is already defined. So the key is extern "C" {}extern "C" is to tell C + + compiler device in parentheses is compiled in accordance with the C obj file format, to connect the word according to C's naming rules to find.

To understand why the extern "C" is used, you have to start with the overloaded handling of the function from CPP. In C + +, in order to support overloading mechanism, in compiling the generated assembly code, the name of the function to do some processing, such as the return type of the function and so on. In C, it's just a simple function name that doesn't include any other information. That is, C + + and C handle the resulting function names differently.

Understand the effect of adding and not adding extern "C" to the function name, we continue our discussion: Why do I need to use extern "C"? C + + 's father in the design of C + +, considering that there is already a large number of C code, in order to support the original C code and already written C library, need to support C + + as far as possible, and extern "C" is one of the strategies.

Imagine this: a library file has been written in C and works well, this time we need to use this library file, but we need to use C + + to write this new code. If the code uses C + + to link to the C library file, then there will be a link error.

Now we have a C library file, its header file is f.h, the resulting lib file is f.lib, then if we want to use this library file in C + +, we need to write:

extern "C" {

#include "f.h"

}

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.