[C++] Mutual calls between C programs and C++ programs


Preface

We all know that C and C++ are two languages, and C++ is compatible with C. But sometimes we write a program in C++ and want to use the functions in the C file. Or we write a program in C language and want to call functions in the C++ program. You will find that the two cannot call each other. In fact, they can. This article will reveal it to you.


1. C++ calls C

First of all, we need to have two projects, one project is compiled with a C++ program, and the project to be called is compiled with a C program.


C++ project:
Insert image description here


C program project:

A header file is required, and the header file is provided to the main program for inclusion.
Insert image description here


Because C++ calls C, and a program has only one main function, there is no main function in the C project.

Generate static library

Then, we need to generate a static library from the C project.
Insert image description here
Insert image description here

Click OK and compile.
Insert image description here

Insert image description here
This lib file is the static library of the Print project

include header files

Then we moved the generated .lib file and the .h file of the C program to the C++ program.
Insert image description here
Then include the header file
Insert image description here

Add dependent libraries

Insert image description here
Add the .lib static library generated by the C program into it
Insert image description here
Insert image description here

extern

Then we need to use extern to modify this header file.


extern "C" //用C语言的方式编译括号内容
{
    
    
#include "Print.h"
}

int main()
{
    
    
	Print();
	return  0;
}

Then we run the program
Insert image description here

In this way, the CPP program can be successfully used to call the content of the C program.

2. C calls C++

C calls are actually similar to C++, and C++ calls C.

We changed the program to be called into a C++ program
Insert image description here
and then changed the main program into a C program.
Insert image description here
But now there is a problem. There is no usage of extern "C" in the C language. This usage will cause an error in the C program, so we put it Delete.

#define _CRT_SECURE_NO_WARNINGS 1


#include "Print.h"

int main()
{
    
    
	Print();
	return  0;
}

Then change the printed content to is C++
Insert image description here

Because CPP programs and C language programs modify function names differently, we have to use extern to modify the functions that need to be called in the C++ program. But at
Insert image description herethis time the problem arises again. The .h header file will be expanded during preprocessing. But the C language doesn't know extern "C", so we can do this.

#pragma once


#ifdef __cplusplus
extern "C"
{
    
    
#endif

void Print();

#ifdef __cplusplus
}
#endif

__cplusplus is a macro definition that only exists when compiling a C++ program. If the program is not compiled as a C++ program, extern "C" will not be expanded.

Then we can compile it and generate a static library of the C++ program.
Insert image description here

Then move the C++ static library and Print.h file to the C program directory
Insert image description here

Don’t forget to add dependent libraries to the main program
Insert image description here
Insert image description here

In this way, we successfully used the C program to call the function in the C++ program
Insert image description here

Summarize

Whether C++ calls a function in C, or C calls a function in C++. The essence is the same. The only thing to note is that there is no such usage of extern "C" in C language. Therefore, the C++ program needs to ensure that when compiled in C++, it is converted to compiled in C, that is, extern "C" is used. But at the same time, you also need to avoid errors caused by extern "C" when the .h file is expanded in a C file.

Guess you like

Origin blog.csdn.net/Lin5200000/article/details/128395713