2013-08-16 91 views
2

我在makefile中傳遞了一個編譯器選項,名稱爲DPATH,它類似於DPATH=/path/to/somefile。基於此,我必須寫一個宏這樣的: -C中的預處理器條件中的字符串比較

#if "$(DPATH)"=="/path/to/x" 
#error no x allowed 
#endif 

如何比較DPATH在預處理器條件測試字符串?

+0

它爲什麼要在C代碼檢查些什麼呢?我把它放在makefile中。 – littleadv

+0

啊,因爲我在這裏和那裏定義了一些其他的宏,所以它實際上是'if $ DPATH && some1 == 3 && some2 == 7 && env_defined == 3.0' – Sayan

+1

這聽起來不對。 ..我認爲你的配置管理需要更多的思考。 – littleadv

回答

3

在預處理器中無法做到這一點。 #if只能計算不引用函數或變量的整數表達式。在宏擴展後存活的所有標識符都被替換爲零,並且字符串常量觸發自動語法錯誤。

不知道更多關於你的問題,我建議編寫編譯和構建過程中執行,和Makefile咕來構建失敗,如果測試沒有通過一個小的測試程序。

#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
    if (!strcmp(DPATH, "/path/to/x") || some1 == 3 || some2 == 7 || ...) 
    { 
     fputs("bogus configuration\n", stderr); 
     return 1; 
    } 
    return 0; 
} 

然後

all : validate_configuration 
validate_configuration: config_validator 
    if ./config_validator; then touch validate_configuration; else exit 1; fi 
config_validator: config_validator.c 
    # etc