Problems Needing Attention in Mixed Programming of C and C++

 The knowledge points explained in this article are "small", but they are very important in mixed programming of C and C++.

Because when we write applications, we often use third-party programs.

If our code uses C, but the third-party code is C++;

Or our code uses C++, and the third-party code is C,

Then you need to be more careful when integrating.

One, C calls functions in C++

1. The header file in the called C++ code (callee.h)

 

2. The source file in the called C++ code (callee.cpp)

 

3. The called C++ code is compiled into an object file (callee.o)

 

Knowledge points:

(1) About __cplusplus

    The compiler uses g++, and all C++ compilers define a macro: __cplusplus, so when compiling callee.h, the function declaration will be wrapped in extern "C".

 

(2) About extern "C"

    C and C++ compilers have different compilation strategies when compiling a function. C++ will rewrite the name of the function (and each C++ compiler has different rules for name rewriting, and even different versions of the same compiler have different name rewriting rules. Therefore, it is best to use the same when using C++. The version of the compiler compiles all modules in the project. Supplement: The purpose of rewriting is to implement function overloading in C++ language).

    In callee.h, put the function cpp_hello in extern "C", which means to tell the compiler g++: This function needs to be called by C. Please do not rewrite the name of this function.

    You can check the symbols in the target file callee.o through the nm command:

    We can do one more test: after removing the extern "C", let's see how this function is renamed by g++:

4. The source file (caller.c) in the main calling C code

 

5. Compile the main calling C file to get the executable file

 

Knowledge points:

(1) When caller.c includes "callee.h", the __cplusplus macro is not defined in the gcc compiler, so there is only one sentence in callee.h: void cpp_helo();

 

(2) When calling the cpp_hello() function, although this function is compiled with g++, the name has not been rewritten by the compiler g++ because of the use of extern "C", that is, in the callee.o object file, The name of the function is "cpp_hello", so it can be called by C code smoothly.

 

Two, C++ calls functions in C

1. The header file in the called C code (callee.h)

 

2. The source file in the called C code (callee.c)

 

3. The called C code is compiled into an object file (callee.o)

 

Knowledge points:

    The compiler uses gcc, which does not define the macro: __cplusplus, so when compiling callee.h, it is equivalent to only one function declaration. Therefore, the function c_hello was not renamed when it was compiled into the callee.o object file.

 

4. The main calling C++ code source file (caller.cpp)

 

5. Compile the main calling C++ file to get the executable file

 

Knowledge points:

    When g++ compiles callee.h, because the __cplusplus macro is defined in g++, when it calls the function c_hello, it will be called in the way of C (that is, there is no name rewriting), so it can be successfully in callee.o Find this function in.

 

Three, summary

1. In the function declaration of C++ code, if the code will be called by C program, extern "C" must be added.

2. In the function declaration of C code, if the code will be called by C++ program, add extern "C".

3. When programming in C++, it is best to use the same compiler for each module, including the same version.

 

I wonder if this blog post is helpful to you?

It’s not easy to be original, I hope you can support it a lot, comments and likes are free~~~

Guess you like

Origin blog.csdn.net/u012296253/article/details/110428720