On the C / C ++ mixed programming

First, before the introduction of C C ++ mixed programming / first thought a few questions

What 1. C / C ++ mixed programming that?

2. C / C ++ mixed programming what's the use?

3. C / C ++ mixed programming should be how to achieve?

Next, a brief talk about my understanding of C / C ++ mixed programming:

What 1. C / C ++ mixed programming that?

Like the problem itself said, C / C ++ programming is a mixed project, call C ++ functions in C function method, the function of the C ++ method can call C functions.

2. C / C ++ mixed programming what's the use?

In our daily development, may encounter some such cases, colleagues A, C incredibly prolific, but know nothing about C ++; colleague B, C ++ come in handy, but C has his head fog but there is such a kind at work. demand, colleagues need to use a method of C ++, C, B method colleagues need to use, how to do this?

Yes, the simplest is, A colleague put C code to write, and then simply call with colleague B, empathy, colleagues just call A B C ++ code written colleagues, their duties and improve work efficiency.

3. C / C ++ mixed programming should be how to achieve?

Well, this hybrid programming how to how to achieve it?

Before the introduction, we briefly understand the following concepts

1. function overloading
2. C ++ name adaptation mechanism
3. extern 及 extern "C"

* Function overloading (  Overloading  )

C ++ and Java overloaded function definitions of the same,

I.e., in the same scope, C ++ allows multiple functions with the same names, but different parameter list, as shown below:

Function overloading

However, if you have ever wondered why C ++ supports function overloading, and C does not support function overloading it?

This would involve C ++ name mangling of the mechanism. Please read on -

* C ++ name adaptation mechanism

In C,

void test (); // Compiler will compile the function after the function name is rewritten to _test

test C language () function

test the C language compiler rename () function called _test

ps: not available test () function is to realize when Xcode link error, so that we can see the true face of test () function!

void test (int a); // After this function compiler compiler rewrites the function name is still _test

C language test (int) Function

test the C language compiler renamed (int) function is still called _test

In C ++,

void test (); // after the function name of compiler compilers will rewrite function test ()

C ++ test the compiler rename () function is called test ()

void test (int a); // this function is compiled after the function name is rewritten compiler test (int)

C ++ test the compiler renamed (int) function called test (int)

ps: Some system compiler compiles into _test_int this format, name adaptation mechanism is only an idea, not a globally unique naming conventions, different compiler naming different specifications, but the same idea as shown below! :

Function names produced by different compilers may be different

This article will cite these few examples, we can try a few more function overloading yourself, then try hands-on results. Practice is king!

A few examples above, I believe we can easily know why C ++ supports overloading C does not support overloaded.

Since C ++ name mangling there are no mechanisms C!

So in C, as long as the same function name, regardless of your parameter list poles apart, both the compiler will compile it as the same function name, which would result in ambiguous function calls during program execution (that is, for function the same function name, the program does not know which function to call), which is not allowed, so the error.

However, for C ++ terms, even though they have the same function name, but because of their different parameter list, compiled the compiler will actually be renamed as a function of their different names, so when the program calling the function and no two ambiguity, so C ++ allows function overloading.

Pull an aside here, C ++ overloading is not considered polymorphism, because polymorphism is dynamic run-time binding of methods, and the C ++ function overloading up to be "polymorphic" compile time. (This sentence is not necessarily correct, please correct)

* extern 及 extern "C"

extern believe we are more familiar, it is generally used to declare a function, scope of global variables. extern tells the compiler, functions and variables can be used for their stated in this document or other files. will not repeat them here.

extern "C" in C What does it mean?

C C language here does not refer to the language, but rather compiling a statute and links. C represents the C language compiler compliance with the statute and in any language connection, such as Fortran (Formula Translation), assembler (assembly language), etc. .

Note:

extern "C" just specify the compile and link the statute, and does not affect the semantics, so in C ++ have to file how to write, how to write, must follow the syntax specification of C ++.

Before the statement C ++ source file plus the role of extern "C" is to tell the compiler to compile this piece of code compile and link in accordance with the Statute of class C and link (Yes, that is, as a function of class C compiler naming convention)

Tip: If more than one statement requires extern "C", may be enclosed in {}, for example:

extern "C" Usage

How you should use extern "C" to achieve the C / C ++ mixed programming it?

Call C ++ code of 1. C
2. C ++ code to call C
3. C / C ++ intermodulation

* C to call the C ++ code

Ado, on the code.

C C ++ calls defined sum (int, int) function

No doubt, this code is not linked through, and why?

In C, sum will compile function calls the main function of the compiler _sum, however far out there written in C ++ files were compiled as a function of the sum sum (int, int), the compiler links when error will be reported can not find _sum function following figure

Undefined function _sum

Solutions are clear, just make sure the statute functions in C and C ++ files compiled and linked like to OK!

This time extern "C" will debut it!

At this point we need only by extern "C" {} in cpp files function needs to be called to C comprises a document, as in FIG.

Compiled successfully, correct output

* C ++ code to call C

C, C ++ calls sum (int, int) function

Also, because the compiler function links lead to different naming conventions for functions not found

At this point, we can not be like before C C ++ method calls to solve the problem, because the extern "C" and can not be used in the C file, besides C file already follow compiling and linking C of the statute, even to .c file in use extern "C" will not help.

At this time, to relate to the role of the #include known, text copy #include equivalent, equal to C functions declared in the header file to intact cpy #include "zs.h" in the following FIG.

#include

Some friends might think, since the function declarations have been copy to the C ++ file, you only need to ensure that this function declaration code C ++ file is compiled and linked in accordance with the compiled class C, links statute can guarantee compile the sum C function name as the file functions in a consistent!

So there was the following methods

Done!

Attentive friends will find, since #include equivalent text copy, then why not put extern "C" statement to the header file into it?

Well, we try ~

Sure, running a successful

But I am sorry to tell you that if you do so, then the C method will not file C file called!

Because the C compiler does not support extern "C" syntax!

This leads to a macro, __cplusplus, as long as the C ++ file, the compiler will automatically define a macro like this, we can use this macro to do C / C ++ ultimate mixed up!

C C function call success

C ++ C function call success

to sum up

To write a C / C ++ function can be called, it must follow the C compiler way (because the C language does not support C ++, and C ++ also supports C / C ++)

To achieve the C / C ++ mixed programming is very simple, just add a few lines of code in the header file, as shown below

C / C ++ mixed programming core code

Interestingly, the Objective-C compiler's function naming conventions with C language, can be seen if you want to achieve C / OC / C ++ mixed programming with C / C ++ programming is similar.

Guess you like

Origin blog.csdn.net/Abelia/article/details/93770748