本文介绍了在用C preprocessor条件字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的makefile DPATH 编译器选项调用,这是像 DPATH = /路径/要/ somefile 。基于此,我必须写一个宏这样: -

I am passing a compiler option in makefile called DPATH, which is something like DPATH=/path/to/somefile. Based on this, I have to write a macro such that:-

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

如何比较 DPATH 在一个preprocessor条件测试字符串?

How do I compare DPATH with the string in a preprocessor conditional test?

推荐答案

这是不可能做到这一点在preprocessor。 #如果只能评估整数EX pressions使得没有提及函数或者变量。幸存下来宏扩展所有标识符由零取代,一个字符串常量触发自动语法错误。

It is not possible to do this in the preprocessor. #if can only evaluate integer expressions making no reference to functions or variables. All identifiers that survive macro expansion are replaced by zeroes, and a string constant triggers an automatic syntax error.

不知道更多关于你的问题,我建议写一个编译的一个小的测试程序,并在生成过程中执行的,和Makefile咕失败的构建,如果测试没有通过。

Without knowing more about your problem, I would suggest writing a tiny test program that is compiled and executed during the build, and Makefile goo to fail the build if the test doesn't pass.

#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 

这篇关于在用C preprocessor条件字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:48