How to effectively solve the problem of mutual invocation between C and C + +

Source: Internet
Author: User
    in the actual work may often have to do mixed C and C + + programming, C + + calling code is usually easier, but there are some details to note. c The code to invoke C + + is somewhat cumbersome, because C does not support object-oriented features. First, let's take a look at C + + code to invoke the language. It's easy to get your C code to be called by C and C + +, but there's still room for attention. The existing three files are as follows:/* File TestC.h * * #ifndef testc_h #define TESTC_H   #ifdef  __cplusplus extern "C" {#endif   I NT Add (int a, int b);        #ifdef  __cplusplus} #endif   #endif/* testc_h *    /* File Te STC.C * *   #include "TestC.h"   int Add (int a, int b) {    return (A + b);}    /* fi Le TestCpp.cpp * * #include "stdio.h" #include "TestC.h"   int main () {       printf ("A DD =%d/n ", Add (2, 5));               return 0; Description: File TestC.h is the header file for C, file TESTC.C is its implementation file, and file TestCpp.cpp is the C + + file that invokes the function. The TESTC_H definition in file TestC.h is for header File Protection, "#ifdef  __cplusplus" is not missing, you can go to see this in C's standard library header file, such as "stdio.h". With this macro compiler, it is now known whether C or C + + is calling it. Why should I differentiate between C and C + + calls? The underlying reason isBecause the C and C + + compilers do not handle functions as they are compiled and linked. C + + adds function arguments and type information to support function overloading at compile time. As the above Add method, the C compiler in the symbol library after the name is _add, and C + + compiler will produce a name like _add_int_int. C + + is relying on this mechanism to achieve the overload of the function. The extern keyword indicates that a function or variable is declared as a global type, which corresponds to a static. The scope of a static-qualified function or variable is this file. extern also has a role to play with "C", that is, extern "C" notifies the compiler to compile and link the code contained in extern "C" to C.   Let's take a look at how to use C + + code (including the C + + class method) in the language. For the sake of simplicity, I put the definition and implementation of a class in a file (usually in the. h and. cpp files, respectively). The custom class file (this omits the header File Protection and other details) is as follows://* file TestClass.h * *   class Hjh {public:     int Add (int a, int b) &NB sp;      {               Return (a + B);       };   Encapsulate C + + classes as a C function file (in order to briefly also put the Declaration and implementation in the same file) as follows:/* file TestCpp.cpp/  #include "TestClass.h"   extern "C" int Add_cpp (int A, int b);   int add_cpp (int a, int b) {       hjh hjh;        &NB Sp Return Hjh.add (A, b);      }   actual call to C + + codeThe C file is as follows:/*file TESTC.C * * * #include "stdio.h"   extern int add_cpp (int a, int b);   int main () {       printf ("add_cpp =%d/n", Add_cpp (2, 5));    & nbsp;          return 0; The procedure above is very clear, is to use a function to encapsulate the use of C + + class, and then declare it externally as a C function. The file TestClass.h defines and implements a class that has only one add method. The file TestCpp.cpp defines and implements a function add_cpp, in which a Hjh class object is defined and the Add method of the object is invoked. The Add_cpp function is then declared externally as C. In order to use the Add_cpp function in the testc.c file, an external declaration is also required. This is to inform the compiler that this function is implemented in other files (note that "C" is not appended to the extern in C file). When these three files compile the link together, the compiler can find the concrete implementation of the add_cpp. The above code is compiled and run in the visual c++6.0 environment. Please leave a message or e-mail:hjhinternet@163.com if you have any questions.  

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.