C C + + and C + + call C

Source: Internet
Author: User

Turn from: http://www.cppblog.com/franksunny/archive/2007/11/29/37510.html

First, C + + is called:

Declare the C + + function as ' extern ' C ' (make this declaration in your C + + code) and call it (called in your C or C + + code). For example:

C + + code:

extern "C" void f (int);

void f (int i)

{

// ...

}

You can then use the F ():

/* C Code: * *

void f (int);

void cc (int i)

{

f (i);

/* ... */

}

Of course, this applies only to Non-members functions. If you want to call 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);

void CCC (struct c* p, int i)

{

Double d = call_c_f (p,i);

/* ... */

}

If you want to call overloaded functions in C, you must provide a wrapper with different names to be called by C code. For example:

C + + code:

void f (int);

void f (double);

extern "C" void f_i (int i) {f (i);}

extern "C" void F_d (double D) {f (d);}

You can then use the f () of each overload:

/* C Code: * *

void f_i (int);

void F_d (double);

void cccc (int i,double D)

{

F_i (i);

F_d (d);

/* ... */

}

Note that these techniques also apply to C + + class libraries, even if you cannot (or do not) modify C + + header files.

The translated document Bjarne Stroustrup's original link address is:

Http://www.research.att.com/~bs/bs_faq2.html#callCpp

Can be summed up as:

1, for C + + non-class member functions, you can simply add extern "C" to the function declaration, usually in the header file, and of course you can put the Declaration and function definitions together in the CPP, and in the absence of a declaration, adding an extern "C" directly before the definition.

2, for C + + class member functions, you need to do a separate CPP file, will need to call the function to wrap.

Second, to implement the C + + code to call, the specific operation:

For the function code in C, either modify the header file of C code, add extern "C" in the declaration when it is included in C + + code, or re-declare the C function in C + + code, and declare the extern "C" header when you re declaring it.

Through the above instructions, I understand that the addition of extern "C" header must be added to the C + + code file in order to work.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Another turn: http://zhidao.baidu.com/question/120770275.html

The use of extern "C" is mainly because the C compiler compiles functions without parameter type information, containing only the symbolic name of the function. if the int foo (float x) C compiler compiles this function into a similar _foo symbol, the C connector considers the connection to be successful as long as it finds the symbol that calls the function. The C + + compiler, in order to implement the function overload, takes the parameter information of the function at compile time. If it can compile the above function into a symbol similar to _foo_float. Therefore, C calls C + +, using extern "C" is to tell the compiler to follow the C way to compile the encapsulation interface, of course, the interface function inside the C + + syntax or compiled in C + + mode.

Such as:

C + + Code

extern "C" int foo (int x);

int foo (int x)

{ //...}

In this way, the compiler compiles the Foo function to resemble a _foo symbol and does not compile to resemble a _foo_int symbol

Then C can call C + + functions like this

C codeint foo (int x);

void cc (int x) {foo (x);//...}

If you want to invoke overloaded C + + functions, you must encapsulate a separate interface with a total of C calls. 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);

// ...

}

and C + + call C,extern "C" The role is: let C + + connectors to call the function of the symbol to use the way C:

C Code

void foo (int x);

C + + to call this function

C + + Code

extern "C" void foo (int x);

is to have a C + + connector that looks like _foo to find this function, rather than a symbol like _foo_int.

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.