C/C ++ hybrid programming

Source: Internet
Author: User

 

In C, Calling C ++ in C ++ will use extern "C", but the meaning of the two is quite different !!

 

 

 

 

First, we will introduce how to call C in C ++, which is familiar to everyone:

 

Example:

 

// C code

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> void foo (int x );

</Span>

 

// C ++ code

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> // C ++ code

Extern "C" void foo (int x );

</Span> enables the C ++ connector to search for this function through a symbol similar to _ foo, rather than a symbol similar to _ foo_int.

 

 

The use of extern "C" tells the compiler to compile the encapsulated interface in the C method. Of course, the C ++ syntax in the interface function is still compiled in the C ++ mode. The use of extern "C" tells the compiler to compile the encapsulated interface in the C method. Of course, the C ++ syntax in the interface function is still compiled in the C ++ mode.

 

 

 

 

Let's talk about calling C ++ in C, which also uses extern "C". This tells the compiler to compile the encapsulation interface in C mode, of course, the C ++ syntax in the interface function is still compiled in C ++ mode.

 

C ++ Code: (non-member functions)

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> extern "C" int foo (int x );

Int foo (int x)

{

//...

} </Span> in this way, the compiler will compile the foo function into a symbol similar to the _ foo symbol instead of the _ foo_int symbol.

 

 

 

C code

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> int foo (int x );

Void cc (int x)

{

Foo (x );

//...

} </Span>

 

 

 

 

If you want to call member functions (including virtual functions) in C, you need to provide a simple wrapper ). For example:

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> // C ++ code:

 

Class C

 

{

 

//...

 

Virtual double f (int );

 

};

 

 

 

Extern "C" double call_C_f (C * p, int I) // wrapper function

 

{

 

Return p-> f (I );

 

} </Span>

Then, you can call C: f ():

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;">/* 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 );

 

/*...*/

 

} </Span>

 

 

 

 

 

 

If you want to call a overload function in C, you must provide the packaging of different names to be called by C code. For example:

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> // 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) ;}</span>

Then, you can use f () for each overload as follows ():

 

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;">/* C code :*/

 

Void f_ I (int );

 

Void f_d (double );

 

 

 

Void cccc (int I, double d)

 

{

 

F_ I (I );

 

F_d (d );

 

/*...*/

 

} </Span>

 

 

 

 

 

References:

 

Bjarne Stroustrup's original link address is http://www.research.att.com /~ Bs/bs_faq2.html # callCpp

 

 

 

 

 

 

 

The following example describes how C calls C ++:

 

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> // cpp. h

# Ifndef _ cpp_h __

# Define _ cpp_h __

 

Class class1 {

Class1 ();

~ Class1 ();

Public:

Int I;

Int J;

 

Int getI (void );

};

 

# Endif

// End file

 

// Cpp. cpp

# I nclude "stdafx. h"

# I nclude <iostream>

# I nclude "cpp. h"

# I nclude "c. h"

 

Using namespace std; // open the standard library namespace

 

Class1: class1 ()

{}

 

Class1 ::~ Class1 ()

{}

 

Int class1: getI (void)

{

Return I ++;

}

 

// Compile the following function by calling C

Extern "C"

Int get_class1_ I (struct1 * p)

{

Class1 * pClass1 = (class1 *) p;

 

Cout <"c ++:" <pClass1-> getI () <endl;

 

Return pClass1-> getI ();

}

 

// End file

 

// C. h

# Ifndef _ c_h __

# Define _ c_h __

 

# Ifdef _ cplusplus

Extern "C "{

# Endif

 

Typedef struct {

Int I; // consistent with the variables in the class1 class

Int j;

} Struct1;

 

# Ifdef _ cplusplus

}

# Endif

# Endif

// End file

 

// C. c

# I nclude <cstdio>

# I nclude "c. h"

 

Extern int get_class1_ I (void * p );

 

Struct1 s;

 

Int main (void)

{

Printf ("c: % d \ n", get_class1_ I (& s ));

Printf ("c: % d \ n", get_class1_ I (& s ));

 

Return 0;

}

 

// End file </span> in linux, run:

 

Gcc-c. c

G ++-c cpp. cpp

Gcc-lstdc ++ c. o cpp. o-o result

 

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.