C++如何调用已经写好的C接口
作者:deroy 时间:2021-10-16 10:16:14
前言:
如何在C++代码中调用写好的C接口?你可能会奇怪,C++不是兼容C吗?直接调用不就可以了,那么我们来测试一下,先看看C++如何调用C代码接口的。
1、C++调用C文件
一个C语言文件test.c
#include <stdio.h>
void print(int a,int b)
{
printf("这里调用的是C语言的函数:%d,%d\n",a,b);
}
一个头文件test.h
#ifndef _TEST_H
#define _TEST_H
void print(int a,int b);
#endif
C++文件调用C函数
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
cout<<"现在调用C语言函数\n";
print(3,4);
return 0;
}
执行命令
gcc -c test.c
g++ -o main main.cpp test.o
编译后链接出错:main.cpp
对print(int, int)
未定义的引用。
那么g++编译器为什么找不到print(int,int)
呢,其实在我们学C++重载的时候就提到过C++底层的编译原理。
2、原因分析
test.c
我们使用的是C语言的编译器gcc进行编译的,其中的函数print
编译之后,在符号表中的名字为 print
,通过nm查看.o文件.
$ gcc -c test.c
$ nm test.o
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T print
U printf
我们链接的时候采用的是 g++ 进行链接,也就是 C++ 链接方式,程序在运行到调用 print
函数的代码时,会在符号表中寻找 _Z5printii
(是按照C++的链接方法来寻找的,所以是找 _Z5printii
而不是找 print
)的名字,发现找不到,所以会提示“未定义的引用”
$ g++ -c test.c
$ ls
main.cpp makefile test.c test.h test.o
$ nm test.o
U _GLOBAL_OFFSET_TABLE_
U printf
0000000000000000 T _Z5printii
此时如果我们在对print
的声明中加入 extern “C”
,这个时候,g++编译器就会按照C语言的链接方式进行寻找,也就是在符号表中寻找print(这才是C++兼容C),这个时候是可以找到的,是不会报错的。
总结:
编译后底层解析的符号不同,C语言是 _print
,C++
是 __Z5printii
3、解决调用失败问题
修改test.h文件
#ifndef _TEST_H
#define _TEST_H
extern "C"{
void print(int a,int b);
}
#endif
修改后再次执行命令
gcc -c test.c
g++ -o main main.cpp test.o
./main
运行无报错
4、思考:那C语言能够调用C接口吗
实验:定义main.c
函数如下
#include <stdio.h>
#include "test.h"
int main()
{
printf("现在调用C语言函数\n");
print(3,4);
return 0;
}
重新执行命令如下
gcc -c test.c
gcc -o mian main.c test.o
报错:C语言里面没有extern “C“这种写法
5、C接口既能被C++调用又能被C调用
为了使得test.c
代码既能被C++调用又能被C调用
将test.h修改如下
#ifndef __TEST_H__
#define __TEST_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
extern void print(int a,int b);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __TEST_H__ */
ps:下期介绍一个Source Insight
的插件,快速生成上面的代码
再次执行命令
gcc -c test.c
gcc -o main main.c test.o
./main
结果示意:
来源:https://developer.51cto.com/art/202110/685147.htm
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java豆瓣电影爬虫——小爬虫成长记(附源码)
![](https://img.aspxhome.com/file/2023/7/84287_0s.png)
Spring实战之清除缓存操作示例
C# JSON格式化转换辅助类 ConvertJson
超简洁java实现双色球若干注随机号码生成(实例代码)
![](https://img.aspxhome.com/file/2023/9/91119_0s.png)
c# WPF中的TreeView使用详解
解决Android手机屏幕横竖屏切换
![](https://img.aspxhome.com/file/2023/4/104644_0s.png)
详解flutter中常用的container layout实例
![](https://img.aspxhome.com/file/2023/6/113306_0s.png)
在Mybatis中使用自定义缓存ehcache的方法
spring boot集成p6spy的最佳实践
![](https://img.aspxhome.com/file/2023/8/72908_0s.png)
mybatis快速入门学习教程新手注意问题小结
![](https://img.aspxhome.com/file/2023/0/128840_0s.png)
C#串口编程System.IO.Ports.SerialPort类
![](https://img.aspxhome.com/file/2023/3/119473_0s.png)
Android自定义圆环倒计时控件
![](https://img.aspxhome.com/file/2023/1/123351_0s.gif)
jax-ws handler 的详解及简单实例
Flutter学习之实现自定义themes详解
Java Synchronized锁失败案例及解决方案
![](https://img.aspxhome.com/file/2023/3/61863_0s.png)
Asp.Net中避免重复提交和弹出提示框的实例代码
JavaWeb实现文件上传下载功能实例详解
![](https://img.aspxhome.com/file/2023/3/62633_0s.jpg)
Java 导出Excel增加下拉框选项
Java C++实现相同MD5加密算法的方式
![](https://img.aspxhome.com/file/2023/5/58685_0s.jpg)