簡體   English   中英

#if 定義(WIN32)和#ifdef(WIN32)之間的區別

[英]difference between #if defined(WIN32) and #ifdef(WIN32)

我正在編譯將在 linux gcc 4.4.1 C99 上運行的程序。

我只是把我的#defines 放在分開的代碼中,這些代碼將在 windows 或 linux 上編譯。 但是,我收到了這個錯誤。

error: macro names must be identifiers.

使用此代碼

#ifdef(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

但是,當我更改為該錯誤時,錯誤已修復:

#if defined(WIN32)
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

我只是想知道為什么會出現這個錯誤以及為什么 #defines 不同?

非常感謝,

如果您使用 #ifdef 語法,請刪除括號。

兩者的區別在於#ifdef只能使用一個條件,
#if defined(NAME)可以做復合條件。

例如在你的情況下:

#if defined(WIN32) && !defined(UNIX)
/* Do windows stuff */
#elif defined(UNIX) && !defined(WIN32)
/* Do linux stuff */
#else
/* Error, both can't be defined or undefined same time */
#endif
#ifdef FOO

#if defined(FOO)

是相同的,

但是一次做幾件事,你可以使用定義的,比如

#if defined(FOO) || defined(BAR)

#ifdef檢查是否定義了具有該名稱的宏, #if計算表達式並檢查是否為真值

#define FOO 1
#define BAR 0

#ifdef FOO
#ifdef BAR
/* this will be compiled */
#endif
#endif

#if BAR
/* this won't */
#endif

#if FOO || BAR
/* this will */
#endif
#ifdef WIN32
/* Do windows stuff
#elif(UNIX)
/* Do linux stuff */
#endif

是正確的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM