关于c++:C是如何调用C接口的

6次阅读

共计 2214 个字符,预计需要花费 6 分钟才能阅读完成。

✨有的时候,当我想在 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 testCfun
0000000000000000 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
$ ./main
start to call c function
I am c fun
end 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.c
In 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 __cplusplus
extern "C"{
#endif

void 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

正文完
 0