1.不要重复包含头文件

--以上出自《C语言程序设计:现代方法(第2版)》

f3.h

//#ifndef AE_OK
#define AE_OK 0
typedef int        ngx_int_t;
//#endif

f2.h

#include "f3.h"

f1.h

#include "f3.h"

test.c

#include <stdio.h>#include "f1.h"
#include "f2.h"int main(){ngx_int_t a1=1;printf("%d",AE_OK);printf("%d",a1);return 0;
}

编译不过去:

导出预编译文件:

...以上省略
# 2 "test.c" 2# 1 "f1.h" 1
# 1 "f3.h" 1typedef int ngx_int_t;
# 1 "f1.h" 2
# 4 "test.c" 2
# 1 "f2.h" 1
# 1 "f3.h" 1typedef int ngx_int_t;
# 1 "f2.h" 2
# 5 "test.c" 2int main(){ngx_int_t a1=1;printf("%d",0);printf("%d",a1);return 0;
}

如果我们在f3.h中增加#ifndef就不会出问题了,直接导出预编译文件:

# 2 "test.c" 2# 1 "f1.h" 1
# 1 "f3.h" 1typedef int ngx_int_t;
# 1 "f1.h" 2
# 4 "test.c" 2
# 1 "f2.h" 1
# 5 "test.c" 2int main(){ngx_int_t a1=1;printf("%d",0);printf("%d",a1);return 0;
}

2. .c文件编译注意不要重复引入

这个是我从redis源码中抽取其事件库的编译

在redis源码的ae.c文件:

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>
#include <time.h>
#include <errno.h>#include "ae.h"
#include "zmalloc.h"
#include "config.h"/* Include the best multiplexing layer supported by this system.* The following should be ordered by performances, descending. */
#ifdef HAVE_EVPORT
#include "ae_evport.c"
#else#ifdef HAVE_EPOLL#include "ae_epoll.c"#else#ifdef HAVE_KQUEUE#include "ae_kqueue.c"#else#include "ae_select.c"#endif#endif
#endifaeEventLoop *aeCreateEventLoop(int setsize) {aeEventLoop *eventLoop;int i;if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;eventLoop->setsize = setsize;eventLoop->lastTime = time(NULL);eventLoop->timeEventHead = NULL;eventLoop->timeEventNextId = 0;eventLoop->stop = 0;eventLoop->maxfd = -1;eventLoop->beforesleep = NULL;if (aeApiCreate(eventLoop) == -1) goto err;/* Events with mask == AE_NONE are not set. So let's initialize the* vector with it. */for (i = 0; i < setsize; i++)eventLoop->events[i].mask = AE_NONE;return eventLoop;err:if (eventLoop) {zfree(eventLoop->events);zfree(eventLoop->fired);zfree(eventLoop);}return NULL;
}

HAVE_EPOLL是前面定义的:

/* Test for polling API */
#ifdef __linux__
#define HAVE_EPOLL 1
#endif

在ae_epoll.c中的aeApiCreate函数

#include <sys/epoll.h>typedef struct aeApiState {int epfd;struct epoll_event *events;
} aeApiState;static int aeApiCreate(aeEventLoop *eventLoop) {aeApiState *state = zmalloc(sizeof(aeApiState));if (!state) return -1;state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize);if (!state->events) {zfree(state);return -1;}state->epfd = epoll_create(1024); /* 1024 is just an hint for the kernel */if (state->epfd == -1) {zfree(state->events);zfree(state);return -1;}eventLoop->apidata = state;return 0;
}

我想抽取出redis的事件库

一开始不知道include的.c文件编译的时候不需要重复引入,不然编译报错:

ae_epoll.c不用被引入,因为在ae.c已经引入了。

成功编译:

生成了redis文件。

2. .h文件编译注意不要重复引入

报错anet.h:47: 错误:expected declaration specifiers or ‘...’ before ‘size_t’

原因是anet.h头文件重复引用,去掉anet.h,重新编译就可以了:

原因分析:

b.h中#include "../../a.h" 而a.h中的函数声明中用到了b.h中的结构体或者typedef,那么就会出现在包含a.h的时候b.h中的结构体或者typedef还没有声明,从而陷入错误。

