• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            <2009年2月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            1234567

            統計

            • 隨筆 - 24
            • 文章 - 0
            • 評論 - 17
            • 引用 - 0

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            相冊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            有關include guard的一個好帖子(收藏)
            終于弄清楚了原來說的同一個頭文件不能被兩次或兩次以上包含是針對同一個源文件而言的。借用80后的流行語,真是漢哪!

            原貼地址:http://www.keil.com/forum/docs/thread10237.asp

            作者 Per Westermark

            The

            #ifndef xx
            #define xx
            ...
            #endif
            

            method is to make sure that a header file isn't included more than once from the same c file.

            You can not - and normally don't want to - stop multiple c files from including the same header file.

            A header file is included because:
            1) You have specifically added a line #include "xx" or #include <xx> in the source file. Don't do that unless you want the file to be included :)
            2) You are including one header file, that it it's turn (one or more steps away) includes another header file. But a header file should only contain a recursive #include if it really needs that other file for some declarations. Hence, you need to include it.

            What does this mean?

            If the header file must be seen by multiple source files, you can't use it to allocate global variables, since the linker would then complain about multiple sets of global variables with the same name. This can be solved with the following:

            //globals.h
            #ifndef _GLOBALS_H
            #define _GLOBALS_H
            #if defined MAIN
            #define EXTERN
            #else
            #define EXTERN extern
            #endif
            ...
            EXTERN int my_global_variable;
            #endif // _GLOBALS_H
            

            // main.c
            #define MAIN
            #include "globals.h"
            ...
            // misc.c
            #include "globals.h"
            ...
            

            In this case, only the inclusion in main.c will result in an "allocation" of global variables, because the #define EXTERN will be empty. All other source files that includes "globals.h" will just see the type information for the global variables.

            posted on 2009-02-19 09:04 小蔥蘸醬 閱讀(994) 評論(2)  編輯 收藏 引用

            評論

            # re: 有關include guard的一個好帖子(收藏) 2009-02-19 21:19 陳梓瀚(vczh)

            EXTERN的時候,如果剛好main沒有引用它,那就鏈接錯誤了- -#
              回復  更多評論    

            # re: 有關include guard的一個好帖子(收藏)[未登錄] 2009-02-20 16:15 小蔥蘸醬

            @陳梓瀚(vczh)
            歡迎評論!
            只要在include "globals.h"前定義了MAIN,就不會聯接錯誤,跟引用有什么關系呢?
              回復  更多評論    
            激情伊人五月天久久综合| 久久人人爽人人爽人人AV东京热| 久久精品a亚洲国产v高清不卡| 亚洲精品乱码久久久久久久久久久久| 欧美亚洲色综久久精品国产| 久久最近最新中文字幕大全| 久久久久人妻一区精品| 国产偷久久久精品专区| 久久国产精品成人片免费| 国产99久久久国产精免费| 国产69精品久久久久9999APGF| 欧美亚洲另类久久综合| 亚洲?V乱码久久精品蜜桃| 精品久久香蕉国产线看观看亚洲 | 久久er国产精品免费观看2| 久久久久久一区国产精品| 国产婷婷成人久久Av免费高清| 久久精品夜色噜噜亚洲A∨| 久久精品无码专区免费东京热| 久久精品国产只有精品66| 久久国产高潮流白浆免费观看| 久久婷婷人人澡人人| 久久97精品久久久久久久不卡| 一本大道久久香蕉成人网| 国产福利电影一区二区三区久久老子无码午夜伦不 | 亚洲午夜无码久久久久| 久久久WWW成人| 久久精品国产只有精品2020| 精品熟女少妇AV免费久久| 蜜臀久久99精品久久久久久| 久久香蕉国产线看观看99| 看久久久久久a级毛片| 久久精品中文字幕一区 | 久久人与动人物a级毛片| 欧美激情精品久久久久久久| 2021国产成人精品久久| 久久精品视频免费| 久久国产高清字幕中文| 韩国三级大全久久网站| 国产一级做a爰片久久毛片| 精品人妻久久久久久888|