-D MACRO和MACRO的#define的precedence [英] Precedence of -D MACRO and #define MACRO

查看:243
本文介绍了-D MACRO和MACRO的#define的precedence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 C 文件foo.c的,虽然我已经给 -Dmacro = 1 作为编译命令行选项。但是,如果在头文件内也我

If I have a C file foo.c and while I have given -DMACRO=1 as command line option for compilation. However, if within the header file also I have

#define MACRO 2

其中哪些将获得precedence?

Which of these will get precedence?

推荐答案

命令行选项适用于从文件中读取任何行前进。文件内容适用于书写的顺序。在一般情况下,如果任何宏观被重新定义,你会得到至少一个警告,不管命令行是否涉及。警告如果可以重新定义不要紧,也许是因为这两个定义是相同的保持沉默。

The command line options apply ahead of any line read from a file. The file contents apply in the order written. In general, you will get at least a warning if any macro is redefined, regardless of whether the command line is involved. The warning may be silenced if the redefinition doesn't matter, perhaps because both definitions are identical.

要回答这样一个问题的正确方法是建立一个小的测试案例和尝试。例如,在q3965956.c把以下内容:

The right way to answer a question like this is to build a small test case and try it. For example, in q3965956.c put the following:

#define AAA 2
AAA

和通过C preprocessor与的gcc -E 运行它,也许是:

and run it through the C preprocessor, perhaps with gcc -E:


C:>gcc -DAAA=42 -E q3965956.c
# 1 "q3965956.c"
# 1 ""
# 1 ""
# 1 "q3965956.c"
q3965956.c:1:1: warning: "AAA" redefined
:1:1: warning: this is the location of the previous definition

2

C:>

您可以从输出中看到,宏扩展到了文件中的的#define 给定的值。此外,还可以从指令的序列内置的定义和命令行均被 q3965956的1号线的任何内容之前处理看.C

You can see from the output that the macro expanded to the value given by the #define in the file. Furthermore, you can see from the sequence of # directives that built-in definitions and the command line were both processed before any content of line 1 of q3965956.c.

这篇关于-D MACRO和MACRO的#define的precedence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