C语言不要重复包含.h头文件和.c文件相关推荐

  1. c语言 自动包含头文件,C语言不要重复包含.h头文件和.c文件

    http://blog.csdn.net/unix21/article/details/8450235 2012 1.不要重复包含头文件 --以上出自<C语言程序设计:现代方法(第2版)> ...

  2. graphics.h头文件_C语言图形(graphics.h头文件功能和示例)

    graphics.h头文件 C中的颜色说明 (Color Description in C) setbkcolor sets the background to the color specified ...

  3. 文件 单片机_单片机C语言编程中reg52.h头文件的作用

    前言:本人出于爱好将不定期发送电气电工.前端.单片机等内容,可能会无法顾及关注我的所有人需求,请大家按需收藏自己想要知识,有用则收之,无用则弃之,不系统更新,仅供零星学习O(∩_∩)O哈哈~ 在代码的 ...

  4. 套头文件html重复,关于C++头文件重复包含的问题

    #ifndef PEOPLE_H #define PEOPLE_H /* - */ #endif 这样写能够防止头文件被重复包含 在头文件中定义变量不是不规范,而是一种错误. 原因在于 如果在head ...

  5. 如何避免头文件被重复包含?

    在实际的工程中我们很有可能重复包含某一个头文件,比如下面这种情况: 开发人员B和开发人员C在自己的头文件中都包含了开发人员A的头文件,而开发人员D在自己的编译单元中包含了B和C的头文件,此时D就相当于 ...

  6. string类 string.h头文件 cstring头文件区别以及读取一行字符串总结

    以前一直分不清string类  string.h头文件 cstring头文件的去别,今天ce了一发才稍微弄懂了. 首先C语言中只有string.h头文件,string.h包含了一些字符数组和字符串的函 ...

  7. C语言头文件避免重复包含

    C语言头文件避免重复包含 假定有以下几个头文件及其包含关系为: File1.h,file2.h,file3.h,file4.h,file5.h,main.cpp 那么:file3.h包含file1.h ...

  8. C语言 define 防止头文件重复包含 - C语言零基础入门教程

    目录 一.头文件重复包含编译器报错 1.简单的理解头文件重复包 2.老流氓的理解头文件重复包 二.通过宏定义解决头文件重复包含 1.通过 #ifndef / #define 解决头文件重复包含 2.通 ...

  9. C语言头文件为什么要加#ifndef #define #endif(防止头文件重复包含)

    当你用VC的菜单新增一个类,你会发现自动生成的代码总是类似下面的样子: #if !defined(AFX_XXXX__INCLUDED_)#define AFX_XXXX__INCLUDED_具体代码 ...

最新文章

  1. 微信小程序使用npm 进行下载构建组价
  2. SDO_GEOMETRY结构说明
  3. CentOS x64 安装gcc
  4. Java中 break continue return 的用法以及区别
  5. c语言程序设计教程课后选择题答案,C语言程序设计教程课后习题包括答案.docx...
  6. 编码原则 之 Once and Only Once
  7. c++中的c_str()函数
  8. 邵国际: C 语言对象化设计实例 —— 命令解析器
  9. 双足机器人的稳定性判据_仿人双足机器人步态规划——零力矩点(ZMP)
  10. 动作捕捉软件系统有那么重要吗?
  11. 计算机是如何储存信息的,计算机是如何储存信息的
  12. 百度飞浆paddlepaddle之中文文本分类(三)
  13. steam邮箱登录教程
  14. 媒体AI配图的时代来临!巴比特今日起全面拥抱AIGC——头条图片,AI创作!
  15. css实现一个温度计图表
  16. android各版本api区别,Android各个版本API的区别
  17. C语言磁盘文件由,C语言对磁盘文件进行快速排序简单实例
  18. Revit命令名称与命令ID
  19. PPPoE拨号过程解析
  20. 春招进来的新人23岁Java开发上来秀了波操作,真是扮猪吃老虎

热门文章

  1. Reading Paper
  2. CloudCompare 的简单的使用说明
  3. 国产芯片WiFi物联网智能插座—电耗采集功能设计
  4. C语言调用easyX图形库画圆盘时钟
  5. qt能使用logback_使用ELK系统分析SpringBoot日志
  6. 在Ubuntu 14.04 64bit上安装字体管理器font-manager
  7. Ubuntu 14.04 64bit安装IPython
  8. Windows下Qt程序打包
  9. CarTool 使用,获取图片资源
  10. DRF序列化和反序列化