Use of extern "C"

content

introduction

1. C++ program calls C library

2. C program calls C++ library


introduction

  Sometimes in a C++ project, it may be necessary to compile some functions according to the C style, adding extern "C" before the function, which means to tell the compiler to compile the function according to the rules of the C language. For example: tcmalloc is a project implemented by Google in C++. It provides two interfaces, tcmallc() and tcfree, for use, but if it is a C project, it cannot be used, so he uses extern "C" to solve it.

1. C++ program calls C library

Here we take the OJ of a stack as an example: LeetCode.20 effective parentheses , we are here to use the stack of a static library written in C language to facilitate the call

 1. We first put the main function of the topic and some interface functions in the C++ project file test.cpp

 As shown below:

2. We make the stack into a static library of C (so that we can directly call the functions inside in C++)

 ①We first create a new project (the compiler used here is VS2019), select the windows desktop wizard (you can also choose a static library)

 ②Configure a new project

 ③Select static library, empty project

 ④Copy the interface function of the stack previously written in C language into the library

 ⑤ Generate a library with a suffix of .lib

 3. Then call the header file of the library in test.cpp

 Where "../" means to return the file of the previous layer

#include "../Stack1/Stack.h"

After that, you can see that the red warning of the report has been eliminated. Declare the header file in this way, which is equivalent to telling the compiler that there are these functions. We compile and link again and find the "unresolved external function". We also need to do some configuration to link it.

 4. Then configure the C++ project properties

①Additional library directory (debug file under Stack1)

Put the Stack1.lib library in the debug into the additional library directory, that is, copy the debug path in the created Stack1 to the additional library directory

 ②Additional dependencies (link the library) Stack1.lib; add it to the front, note that the semicolon cannot be lost

 5. Next comes the use of extern "C", add it before test.cpp

extern "C"
{
#include"../Stack1/Stack.h"
}

Finally compile and run

 The call was successful! !

2. C program calls C++ library

 When we write C programs and need to call C++ libraries, we also need extern "C"

1. Put the main function and the interface function to be used in the test.c file

 2. Make the stack a static library of C++ (same process as above)

 The same compilation produces a file with the suffix .lib

3. Configuration properties, additional library directories and additional dependencies (not repeated here)

After compiling and linking, you will find that it cannot be linked again

4. extern "C" comes in handy

The extern "C" here is to make the C++ static library process functions with the C function name modification rules, so that the C file can recognize these functions and call them smoothly. 

We can change this, using the method of conditional compilation , we can make the C++ library modify the function with the C function name modification rules, thereby generating .lib

#ifdef __cplusplus
extern "C"
{
#endif

	//初始化栈
	void StackInit(Stack* ps);

	//销毁栈
	void StackDestroy(Stack* ps);

	void StackPush(Stack* ps, STDataType x);

	void StackPop(Stack* ps);

	//当前栈的大小
	int StackSize(Stack* ps);

	//获取栈顶元素
	STDataType StackTop(Stack* ps);

	//栈是否为空
	bool StackEmpty(Stack* ps);

#ifdef __cplusplus
}
#endif 

Finally, compile, link and run, and observe that the suffix is ​​test.c, which proves that C calls C++ successfully. 

Run successfully! ! !

This is the usage of extern "C", thank you! !

Guess you like

Origin blog.csdn.net/weixin_57675461/article/details/121876001