C++实践小经验——#include 何时放在头文件里,何时放在cpp文件里?

结合我自己的经验,谈一谈模块化编程时#include应该出现的位置。总结起来大体有二条规则:

一、规则1:只包含必要的头文件

  看下面这个模块:

 

===foo.c====
#include <stdio.h>
#include <foo.h>
#include <uart.h>

​void foo ()
{
   printf ("hello world!\n");
}

 

===foo.h====
#ifndef __foo_h__
#define __foo_h__

extern void foo();

#endif

 

在foo()函数中也只有简单的一行打印语句。由于printf()函数的原型声明来源于stdio.h,因此foo.c中包含了stdio.h无可厚非,否则根本无法使用。但foo.c文件中除了包含stdio.h外还包含了另外一个多余的头文件 ——uart.h,这不会导致编译的出错,但我们并不需要使用uart.h中声明的接口,因此这种行为会导致编译效率的降低。如果此时uart.h中还包含了其它文件,那么全部都会在预处理时展开到foo.c中。当一个项目的代码量很大时,由于效率地下而多占用的时间就无法忽视了。

二、规则2:使#include出现在它被需要的地方

为了使用printf()函数,可以把#include<stdio.h>放在foo.h文件中,编译也可以正常通过,如下:

 

===foo.h====
#ifndef __foo_h__
#define __foo_h__

#include <stdio.h>

extern void foo();

#endif

 

===foo.c====

#include <foo.h>


void foo ()
{
   printf ("hello world!\n");
}

 

但这样会产生一个问题,stdio.h对于我们的头文件foo.h来说,是必须的么?当然不是!那么会导致什么样的问题呢?我们已经直到.h文件的作用相当于模块的使用说明书,如果其它模块要使用foo模块时,需要通过#include<foo.h>来添加接口函数,此时就会间接的包含了stdio.h,同样的会导编译致速度下降的问题。因此,正确的做法是将#include<stdio.h>放在foo.c中,如下:

 

===foo.h====
#ifndef __foo_h__
#define __foo_h__

extern void foo();

#endif


===foo.c====
#include <stdio.h>
#include <foo.h>


void foo ()
{
   printf ("hello world!\n");
}

 

理解了上面的例子,则看下面一条实例:

在system.h中,定义了如下结构体定义:

 

=====system.h=====

#ifndef __system_h__
#def __system_h__

typedef struct stuTAG{

    char * name;
    u8       age;

}stu_st;


#endif

 

同样在一个foo.c模块中,定义了一个函数:

 

===foo.h====
#ifndef __foo_h__
#define __foo_h__
#include "system.h"

extern void print_info(stu_st * student);

#endif

 

=====foo.c=====
#include "foo.h"
#include <stdio.h>

void print_info(stu_st * student)
{
      printf("name:%s\n",student->name);  
      printf("age :%d\n",student->age);    

}

 

从这个foo模块的实现代码来看,foo.h和foo.c都需要了解stu_st结构体的内容,也就是stu_st是被foo.c和foo.h同时需要的,因此它必须被放置在foo.h中,否则编译时会出现stu_st未定义的错误,此时将#include放置在.h文件中是必须的!

 

用一句话概括:只在必要的地方包含必要的头文件!

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值