這個問題在10個月前我還需要去找上司,現在終于不用了。感覺好簡單的說。
問題是這樣,我引用了一個開源庫,包含的它的頭檔案,顯示宏沖突。開源庫叫做TAO.c、TAO.h,我自己的檔案叫OKAMOTO.c(大愛岡本多緒)。
1.define沖突
其中TAO.h中定義的宏和OKAMOTO.c中的沖突了,那肯定是ifndef啊,但是如果我把.h中的宏加上ifndef的話,會不會影響到TAO.c中原有的内容呢?我認為是不會的,問了幾個同僚,他們不确定,我就自己測試了一下,下面是内容
TAO.c
#include <stdio.h>
#include "TAO.h"
int main(void)
{
printf("x=%d\n",X);
return 0;
}
TAO.h
#ifndef X
#define X (1+1)
#endif
OKAMOTO.c
#include <stdio.h>
#define X (1+2)
#include "TAO.h"
int main(void)
{
printf("x=%d\n",X);
return 0;
}
原來确實是不會的,因為在編譯TAO.c的時候X是沒有定義的,是以會選擇進入ifndef,但是在執行OKAMOTO.c的時候宏已經定義過了,自然就不會ifndef,互相之間并不影響
2.typedef沖突
這個又有點不一樣,不能直接用ifndef了,比如
要是這麼寫就蠢了
#ifndef ULONG
typedef unsigned int ULONG;
#endif
那咋辦嘞,這個很有意思
先看OKAMOTO.c
#include <stdio.h>
#define X (1+2)
typedef unsigned long ULONG;
#define type_conflicting_ULONG
#include "TAO.h"
int main(void)
{
printf("x=%d\n",X);
return 0;
}
再看TAO.h
#ifndef X
#define X (1+1)
#endif
#ifndef type_conflicting_ULONG
typedef unsigned int ULONG;
#endif
有沒有感覺智商受到了侮辱,哈哈哈