<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>
"); //-->

博客專(zhuān)欄

EEPW首頁(yè) > 博客 > #ifdef __cplusplus extern "C" { #endif”的定義

#ifdef __cplusplus extern "C" { #endif”的定義

發(fā)布人:電子禪石 時(shí)間:2020-02-18 來(lái)源:工程師 發(fā)布文章

平時(shí)我們在linux c平臺開(kāi)發(fā)的時(shí)候,引用了一些Cpp或者C的代碼庫,發(fā)現一些頭文件有如下代碼條件編譯。

復制代碼
#ifdef  __cplusplusextern "C" {#endif// 代碼#ifdef  __cplusplus
}#endif
復制代碼

這個(gè)是什么意思呢?一開(kāi)始看到這個(gè)也很茫然。上網(wǎng)查找了一些資料。

主要作用:

為了在C++代碼中調用用C寫(xiě)成的庫文件,就需要用extern"C"來(lái)告訴編譯器:這是一個(gè)用C寫(xiě)成的庫文件,請用C的方式來(lái)鏈接它們。

原因:

 C++支持函數重載,而C是不支持函數重載的,兩者語(yǔ)言的編譯規則不一樣。編譯器對函數名的處理方法也不一樣。

假設有這個(gè)一個(gè)函數原型:

void func(int a,int b)
{  //code  }

可能在C++編譯之后會(huì )產(chǎn)生_func_int_int之類(lèi)的名字,因為C++支持重載。而C編譯之后,可能為_(kāi)func。

關(guān)鍵字:extern "C" 表示編譯生成的內部符號名使用C約定。

復制代碼
//C++引用C函數的例子//test.c#include <stdio.h>void mytest()
{
printf("mytest in .c file ok\n");
}//main.cppextern "C"{void mytest();
}int main()
{
mytest();return 0;
}
復制代碼

復制代碼
//在C中引用C++函數//在C中引用C++語(yǔ)言中的函數和變量時(shí),C++的函數或變量要聲明在extern "C"{}里,但是在C語(yǔ)言中不能使用extern "C",否則編譯出錯。//test.cpp#include <stdio.h>extern "C"{void mytest()
{
printf("mytest in .cpp file ok\n");
}
}//main.cvoid mytest();int main()
{
mytest();return 0;
}
復制代碼

復制代碼
//綜合使用//一般我們都將函數聲明放在頭文件,當我們的函數有可能被C或C++使用時(shí),我們無(wú)法確定是否要將函數聲明在extern "C"里,所以,我們應該添加#ifdef __cplusplusextern "C"{#endif//函數聲明#ifdef __cplusplus
}#endif
復制代碼

如果我們注意到,很多頭文件都有這樣的用法,比如string.h,等等。

復制代碼
//test.h#ifdef __cplusplus
#include <iostream>using namespace std;extern "C"{#endifvoid mytest();
#ifdef __cplusplus
}#endif
復制代碼

 

這樣,可以將mytest()的實(shí)現放在.c或者.cpp文件中,可以在.c或者.cpp文件中include "test.h"后使用頭文件里面的函數,而不會(huì )出現編譯錯誤。

復制代碼
//test.c#include "test.h"void mytest()
{
#ifdef __cplusplus
cout << "cout mytest extern ok " << endl;#elseprintf("printf mytest extern ok n");#endif}//main.cpp#include "test.h"int main()
{
mytest();return 0;
}
復制代碼

參考博文:

http://www.cnblogs.com/HappyXie/archive/2011/01/07/1929369.html


*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。



關(guān)鍵詞:

相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>