我有以下代码使用预处理器conditional compilation directives:
#define foo
#define bar
#ifdef foo || !bar
extern bool Verbose = FALSE;
#else
extern bool Verbose = TRUE;
#endif
void start() {
}
由于以下错误而无法编译:test.mq4(3,12) : error 175: '
||
' - expressions are not allowed on a global scope
但是,当注释掉第一行(foo)时,代码可以正常编译,在这种情况下(未定义foo时)似乎编译器允许在全局范围内使用此表达式。
您可以尝试使用
mql
compilator编译以上代码(在Linux下,使用wine
):mql.exe /mql4 test.mq4
所以问题是:为什么不起作用和我如何以适当的方式定义上述预处理条件(
foo || !bar
)?我还尝试了以下语法:
#if defined (foo) || defined (!bar)
如user2357112(GNU cpp syntax)先前建议的那样,但失败并出现以下错误:error 109: '
#if
' - invalid preprocessor commanderror 149: '
#else
' - unexpected token
这是因为MQL语法完全不同,并且不支持这些预处理程序命令。
最佳答案
为什么这不起作用? Preprocessor conditional compilation directives allow compiling or skipping a part of the program depending on the fulfillment of a certain condition.
因为
条件 语法不符合MQL4preprocessor 的规范。
(而不是再次检查并交叉检查 MQL4 Documentation
部分:[ MQL4
引用/语言基础/预处理程序/条件编译(#ifdef
,#ifndef
,#else
,#endif
)])
That condition can
take one of the following forms.#ifdef
identifier
// ... the code located here is compiled
// ... if identifier
has already been defined for the preprocessor in #define directive.#endif
#ifndef
identifier
// ... the code located here is compiled
// ... if identifier
is not currently defined by preprocessor #define directive.#endif
在此MQL4预处理程序上下文中, condition
是静态的,封闭的一组公认的语法指令构造函数的成员, {
#ifdef | #ifndef | #else
}
在此MQL4预处理程序上下文中, identifier
是常量term
,而不是表达式。
Q.E.D.
合适的方法吗?
仍然可以选择“手动”构建缺少的逻辑灵活性:#ifdef bar_identifier
#ifndef foo_indentifier
extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = True;
#else
extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = False;
#endif
#else
extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = False;
#endif
关于compiler-errors - 如何在#ifdef编译指令中将逻辑运算符用于预处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048046/