Preprocessor Support for Linking to C

Rate this post

C++ Primer 4/e在Inherently Nonportable Features這個地方有一個單元:『It can be useful sometimes to compile the same source file in both C or C++. The preprocessor name __cplusplus (two underscores) is automatically defined when compiling C++, so we can conditionally include code based on whether we are compiling C++.

     #ifdef __cplusplus
     // ok: we're compiling C++
     extern "C"
     #endif
     int strcmp(const char*, const char*);

中文版的這樣寫:『有時候以C和C++編譯同一份源碼是有用的。以C++編譯時,預處理器名稱 __cplusplu(雙底線)會被自動定義,因此我們可以根據當前是否正在進行C++編譯而含入不同程式碼。

#ifdef __cplusplus
//好,我們正在編譯C++
extern “C”
#endif
int strcmp(const char*,const char*);

對於各種不同程式語言的連結,ㄚ琪非常的感興趣。

本站部分內容包含聯盟行銷連結。當您透過連結購買商品或服務時,本站將獲得微薄的佣金,這並不會影響您的購買價格,但能支持本站的運作,感謝您的支持。問題詢問

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *