C ++

Source: Internet
Author: User

<1> how to call C ++ in C
How to use c language to call a dynamic link library made of c ++, go
Http://blog.donews.com/xzwenlan/archive/2005/05/31/405799.aspx

Link Library header file:
// Head. h
Class
{
Public:
A ();
Virtual ~ A ();
Int gt ();
Int pt ();
Private:
Int s;
};

. Cpp
// Firstso. cpp
# Include <iostream>
# Include "head. h"

A: (){}
A ::~ A (){}
Int A: gt ()
{
S = 10;
}
Int A: pt ()
{

Std: cout <s <std: endl;
}
The compilation command is as follows:
G ++-shared-o libmy. so firstso. cpp
At this time, generate the libmy. so file and copy it to the System Library:/usr/lib/
Perform secondary encapsulation:
. Cpp
// Secso. cpp
# Include <iostream>
# Include "head. h"
Extern "C"

{

Int f ();

Int f ()
{
A;
A. gt ();
A.pt ();
Return 0;
}

}
Compile command:
Gcc-shared-o sec. so secso. cpp-L.-lmy
At this time, the second. so file is generated. At this time, the Library is changed from a class to a c interface.
Copy to/usr/lib
Call the following:
// Test. c
# Include "stdio. h"
# Include "dlfcn. h"

# Define SOFILE "sec. so"
Int (* f )();
Int main ()
{
Void * dp;
Dp = dlopen (SOFILE, RTLD_LAZY );
F = dlsym (dp, "f ");
F ();
Return 0;
}
The compilation command is as follows:
Gcc-rdynamic-s-o myapp test. c
Run Z $./myapp
10
$

A supplement to this article goes to http://blogs.sun.com/lirincy/
This blog shows how to use C to call the C ++ library. I tried it and succeeded in linux,
There are two points to change. The final compilation statement should be:
Gcc-rdynamic-s-o-ldl myapp test. c
In addition, test. c should end:
Dlclose (dp );
Otherwise, it will be CoreDump.
In fact, he turns the class method into a C function that can be called externally and uses extern C.

Ii. How does the C ++ program call libraries written in C language, such as a. lib, with the corresponding library header file a. h. Suppose a. h defines the function:

Int WhyCoding (int a, float B );

The practice is,

/* Cpp_a.h */

Extern "C "{

# Include "a. h"

}

Or

/* Cpp_a.h */

Extern "C "{

Int WhyCoding (int a, float B);/* redefine all C Functions */

}

As shown above, extern "C" is a bridge between C and C ++. This bridge is required because C compiler does not include

Function type information, only contains the function symbol name. For example, the C compiler compiles the int a (float x) function into a symbol similar to _ a. The C connector only needs

After finding the symbol that calls the function, you can connect successfully. It assumes that the parameter type information is correct, which is a disadvantage of the C compilation connector. C ++

In order to implement function overloading, the compiler will include the type information of the function during compilation. For example, it may compile the above a function into a value like _ a_float.

In order to implement overload, note that it still does not contain returned value information. This is why C ++ does not support using function return values to differentiate functions.

One of the reasons for overloading is, of course, the way the function user processes the function return values (such as ignoring) is also an important reason.

Based on the above, C ++ needs to first encapsulate calls to C ++ classes into C functions so that C can be called. Therefore

The function is to let the compiler know about this and then compile and connect the encapsulated functions in C language. (The C ++ compiler usually uses the encapsulated Functions

After using extern "C", the compiler compiles and encapsulates the interface in C mode. Of course, the C ++ syntax in the interface function is still in C ++ mode.

Compile; for the C language part-the caller, or by C language; Compile the C ++ Interface part and C part respectively, and then connect to implement C

C ++ is called ).

On the contrary, C ++ calls the C function. The function of extern "C" is to use the C method for the C ++ connector to find the symbol used to call the function, that is, use _ a instead

_ A_float to call the function.

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.