如果我有以下内容:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

#include <stdio.h>

extern int parse_string(FILE *, char const*, const unsigned int);

#endif

,点击 这是否意味着使用...... #include example.h ...的代码不必...... #include <stdio.h>的依赖关系? (即:<=>)

有帮助吗?

解决方案

正确 - 这就是为什么限制其他标头中包含的必要标头的良好做法。预处理器将替换<!>“#include <!>”;带有stdio.h内容的指令, 所以你的标题看起来像编译器:

/*
 * example.h
 */

#ifndef EXAMPLE
#define EXAMPLE

<contents of stdio.h>

extern int parse_string(FILE *, char const*, const unsigned int);

#endif

其他提示

但是如果代码依赖于<stdio>,它可能应该包括它。 (毕竟,<=>还有多重包含保护。)

如果你想要的是你的项目的主要包含,那么继续做一个,但它包括实际的标题和公共系统标题,但没有原型或声明或宏。也就是说,大型包括什么都不做,只包括。这样,随着程序的发展,各个模块可以自行决定。

是的,你可以这样做,它会产生预期的效果。

对于您的特定示例,您需要在<!> lt; stdio.h <!> gt;中声明FILE,因此最好包含它。

如果parse_string()原型使用size_t而不是unsigned intconst char *(对于文件名)而不是FILE *,我将包含<!> lt; stddef.h <!> gt;在<!> quot; example.h <!>“

#ifndef EXAMPLE
#define EXAMPLE
#include <stddef.h> /* size_t typedef */
extern int parse_string(const char *filename, char const*, const size_t);
#endif
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top