| 副标题[/!--empirenews.page--] 如何在C++代码中调用写好的C接口?你可能会奇怪,C++不是兼容C吗?直接调用不就可以了?这里我们先按下不表,先看看C++如何调用C代码接口。 
 C++如何调用C接口 为什么会有这样的情况呢?想象一下,有些接口是用C实现的,并提供了库,那么C++中该如何使用呢?我们先不做任何区别对待,看看普通情况下会发生什么意想不到的事情。 首先提供一个C接口: #include"test.h" void testCfun() {     printf("I am c funn");     return; } 
 为了简化,我们在这里就不将它做成静态库或者动态库了,我们在这里编译成C目标文件: gcc -c test.c 
 另外提供一个头文件test.h: #include<stdio.h> void testCfun(); 
 我们的C++代码调用如下: #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不一致,所以即便是函数名一样,对于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接口要被代码调用呢?增加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"{ 
 (编辑:宣城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |