LinuxC calls the C ++ Library

Source: Internet
Author: User
LinuxC calls the C ++ library-general Linux technology-Linux programming and kernel information. The following is a detailed description. C calls the C ++ function library. Generally, it cannot be called directly. You need to convert the C ++ library into the C interface output. You can use the C call. See the following example:

Aa. cxx
# Include "add. h"
Int sample: method ()
{
Cout <"method is called! \ N ";
}

Aa. h
# Include

Using namespace std;
Class sample
{
Public:
Int method ();
};

Place the above two files to the/usr/lib directory to generate the dynamic library libaa. so. The compilation command is as follows:

Sudo g ++-fpic-shared-g-o/usr/lib/libaa. so aa. cxx-I ./

Because the class cannot be recognized in C, the member functions of the above class should be encapsulated into the C interface function output. The following encapsulation will convert the output interface to the C interface.

Mylib. cxx
# Include "add. h"
# Ifndef _ cplusplus
# Define _ cplusplus
# Include "mylib. h"
# Endif

Int myfunc ()
{
Sample ss;
Ss. method ();
Return 0;
}

Mylib. h
# Ifdef _ cplusplus
Extern "C"
{
# Endif

Int myfunc ();

# Ifdef _ cplusplus
}
# Endif

In linux, the gcc compiler does not use the Variable _ cplusplus to distinguish between C code and C ++ code. If the gcc compiler is used, here we can define a Variable _ cplusplus to distinguish C and C ++ code, so in mylib. cxx defines a Variable _ cplusplus to identify whether "extern" C "is required to encapsulate function interfaces into C interfaces. However, if you use the g ++ compiler, you do not need to define _ cplusplus. The compilation command is as follows:

G ++-fpic-shared-g-o mylib. so mylib. cxx-la-I ./

Main. c
# Include
# Include
# Include "mylib. h"

Int
Main ()
{
Int (* dlfunc )();
Void * handle; // define a handle
Handle = dlopen ("./mylib. so", RTLD_LAZY); // obtain the library handle
Dlfunc = dlsym (handle, "myfunc"); // obtain the function entry
(* Dlfunc )();
Dlclose (handle );

Return 0;
}

The compilation command is as follows:

Gcc-o main. c./mylib. so-ldl

The following code can be executed.

It must be noted that, because main. c and mylib. cxx must contain mylib. h, and to encapsulate the function myfunc into a C interface function output requires "extern" C "", while C does not recognize "extern" C "", therefore, we need to define _ cplusplus to differentiate mylib. the function myfunc in h.

The myfunc () function can also be called directly in the main function of main. c. Here we will introduce the method for calling library functions.
Related Article

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.