Linux under C to call C + + interface detailed

Source: Internet
Author: User

Turn from: http://blog.csdn.net/feiyinzilgd/article/details/6723882

C + + for a long time, often in the way of C + + to think about the problem, and sometimes suddenly found that they are not too able to write C program.    When writing a program, will inevitably use the third party plug-ins or libraries, and these plug-ins or libraries are often not fully meet our needs, encountered this situation, if all is C + +, that good to do, write an adapter OK, about adapter mode reference my Blog "C + + adaptor design mode" If you want to provide to C program use, you need to encapsulate the C program can use the library. A few days ago, in the Csdn Linux/unix version of the stroll, met a netizen, he asked such a question: (the exact words I can't remember, roughly this meaning) I want to encapsulate a C + + interface to provide for the use of the program, I do not have a C + + program, can run, but the encapsulation, I use g++ Compile, there is no problem, but the expected result is to use GCC compiler, but when compiling with GCC to ensure that the error, a lot of undefined ... I want people familiar with Linux programming to see this problem and know what's going on. I'm not going to be a maverick here, I just want to sum up. If you let C call the C + + interface

Before encapsulating the interface, it is helpful to understand some of the features of C/A + +.

C + + founder in writing C + +, the language is popular, he had to make C + + compatible C. The biggest feature of C + + is encapsulation, inheritance, polymorphism, overload. And these characteristics are precisely what C language does not have. As for polymorphism, the core technology is realized through the virtual function table, which is actually the pointer. And for overloading, and C language, in fact, is different from the compilation method: C + + compilation method and C compilation mode. For a function call, the compiler can compile the connection as long as it knows the parameter type and return value of the function and the function name. So in order for C to invoke C + + interface or C + + call interface, it must be the caller and the callee have the same way of compiling. This is the role of extern "C", the extern "C" is the program in the way of C compiled. Let's take a look at how the C + + and C Two compile methods are different, because C only considers function calls, here we discuss the difference between functions only. Let's look at a section of source code:

We use a very short code to illustrate the problem:

[CPP] view plain copy print? Cplus.cpp//compiler program int Operation (int) in C + + mode
Type the command compiled cplus:g++-c Cplus.cpp-o CPLUS.O produces the target file CPLUS.O. Let's take a look at the symbols in the target file

To view internal symbols using the NM command: NM CPLUS.O

The content is simple: 00000000 T _z9operationi

Let's take a look at add Exern "C" to compile the program in the C way:

[CPP] view plain copy print? extern "C" int Operation (int)
Also use the command above to generate CPLUS.O. Then look at the symbols as follows:

00000000 T Operation

In contrast, we can find that using C + + method to compile functions with a _z9 prefix and i suffix, where I refers to the parameter type. This makes sense, because C does not have overloads, just know the function name can determine the function, and C + + has overloads, need to be based on the parameter type and return type to uniquely determine a function.


Say here, you are expected to understand almost. The interface provided to C must be added with extern "C". Here is just a way to determine how to compile, extern "C" can only be compiled by the compiler in the way of C. But C doesn't know.

extern "C", here also to add a process: In the C file in the extern below interface. So the C program will recognize the interface function. The following is a simple example to illustrate how to make the encapsulated C + + interface available to C.

[CPP] view plain copy print?      Myclass.h #include <iostream> using namespace std;      Class Myclass {Public:myclass () {} ~myclass () {} void Operation ();         };   myclass.cpp extern "C" void myclass::operation {cout << "Hi, Harlen" << Endl; }

Compile command: g++-C Myclass.cpp-o MYCLASS.O

[CPP] view plain copy print?       Interface.cpp #include "myclass.h" Void Interface () {MyClass obj; Obj.   Operation (); }


Compile command: g++-C Interface.cpp-o INTERFACE.O

In this way, the interface is already ready. One way is to use the command: AR rs libinterface.a interface.o myclass.o generate a static library to provide interfaces.

Another way is to use GCC, which will invoke the. o Target file of the program and MYCLASS.O, INTERFACE.O, to be compiled into an executable program.

[CPP] view plain copy

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.