compiler-errors - 如何在#ifdef编译指令中将逻辑运算符用于预处理程序?

标签 compiler-errors syntax-error conditional preprocessor-directive mql4

我有以下代码使用预处理器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 command

error 149: '#else' - unexpected token


这是因为MQL语法完全不同,并且不支持这些预处理程序命令。

最佳答案

为什么这不起作用?

因为
条件 语法不符合MQL4preprocessor 的规范。
(而不是再次检查并交叉检查 MQL4 Documentation 部分:[ MQL4引用/语言基础/预处理程序/条件编译(#ifdef#ifndef#else#endif)])

Preprocessor conditional compilation directives allow compiling or skipping a part of the program depending on the fulfillment of a certain condition.

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/

相关文章:

java - ' "'和 "''在Java中..如何处理它们?

haskell - Haskell-同时打印并返回状态

php - 语法错误: PHP brackets issue for IF and FOREACH? [duplicate]

mysql - 基于 MySQL 中其他列的查询条件列

java - 将 List<Subclass> 传递给需要 List<SuperClass> 的方法

powershell - 从Powershell编译名称为1.0的gcc库

cmake - 参数列表中的 CMake 中的内联条件?

c - C 中的宏条件语句

c# - 编译器错误消息: CS0246: The type or namespace name could not be found

sql - Oracle存储过程运行错误