应该使用间接标头包含的方式吗? [英] Should reliance on indirect header inclusion be used?

查看:47
本文介绍了应该使用间接标头包含的方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果项目的标头包含第二个标头,并且两个标头都包含一个公共标头,那么优良作法是从第一标头中删除对通用标头的包含,并依赖于通过第二标头的间接包含吗?

If a project has a header that includes a second header and both of them include a common header, is it good practice to drop the inclusion of the common header from the first header and rely on the indirect inclusion via the second header?

例如: 我知道可以从temperature.h中删除stdint.h ,但是应该是吗?

e.g.: I know the stdint.h can be removed from temperature.h, but should it be?

temperature.h中:

#include <stdint.h>  // *Should* this include be removed in this case.
#include "i2c.h"

extern uint16_t temperatureRead (i2cData_t x); 

i2c.h中:

#include <stdint.h>

typedef struct i2cData_t {
    uint16_t exampleMember
} i2cData_t;

推荐答案

通常,您希望模块是独立的.如果您的模块依赖于标题中的某些内容,则将其包括在内. Temperature.h可能有一天会决定不再需要包含stdint.h并将其删除.您的代码应该与该决定无关,因此请通过包含stdint.h来保护它,即成为独立的.

Generally you want your modules to be self contained. If your module relies on something in a header then include it. Temperature.h may one day decide it doesn't need to include stdint.h any more and have it removed. Your code should have nothing to do with that decision, so safeguard it by including stdint.h, i.e. be self contained.

使用标头保护符(或C ++编译指示一次)以确保编译速度不会由于多次包含标头而降低.

Use header guards (or C++ pragma once) to make sure your compilation speed doesn't degrade due to including a header multiple times.

这篇关于应该使用间接标头包含的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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