c, c ++ mixed situation calls

        Use extern "C" compiler switches when calling c function in c ++ by if the forgotten will be compiled, but the situation through link. Since C appears before C ++, C ++ after a natural compatible with C, call C in C ++ are supported by relevant standards in the C ++ standard. However, the C does not provide the backward compatibility, it can not directly call C ++. Therefore, the solution is in the package of C ++, a C interface can support, to call C ++ functions through this interface.

C, how to call C ++ function?

Recently Asked a question on how to call in C C ++ function, when the simple answer is to function with the extern "C" statement, when asked how within a class member function declaration, know what to say, and later checked the Internet , the internet has a translation of the article the father of C ++ can be used as answers, then Mark brought about.

 

The C ++ function is declared as `` extern "C" '' (made this statement in your C ++ code), then call it (calling in your C or C ++ code). E.g:

// C++ code:

extern "C" void f(int);

void f(int i){

     // ...

}

Then, you can use this f ():

/* C code: */

void f(int);

void cc(int i)

{

    f(i);

   /* ... */

    }

 

Of course, this trick only for non-member functions. If you want to call member functions (including virtual functions) in C, you need to provide a simple wrapper (wrapper). E.g:

// C++ code:

class C

{

   // ...

   virtual double f(int);

};

 

extern "C" double call_C_f(C* p, int i) // wrapper function

{

   return p->f(i);

}

Then you can call C :: f ():

/* 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);

   /* ... */

}

 

If you want to call overloaded functions in C, you must provide the name of different packaging, so as to be called C code. E.g:

// 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); }

 

Then, you might use each overloaded f ():

/* C code: */

void f_i(int);

void f_d(double);

 

void cccc(int i,double d)

{

   f_i(i);

   f_d(d);

   /* ... */

}

Note that these tips also apply to calling C ++ class library in C, even if you can not (or do not want) to modify C ++ header files.

 

 

How to call C code in C ++

Previously summarized an article ( http://www.cppblog.com/franksunny/archive/2007/11/29/37510.html ), on how to call C ++ code in C, then do not fully deployed, simply do a debug, recently I saw a subject required to achieve each call C and C ++ code, although its results are achieved by extern "C", but there are some specific differences.

First call for C C ++ code to make a brief review:

1, Central Africa for the C ++ class member function can be simply added before the function declaration extern "C", usually located in the function declaration in the header file, of course, may be put together in cpp function definitions and declarations, in the absence of a statement of case, direct addition extern "C" can also be defined before

2, for C ++ class member function, you will also need to do a cpp file, will need to call the function of packaging.

See the above two examples of how to call the article in front of C C ++ code.

To implement the code in C ++ call C, the specific operation:

For the function code in C, or the header files of C code to modify, add extern "C" in a statement when it is subsumed into C ++ code or re-statement about the C functions in C ++ code, add the extern re-statement "C" head.

Through the above description, I understand that, and that is to add extern "C" head must be added to the C ++ code files in order to play a role.

 

Following analysis of the substance of the reasons for this phenomenon:

Type information without function when C compiler function, the function comprising only symbolic name, such as a C compiler to function int a (float x) compiled into a symbol like this _a, just find the connector C function call sign , you can connect successfully, it is assumed that parameter type information is correct, this is the disadvantage of the C compiler connector. The C ++ compiler to achieve function overloading, the function will bring compile-time type information, as he above functions may be compiled into a symbol in order to achieve such _a_float overloaded, note that it is not worthwhile with return information, which why C ++ does not support the use of one of the reasons the function return value to distinguish overloaded functions, of course, a function of the user function returns the value of treatment (such as ignoring) is also an important reason.

Based on the above, the C calling C ++, you first need a wrapper function to call to C ++ classes and other packaged into a C function to C calls, so extern role of "C" is: let the compiler know about it, and then the C language after compiled and connected to the wrapper function (usually the wrapper function in C ++ compiler is compiled C ++ embodiment, with the extern "C", the compiler will depend C is compiled package interface, of course, the interface function inside the C ++ syntax or by C ++ compile way; for the C language section - the caller, or press the C language compiler; C interface respectively to the parts a and C ++ compilers, and then connect you can achieve the C calling C ++). In contrast, C ++ calls a C function, the role of extern "C" is: when using C C ++ connector allows the calling function to find a symbol of the way that the use of _a instead _a_float come to the calling function.

 

See specific examples http://www.cppblog.com/Files/franksunny/CCallCpp.rar

Note: If you encounter a similar "fatal error C1010: unexpected end of file while looking for precompiled header directive" when using VC6.0 compiler error attachments, then please do bb.c file the following documents deal with right-click on the project in select setting, in c / c ++ column, select preCompiled headers, and then set the first option, choose not to use precompiled headers.

Guess you like

Origin blog.csdn.net/xiebingsuccess/article/details/91845852