✨有的时候,当我想在c++代码中调用c接口,而后就编译报错了!!!
引出问题
你可能会奇怪,C++不是兼容C吗?间接调用不就能够了?为什么会有这样的状况呢?设想一下,有些接口是用C实现的,并提供了库,那么C++中该如何应用呢?
咱们先不做任何区别对待,看看一般状况下会产生什么意想不到的事件。首先提供一个C接口:
//test.c#include"test.h"void testCfun(){ printf("I am c fun\n"); return;}
咱们在这里编译成C指标文件:
gcc -c test.c
另外提供一个头文件test.h:
#include<stdio.h>void testCfun();
咱们的C++代码调用如下:
//main.cpp#include"test.h"#include<iostream>using namespace std;int main(void){ /*调用C接口*/ cout<<"start to call c function"<<endl; testCfun(); cout<<"end to call c function"<<endl; return 0;}
编译:
$ g++ -o main main.cpp test.o/tmp/ccmwVJqM.o: In function `main':main.cpp:(.text+0x21): undefined reference to `testCfun()'collect2: error: ld returned 1 exit status
很可怜,最初的链接报错了,说找不到testCfun,然而咱们的确定义了这个函数。为什么会找不到呢?
揭开迷雾
咱们都晓得,C++中函数反对重载,而C并不反对。C++为了反对函数重载,它在“生成”函数符号信息时,不能仅仅通过函数名,因为重载函数的函数名都是一样的,所以它还要依据入参,命名空间等信息来确定惟一的函数签名。
或者说C++生成函数签名的形式与C不统一,所以即使是函数名一样,对于C和C++来说,它们最终的函数签名还是不一样。当然这里又是另外一回事了,咱们不细说。咱们看看两个文件里的函数符号有什么区别:
$ nm test.o|grep testCfun0000000000000000 T testCfun$ nm main.o|grep testCfun U _Z8testCfunv
所以它们两个能链接在一起才真是奇怪了呢!名字都不同,还怎么链接?
解决办法
那么如何解决呢?很显然,咱们必须通知链接器,这是一个C接口,而不是C++接口,所以须要退出 extern C,咱们批改test.h
#include<stdio.h>extern "C"{void testCfun();}
这里用extern "C"将testCfun接口包裹起来,通知编译器,这里的是C接口哈,你要按C代码的形式解决。再次编译:
$ g++ -o main main.cpp test.o$ ./mainstart to call c functionI am c funend to call c function
优化
尽管下面的C接口能够被C++失常调用了,然而如果这个C接口要被C代码调用呢?减少main.c内容如下:
//main.c#include"test.h"int main(void){ /*调用C接口*/ testCfun(); return 0;}
编译:
$ gcc -o main main.c test.cIn file included from main.c:2:0:test.h:2:8: error: expected identifier or '(' before string constant extern "C"{ ^In file included from test.c:2:0:test.h:2:8: error: expected identifier or '(' before string constant extern "C"{
不出意外,又报错了,很显然,C语言中并没有extern "C"这样的写法,所以为了能使得test.c的代码既能被C++调用,也能被C调用,须要改写成上面这样:
#include<stdio.h>#ifdef __cplusplusextern "C"{#endifvoid testCfun();#ifdef __cplusplus}#endif
这里通过__cplusplus宏来管制是否须要extern “C”,如果是C++编译器,那么extern "C"局部就会被预处理进去,这样test.c代码就能够既用于C++,也能够用于C啦。
为什么咱们在C++代码中能够间接调用一些规范C库函数呢?即便你在main函数中调用printf等函数,它也不会呈现链接谬误。因为库函数曾经有了相似的解决了。
总结
C++反对重载,而C不反对,C++并不能间接调用C代码写好的接口,因而如果你的C代码想要可能被C调用,也想被C++调用,那么别忘了extern "C"。
记住,c代码被c++调用,是须要用extern "C"
把被调用的函数的申明给括起来。
关注&&分割
gitee: https://gitee.com/cmcc-oneos/OneOS-Lite
docs: https://oneos-lite.com/
援用自:https://gitee.com/cmcc-oneos/OneOS-Lite/blob/dev/docs/quick_guide/cplusplus/c++-call-c.md