C++中是如何調(diào)用C接口的?
如何在C++代碼中調(diào)用寫好的C接口?你可能會(huì)奇怪,C++不是兼容C嗎?直接調(diào)用不就可以了?這里我們先按下不表,先看看C++如何調(diào)用C代碼接口。
C++如何調(diào)用C接口
為什么會(huì)有這樣的情況呢?想象一下,有些接口是用C實(shí)現(xiàn)的,并提供了庫(kù),那么C++中該如何使用呢?我們先不做任何區(qū)別對(duì)待,看看普通情況下會(huì)發(fā)生什么意想不到的事情。
首先提供一個(gè)C接口:
- #include"test.h"
- void testCfun()
- {
- printf("I am c fun\n");
- return;
- }
為了簡(jiǎn)化,我們?cè)谶@里就不將它做成靜態(tài)庫(kù)或者動(dòng)態(tài)庫(kù)了,我們?cè)谶@里編譯成C目標(biāo)文件:
- gcc -c test.c
另外提供一個(gè)頭文件test.h:
- #include<stdio.h>
- void testCfun();
我們的C++代碼調(diào)用如下:
- #include"test.h"
- #include<iostream>
- using namespace std;
- int main(void)
- {
- /*調(diào)用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
很不幸,最后的鏈接報(bào)錯(cuò)了,說(shuō)找不到testCfun,但是我們確實(shí)定義了這個(gè)函數(shù)。為什么會(huì)找不到呢?現(xiàn)在你還會(huì)認(rèn)為C++直接就可以調(diào)用C接口了嗎?
真相
我們都知道,C++中函數(shù)支持重載,而C并不支持。C++為了支持函數(shù)重載,它在“生成”函數(shù)符號(hào)信息時(shí),不能僅僅通過(guò)函數(shù)名,因?yàn)橹剌d函數(shù)的函數(shù)名都是一樣的,所以它還要根據(jù)入?yún)ⅲ臻g等信息來(lái)確定唯一的函數(shù)簽名。或者說(shuō)C++生成函數(shù)簽名的方式與C不一致,所以即便是函數(shù)名一樣,對(duì)于C和C++來(lái)說(shuō),它們最終的函數(shù)簽名還是不一樣。當(dāng)然這里又是另外一回事了,我們不細(xì)說(shuō)。我們看看兩個(gè)文件里的函數(shù)符號(hào)有什么區(qū)別:
- $ nm test.o|grep testCfun
- 0000000000000000 T testCfun
- $ nm main.o|grep testCfun
- U _Z8testCfunv
所以它們兩個(gè)能鏈接在一起才真是奇怪了呢!名字都不同,還怎么鏈接?
如何處理
那么如何處理呢?很顯然,我們必須告訴鏈接器,這是一個(gè)C接口,而不是C++接口,所以需要加入 extern C,我們修改test.h
- #include<stdio.h>
- extern "C"{
- void testCfun();
- }
這里用extern "C"將testCfun接口包裹起來(lái),告訴編譯器,這里的是C代碼哈,你要按C代碼的方式處理。再次編譯:
- $ g++ -o main main.cpp test.o
- $ ./main
- start to call c function
- I am c fun
- end to call c function
看終端輸出,完美!
優(yōu)化
雖然上面的C接口可以被C++正常調(diào)用了,但是如果這個(gè)C接口要被代碼調(diào)用呢?增加main.c內(nèi)容如下
- //main.c
- #include"test.h"
- int main(void)
- {
- /*調(diào)用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"{
不出意外,又報(bào)錯(cuò)了,很顯然,C語(yǔ)言中并沒有extern "C"這樣的寫法,所以為了能使得test.c的代碼既能被C++調(diào)用,也能被C調(diào)用,需要改寫成下面這樣:
- #include<stdio.h>
- #ifdef __cplusplus
- extern "C"{
- #endif
- void testCfun();
- #ifdef __cplusplus
- }
- #endif
這里通過(guò)__cplusplus宏來(lái)控制是否需要extern “C”,如果是C++編譯器,那么extern "C"部分就會(huì)被預(yù)處理進(jìn)去,這樣test.c代碼就可以既用于C++,也可以用于C啦。
趕快去你的C項(xiàng)目代碼頭文件中看看,是不是也有這樣的代碼段呢?
問(wèn)題
為什么我們?cè)贑++代碼中可以直接調(diào)用一些標(biāo)準(zhǔn)C庫(kù)函數(shù)呢?即使你在main函數(shù)中調(diào)用printf等函數(shù),它也不會(huì)出現(xiàn)鏈接錯(cuò)誤。因?yàn)閹?kù)函數(shù)已經(jīng)有了類似的處理了。
如果你還是不確定,你可以先預(yù)處理:
- $ g++ -E main.i main.cpp
去生成的main.i文件中找一找,是不是有extern "C"。
總結(jié)
C++支持重載,而C不支持,C++并不能直接調(diào)用C代碼寫好的接口,因此如果你的C代碼想要能夠被C調(diào)用,也想被C++調(diào)用,那么別忘了extern "C"。