c __cplusplus詳解


Code:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
解釋:1.c++中定義了__cplusplus,C語言中沒有該定義。即:識別是c代碼還是c++代碼。

如下段代碼:

#include <stdio.h>

int main(int argc,char *argv[])

{

#ifdef __cplusplus

printf("This is a c ++ program!\n");

#endif

#ifndef __cplusplus

printf("This is a c program!");

#endif

reutrn 0;

}

分別編譯:gcc test.c

./a.out

g++ test.c

./a.out

看到程序輸出內容你便知道了。

解釋2.C語言和C++編譯出來的函數不用,調用extern "C"會讓c++編譯器按照c的編譯格式來編譯。多用於c++庫的頭文件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM