日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言 自动包含头文件,C语言不要重复包含.h头文件和.c文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://blog.csdn.net/unix21/article/details/8450235

2012

1.不要重復(fù)包含頭文件

--以上出自《C語(yǔ)言程序設(shè)計(jì):現(xiàn)代方法(第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?

#include?"f1.h"

#include?"f2.h"

int?main(){

ngx_int_t?a1=1;

printf("%d",AE_OK);

printf("%d",a1);

return?0;

}

編譯不過(guò)去:

導(dǎo)出預(yù)編譯文件:

...以上省略

#?2?"test.c"?2

#?1?"f1.h"?1

#?1?"f3.h"?1

typedef?int?ngx_int_t;

#?1?"f1.h"?2

#?4?"test.c"?2

#?1?"f2.h"?1

#?1?"f3.h"?1

typedef?int?ngx_int_t;

#?1?"f2.h"?2

#?5?"test.c"?2

int?main(){

ngx_int_t?a1=1;

printf("%d",0);

printf("%d",a1);

return?0;

}

如果我們?cè)趂3.h中增加#ifndef就不會(huì)出問(wèn)題了,直接導(dǎo)出預(yù)編譯文件:

#?2?"test.c"?2

#?1?"f1.h"?1

#?1?"f3.h"?1

typedef?int?ngx_int_t;

#?1?"f1.h"?2

#?4?"test.c"?2

#?1?"f2.h"?1

#?5?"test.c"?2

int?main(){

ngx_int_t?a1=1;

printf("%d",0);

printf("%d",a1);

return?0;

}

2..c文件編譯注意不要重復(fù)引入

這個(gè)是我從redis源碼中抽取其事件庫(kù)的編譯

在redis源碼的ae.c文件:

#include?

#include?

#include?

#include?

#include?

#include?

#include?

#include?

#include?

#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

#endif

aeEventLoop?*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?

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函數(shù)

#include?

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的事件庫(kù)

一開(kāi)始不知道include的.c文件編譯的時(shí)候不需要重復(fù)引入,不然編譯報(bào)錯(cuò):

ae_epoll.c不用被引入,因?yàn)樵赼e.c已經(jīng)引入了。

成功編譯:

生成了redis文件。

2..h文件編譯注意不要重復(fù)引入

報(bào)錯(cuò)anet.h:47: 錯(cuò)誤:expected declaration specifiers or ‘...’ before ‘size_t’

原因是anet.h頭文件重復(fù)引用,去掉anet.h,重新編譯就可以了:

原因分析:

b.h中#include "../../a.h" 而a.h中的函數(shù)聲明中用到了b.h中的結(jié)構(gòu)體或者typedef,那么就會(huì)出現(xiàn)在包含a.h的時(shí)候b.h中的結(jié)構(gòu)體或者typedef還沒(méi)有聲明,從而陷入錯(cuò)誤。

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的c语言 自动包含头文件,C语言不要重复包含.h头文件和.c文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。