我一直在瘋狂地試圖讓下面的代碼工作。預處理器定義WIDR和LIDR都沒有工作,它給我的編譯錯誤:預處理器定義在C中不起作用
projects/elcain.c: In function ‘main’:
projects/elcain.c:17:6: error: ‘WIDR’ undeclared (first use in this function)
if (WIDR) {
^
projects/elcain.c:17:6: note: each undeclared identifier is reported only once for each function it appears in
projects/elcain.c:19:13: error: ‘LIDR’ undeclared (first use in this function)
} else if (LIDR) {
我真的沒有用預處理宏,或在一般的預處理器的經驗,這樣下去容易在我身上。
下面的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define WIDR 1
#elif defined _linux_
#define LIDR 1
#endif
int main() {
char* directory = (char*) malloc (1);
if (WIDR) {
strcpy(directory, "C:\\TEMP\\");
} else if (LIDR) {
strcpy(directory, "~/.temp/");
} else {
*directory = 0x00;
}
printf("%s\n", directory);
return 0;
}
無論'_WIN32'也不'_linux_'定義。 – tkausl
如果沒有定義'_WIN32',那麼'WIDR'也沒有定義。 Anywa,這是完全錯誤的,因爲'WIRD'和'LIDR'不能同時定義,但它們都在程序中使用。 –
順便說一句'(char *)malloc(1);'是荒謬的。想想看。 –