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++庫的頭文件。