#include在size_t的头文件中 [英] #include in header file for size_t

查看:138
本文介绍了#include在size_t的头文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个带有size_t成员的结构:

/ * foo.h * /

struct foo {

char const *吧;

size_t barlen;

};

void make_foo(struct foo * p);

现在我#include它在需要的地方

/ * foo.c * /

#include" foo.h"

void make_foo(struct foo * p){

p-> bar =" immutable string";

p-> barlen = 16;

}

如果我现在编译这些文件,我会在'size_t"''之前得到'解析错误。

要编译我需要#include< stdlib.h>。 />

因为foo.c不需要stdlib.h中的任何内容我想在foo.h中包含

但是后来记住头文件不应该包括其他

头文件,所以我尝试在两个文件中包含stdlib.h,这两个实验都工作了




这可能是关于不包括

中的头文件的规则的例外头文件?

[没关系,但为了完整起见,我正在使用gcc 3.3.5]

[编译命令行:gcc - W -Wall -std = c89 -pedantic -c foo.c]


-

如果您通过Google发帖,请阅读< http: //cfaj.freeshell.org/google>

解决方案

Pedro Graca< he **** @ dodgeit.com>写道:

想象一下我有一个带有size_t成员的结构:

/ * foo.h * /
struct foo {
char const * bar ;
size_t barlen;
};
void make_foo(struct foo * p);

现在我把#include到需要的地方

/ * foo.c * /
#include" foo.h"
void make_foo(struct foo * p){
p-> bar =" immutable string" ;;
p-> barlen = 16;
}

如果我现在编译这些文件,我会在size_t之前得到一个解析错误。
要编译我需要#include< stdlib.h>。

因为foo.c不需要来自stdlib.h的任何东西我想过把它包含在foo.h中然后记得头文件不应该包含其他
头文件,所以我尝试在两个文件中包含stdlib.h,这对两个实验都有效。

这可能是一个例外吗?关于不包括头文件的规则在
头文件中?




你在哪里知道有这样的规则?


在头文件中包含头文件没有任何问题。

如果你需要,请不要这样做,如果你不这样做,就不要这样做。

大多数(或所有?)头文件应该包含防护装置,这样他们就可以安全地多次包含
。例如:


=================================== =====

#ifndef FOO_H

#define FOO_H


struct foo {

char const * bar;

size_t barlen;

};

void make_foo(struct foo * p);


#endif / * FOO_H * /

============================== ==========


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Keith Thompson说:

在头文件中包含头文件没有错文件。
如果你需要的话,不要这样做,如果你不这样做,就不要这样做。

大多数(或所有?)头文件应该包含防护,这样他们就能<安全地多次包括在内。例如:

======================================= =
#ifndef FOO_H
#define FOO_H

struct foo {
char const * bar;
size_t barlen;
};
void make_foo(struct foo * p);

#endif / * FOO_H * /
==================== ====================




你忘了#include< stddef.h> :-)


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


>大多数(或所有?)头文件应该包含防护,这样他们就可以安全地多次包含

。例如:




其中一些不是:< assert.h>,一个。唯一一次包括警卫

不应该被真正使用的是如果一个标题可以被多次包含

在同一个文件中并且每次都有不同的效果。例如,我曾经设计了一个动态数组头,我没有把它包括在内,因为它需要定义的东西才能工作并且可以包含在内。 >
在同一个文件中多次为不同类型的动态数组。


不过,它是例外,而不是规则。如果一个包含文件不应该包含警卫,那么程序员应该在评论中这样说。


Gregory Pietsch


Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"''.
To compile I need to #include <stdlib.h>.

As foo.c doesn''t need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn''t include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?
[ It doesn''t matter, but for completeness sake, I''m using gcc 3.3.5 ]
[ compile command-line: gcc -W -Wall -std=c89 -pedantic -c foo.c ]

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>

解决方案

Pedro Graca <he****@dodgeit.com> writes:

Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"''.
To compile I need to #include <stdlib.h>.

As foo.c doesn''t need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn''t include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?



Where did you get the idea that there''s such a rule?

There''s nothing wrong with including header files in header files.
Do it if you need to, don''t do it if you don''t.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

========================================
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
========================================

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson said:

There''s nothing wrong with including header files in header files.
Do it if you need to, don''t do it if you don''t.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

========================================
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
========================================



You forgot to #include <stddef.h> :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


> Most (or all?) header files should have include guards so they can

safely be included multiple times. For example:



Some of them don''t: <assert.h>, for one. The only time include guards
shouldn''t really be used is if a header can be included multiple times
in the same file and have different effects each time. For example, I
once designed a dynamic array header that I didn''t put include guards
in because it needed things defined to work and could be included
multiple times in the same file for dynamic arrays of different types.

Still, it''s the exception, not the rule. If an include file should not
have include guards, the programmer should say so in the comments.

Gregory Pietsch


这篇关于#include在size_t的头文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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