包含头文件中的 C 预处理器

发布于 2024-08-20 21:12:44 字数 1059 浏览 2 评论 0原文

我在名为 data.h 的头文件中定义了一个结构。

我将 data.h 包含在 myfile.c 中。

在结构中,我用以下命令封锁了部分变量:

#ifndef TEST
 int x;
#endif

在 myfile.c 中,我有:

#ifdef TEST
localx++;
#else
mystruct.x++; //<-compiler complains on this line when compiling
#endif

当我尝试使用 -DTEST 进行编译时,编译器抱怨 mystruct 类型不包含名为 x 的字段。这是怎么回事?

我手边没有 C 编译器,所以这是我刚刚输入的内容:

in data.h

typdef struct {

#ifndef TEST
int x;
#endif
int y;
} coords;

in myfile.c

#include "data.h" 
static coords coord1;
int localx;

int main( ) 
{ 
#ifdef TEST
  localx = 1;
#else
  coord1.x = 1;
#endif

  coord1.y = 2;
  printf("%i\n", coord1.x);
  printf("%i\n", coord1.y);
  printf("%i\n", localx);

  return 0; 
} 

当我输入 cc myfile.c 时会进行编译但不适用于 cc myfile.c -DTEST 我正在使用此处引用的 MIPSPro C 编译器

I have a structure defined in a header file called data.h.

I am including data.h in myfile.c.

In the structure, I have part of the variables blocked off with:

#ifndef TEST
 int x;
#endif

and in myfile.c I have:

#ifdef TEST
localx++;
#else
mystruct.x++; //<-compiler complains on this line when compiling
#endif

When I try to compile with -DTEST I get a compiler complaining that mystruct type does not containing a field called x. What is up with this?

I don't have a C compiler handy, so here is what I just typed up:

in data.h

typdef struct {

#ifndef TEST
int x;
#endif
int y;
} coords;

in myfile.c

#include "data.h" 
static coords coord1;
int localx;

int main( ) 
{ 
#ifdef TEST
  localx = 1;
#else
  coord1.x = 1;
#endif

  coord1.y = 2;
  printf("%i\n", coord1.x);
  printf("%i\n", coord1.y);
  printf("%i\n", localx);

  return 0; 
} 

This compiles when I type cc myfile.c but not with cc myfile.c -DTEST
I am using the MIPSPro C compiler referenced here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-08-27 21:12:44

您最近的编辑(当任何人阅读本文时可能会有所不同)将在包含一堆 printf() 语句的部分中遇到问题。 该行:

 printf("%i\n", coord1.x);

无论 TEST 预处理器宏的设置如何, 都会引用结构的 x 成员。它也需要位于条件编译部分内,以便在 x 成员不存在时正确编译(而不是根本不编译)。

You most recent edit (which may well be different by the time anyone reads this) will have a problem in the section that has a bunch of printf() statements. The line:

 printf("%i\n", coord1.x);

is referencing the x member of the struct regardless of the setting of the TEST preprocessor macro. It needs to be inside a conditional compilation section too in order to compile correctly (rather not compile at all) when the x member doesn't exist.

美人骨 2024-08-27 21:12:44

由于您对字段 x 使用 ifndef,因此仅在未定义 TEST 时才可使用!

#ifdef 仅当指定为参数的宏已定义时才允许编译程序的一部分,无论其值是什么。例如:

#ifdef TABLE_SIZE

int table[TABLE_SIZE];

#endif  

在本例中,代码行 int table[TABLE_SIZE];仅当 TABLE_SIZE 先前使用 #define 定义时才编译,与其值无关。如果未定义,该行将不会包含在程序编译中。

#ifndef 的作用恰恰相反:只有在先前未定义指定的标识符时,才会编译 #ifndef 和 #endif 指令之间的代码。例如:

#ifndef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];

在这种情况下,如果到达这段代码时,TABLE_SIZE宏尚未定义,它将被定义为值100。如果它已经存在,它将保留自#define指令以来的先前值不会被执行。

来自:http://www.cplusplus.com/doc/tutorial/preprocessor/

Since you are using ifndef for the field x, it is only there to use if TEST is not defined!!

#ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been defined, no matter which its value is. For example:

#ifdef TABLE_SIZE

int table[TABLE_SIZE];

#endif  

In this case, the line of code int table[TABLE_SIZE]; is only compiled if TABLE_SIZE was previously defined with #define, independently of its value. If it was not defined, that line will not be included in the program compilation.

#ifndef serves for the exact opposite: the code between #ifndef and #endif directives is only compiled if the specified identifier has not been previously defined. For example:

#ifndef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];

In this case, if when arriving at this piece of code, the TABLE_SIZE macro has not been defined yet, it would be defined to a value of 100. If it already existed it would keep its previous value since the #define directive would not be executed.

From: http://www.cplusplus.com/doc/tutorial/preprocessor/

各空 2024-08-27 21:12:44

除了拼写错误(typdef)之外,您的示例使用 gcc 对我来说编译得很好。

编辑:
新示例不应编译。您需要将每个对“x”的引用包装在#ifdef 指令中。

另外,gcc 在文件列表之前接受 -D 标志,但我无权访问 MIPSpro。文档说您的命令行出现故障。

Except for the typo (typdef), your example compiles fine for me using gcc.

Edit:
The new example shouldn't compile. You need to wrap every reference to "x" in #ifdef directives.

Also, gcc accepts the -D flag before the file list, but I don't have access to MIPSpro. The docs say you have the command line out of order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文