C++ - C与C++相互调用 extern “C“

1.C与C++相互调用

-> 实际工程中C++和C代码相互调用是不可避免的。
-> C++编译器能够兼容C语言的编译方式。
-> C++编译器会优先使用C++编译的方式。
-> extern关键字能强制让C++编译器进行C方式的。

同一套代码,用gcc -c -o,g++ -c -o编译后,再objdump -d反汇编得到.i文件。

在这里插入图片描述

 同样的源码,编译后生成的二进制代码其实是一样的,所以功能其实也是一样的所以本质上是可以混合编程的,但是生成的中间符号名称不同,所以链接器难受。

2.C与C++相互调用的三种情况

-> 同一个项目全部有源码即C源码和C++源码,一次编译链接。

-> 同一个项目中C是库,C++是源码,C++调用C。

-> 同一个项目中C++是库,C是源码,C调用C++。

(1)全部有源码即C源码和C++源码

有clib.c,clib.h,main.cpp,c和c++都有源码,并且在main.cpp中调用clib.c的函数。

 clib.h:

#ifndef     __CLB_H__
#define     __CLB_H__	

#ifdef      __cplusplus
extern "C"
{
#endif

//函数声明
int add(int a, int b);

#ifdef      __cplusplus
}
#endif

#endif

clib.c:

#include "clib.h"

int add(int a, int b)
{
	return a + b;
}

main.cpp:

#include "clib.h"
#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
	int x = 3, y = 5, ret = 0;
	ret = add(x,y);

	cout << "ret = " << ret <<endl;
	return 0;
}

在C的头文件中加extern "C"声明,先用gcc编译C文件生成.o文件再用g++编译生成最终执行文件。

在这里插入图片描述

(2) C++调用C

用clib.c制作成静态库libclib.a,提供clib.h,main.cpp,并且在main.cpp中调用clib.c的函数。

#include "clib.h"

int add(int a, int b)
{
	return a + b;
}

制作静态库:

在这里插入图片描述

clb.h:

#ifndef     __CLB_H__
#define     __CLB_H__	

#ifdef      __cplusplus
extern "C"
{
#endif

//函数声明
int add(int a, int b);

#ifdef      __cplusplus
}
#endif

#endif

 main.cpp:

#include "clib.h"
#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
	int x = 3, y = 5, ret = 0;
	ret = add(x,y);

	cout << "ret = " << ret <<endl;
	return 0;
}

用g++编译连接,成功执行:

在这里插入图片描述

(3)C调用C++

用cpp写一个封装层,用上extern “C”,cppaddwrapper.cpp和cppaddwrapper.hpp,用g++编译成静态库:

cppaddwrapper.hpp:

#ifndef __CPPADDWRAPPER_HPP__
#define __CPPADDWRAPPER_HPP__

#ifdef __cplusplus
extern "C"
{
#endif

int addwrapper(int a, int b);

#ifdef __cplusplus
}
#endif

#endif

cppaddwrapper.cpp:

#include "cppaddwrapper.hpp"
#include "cppadd.hpp"    //要包含库的头文件

int addwrapper(int a, int b)   //中间层的函数参数要和要调用的函数的参数一致
{
	add(a,b);
}

在这里插入图片描述

test.c调用我们制作的中间层库,用gcc编译链接,运行查看结果 :

#include "cppaddwrapper.hpp"
#include <stdio.h>

int main(int argc, char const *argv[])
{
	int x = 4, y = 5, ret = 0;
	ret = addwrapper(x,y);

	printf("ret = %d\n", ret);
	
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式_笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值