• <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无码久久久久不卡免费网站| 日本强好片久久久久久AAA| 丁香狠狠色婷婷久久综合| 狠狠人妻久久久久久综合蜜桃| 欧美亚洲日本久久精品| 久久亚洲精品国产精品| 久久久久这里只有精品| 一本色道久久HEZYO无码| 国产精品成人精品久久久| 色综合久久久久久久久五月| 97久久精品人人澡人人爽| 三级三级久久三级久久| 久久精品中文字幕久久| 久久精品一本到99热免费| 国产香蕉久久精品综合网| 亚洲国产精品久久久久婷婷老年| 久久久精品一区二区三区| 久久精品亚洲日本波多野结衣| 潮喷大喷水系列无码久久精品| 狠狠色丁香婷婷久久综合不卡| 久久99精品久久久久久动态图| 成人精品一区二区久久| 国产精品99久久久久久董美香| 日韩美女18网站久久精品| 久久久婷婷五月亚洲97号色| 少妇人妻综合久久中文字幕| 久久婷婷久久一区二区三区| 久久99国内精品自在现线| 一本色道久久HEZYO无码| 亚洲国产成人久久综合一| 亚洲va国产va天堂va久久| 久久国产精品免费一区| 久久青草国产手机看片福利盒子| 久久无码人妻一区二区三区午夜| 无码精品久久一区二区三区| 久久毛片免费看一区二区三区| 99久久综合国产精品二区| 久久这里只有精品久久| 久久综合久久综合久久|