C and C + + call each other! Just like the university dormitory, I use yours, you use mine!

Time:2021-11-27

Note that the premise of this article is that C code is compiled by C language compilers such as GCC, and C + + code is compiled by C + + compilers such as G + +. If C and C + + codes are compiled by G + +, in most cases, the two codes can call each other.

The following is a summary o_ O||。

 

Functional differences between C and C + +

To understand how to call each other, we must first understand the differences between C and C + +.

As an upgraded version of C language, C + + and C + + must have many differences.

One major difference is that C + + supports function overloading, while C language does not. In order to make the function support overloading, C + + adds the type information of return value and parameter to the function name on the basis of C language.

For example, after the function int add (int, int) is compiled by the C + + compiler, the possible function name is int_ add_ int_ int(int, int)

(Note: This is a general description of how C + + adds the return value and parameter information to the function name, which is not necessarily implemented by the compiler in practice).

It can be concluded from the above description that due to the support of C + + for function overloading, the compiled function symbols are inconsistent with those of C language, even on the premise that the two function names are the same.

 

Function of extern “C”

So, can’t C and C + + call each other? The answer is no, because the keyword “extern” C “exists, which enables statements to be compiled and connected according to the compilation and connection protocol of class C, rather than the compiled connection protocol of C + +. In this way, C + + functions or variables can be called in C-like code.

Note: the “C” in the extern “C” instruction represents a compilation and connection protocol, not a language. “C” means any language that conforms to the compilation and connection specifications of C language, such as FORTRAN, assembler, etc.

It should also be noted that the extern “C” instruction only specifies the compilation and connection specifications, but does not affect the semantics. For example, if extern “C” is specified in the function declaration, the type detection and parameter conversion rules of C + + must still be observed.

 

Calling C code in C++

For c++, because the c++ compiler is compatible with C language, calling function written in C language in c++ only requires the keyword extern “C” in front of the function declaration, which indicates that the function symbols are parsed in the way of class C language. Examples are as follows:

// add.h

#ifdef __ADD_H__

#define __ADD_H__

extern "C" int add(int a, int b);

#endif

// add.c

int add(int a, int b)

{

    return a + b;

}

// main.cc

#include 

#include "add.h"

using namespace std;

int main()

{

    cout << "1 + 1 = " << add(1, 1) << endl;

}

 

         In the example, main.cc is C + + code and add. C is C language code. When the C + + compiler recognizes the external “C” keyword, it will look for itaddFunction instead of looking for similarint_add_int_intIn this way, the function with parameter information is implemented.

 

C language calls C + + code

It is not easy for C language to call C + + code because C language is not compatible with C + +.

Even if C language can call C + +, it will compile and report errors because it cannot recognize the newly defined symbols of C + +.

Therefore, in order to call C + + functions in C language, the following two steps must be implemented:

one   Encapsulate C + + related functions as static libraries or dynamic libraries (because the compiler does not know what language is executed when calling library functions);

two   Provide interface functions that follow the C-like language specification.

 

Examples are as follows:

// printNum.h

#ifdef __PRINTNUM_H__

#define __PRINTNUM_H__

extern "C" void printNum(int a);

#endif

// printNum.cc

#include 

#include "printNum.h"

using namespace std;

void printNum(int a)

{

    cout << << "num is " << a << endl;

}

// main.c

extern void printNum(int a);

printNum(5);

 

         By willcoutThe function is encapsulated as an interface function similar to the C language specification, so that C + + functions can be successfully called in main. CprintNum

It is worth noting that printnum. H cannot be directly introduced into main. C because C language cannot recognize the external “C” keyword. You can use C + + predefined macros to rewrite header files:

#ifdef __PRINTNUM_H__

#define __PRINTNUM_H__

#ifdef __cplusplus

extern "C" {

#endif

void printNum(int a);

#ifdef __cplusplus

}

#endif

#endif
#ifdef __PRINTNUM_H__

#define __PRINTNUM_H__

#ifdef __cplusplus

extern "C" {

#endif

void printNum(int a);

#ifdef __cplusplus

}

#endif

#endif

 

 

Summary

✿ the mutual call between C language and C + + can be realized through   extern “C”   Keyword implementation

✿   The C code is invoked in c++, and the extern “C” is added before the declaration of C code function in c++.

✿   When C language calls C + + code, you need to compile c + + code into static library or dynamic library, and then provide it for external use   extern “C”   Declared class C encapsulation function

 

 

Whether you are a career change or a beginner, you can also advance——Noteworthy entry】YesC / C + + programming learning advanced Club

It involves:C language, C + +, windows programming, network programming, QT interface development, Linux programming, game programming, hackers and so on

 

An active, high-profile and high-level programmer programming learning hall; The introduction to programming is only incidental, and the improvement of thinking is valuable!