有时候在C++工程中可能需要将某些函数按照C的风格来编译,在函数前加 extern "C" ,意思是告诉编译器,将该函数按照C语言规则来编译。
比如我写一个程序,其中某一段程序想用别人写好的程序,这时我们可以使用静态库/动态库。
静态库/动态库的创建
在VS2019中,创建空项目(不要创建静态库项目):
一定要把代码拷贝到这个工程目录下,再去添加到工程:
然后右击“项目”,选择“属性”:
在此处将“配置类型”改为静态库:
然后重新生成解决方案(单纯编译无法生成.lib文件):
可以看到生成了.lib文件:
静态库/动态库的使用
在cpp程序里包含.h文件
使用此语句:
#include"../stack/stack.h"
.. 表示返回上一级文件夹
. 表示当前文件夹
stack.h:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps,STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int StackSize(ST* ps);
bool StackEmpty(ST* ps);
.cpp
#include<iostream>
using namespace std;
#include"../stack/stack.h"
bool isValid(const char* s) {
ST st;
StackInit(&st);
while (*s)
{
if (*s == '('
|| *s == '['
|| *s == '{')
{
StackPush(&st, *s);
++s;
}
else
{
//遇到右括号了,但是栈里面没有数据
//说明前面没有左括号,不匹配
//返回false
if (StackEmpty(&st))
{
StackDestory(&st);
return false;
}
STDataType top = StackTop(&st);
StackPop(&st);
if ((*s == ')' && top != '(')
|| (*s == ']' && top != '[')
|| (*s == '}' && top != '{'))
{
StackDestory(&st);
return false;
}
else
{
++s;
}
}
}
//如果栈不是空,说明栈中还有左括号未出
//没有匹配,返回的是false
bool ret = StackEmpty(&st);
StackDestory(&st);
return ret;
}
int main()
{
cout << isValid("(){}") << endl;
return 0;
}
按F7编译会发现有链接错误:
这就代表了cpp程序找stack里面函数的时候找不到,因为只包含了stack.h文件,只有声明,没有定义。
这时候我们就要链接动态库:
要附加库目录
选择Debug文件夹:
即将.lib文件放到目录中来
然后在此处添加.lib文件
然后重新生成解决方案,发现还是有链接错误:
一般来说,当主程序文件和静态库同为c或cpp后缀时,编译链接是没有问题的。
但是当一个文件为.c和一个文件为.cpp文件,就会出现问题
C++程序调用C静态库
为什么上面链接出现了错误?
原因为:C++和C生成的函数名规则不一样,链接参考函数重载
所以这个时候就轮到 extern "C" 出场了!
我们需要在cpp程序里面加上这段代码:
extern "C"
{
#include"../stack/stack.h"
}
这就是告诉编译器,{ }内的函数是C编译器编译的,链接的时候用C的函数名规则去找,就可以链接上。
C程序调用C++静态库
用一个C程序调用C++写的静态库,跟前面包含静态库的步骤类似,就不重复展示了。
但是这里"extern C"使用时与前面是有区别的,因为C语言的语法中不支持extern C,所以只能在C++静态库中使用。
在cpp静态库中使用:
在主程序c中使用:
然后编译报错:
我们要在cpp静态库中动点手脚:
stack.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//C++静态库,就会按C的函数名修饰规则去处理下面的函数,但是下面的函数就不能使用重载了
#ifdef __cplusplus
extern "C"
{
#endif
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int StackSize(ST* ps);
bool StackEmpty(ST* ps);
#ifdef __cplusplus
}
#endif // __cplusplus
对代码的几点解释:
如果在库中的文件要使用这个头文件,代码就是原样
如果在c项目中,extern c被条件编译屏蔽掉了,就是这个样子
#ifdef __cplusplus
//extern"C"
//{
//#endif
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int StackSize(ST* ps);
bool StackEmpty(ST* ps);
//#ifdef __cplusplus
//}
//#endif
然后就可以使用了