模块化编程时,#include到底要放在哪里?

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

1、规则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中。当一个项目的代码量很大时,因为效率地下而多占用的时间就没法忽视了。spa

2、规则2:使#include出如今它被须要的地方code

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

===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中,定义了以下结构体定义:io

=====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文件中是必须的!

 

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

相关文章
相关标签/搜索