• <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>
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            • 隨筆 - 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 小蔥蘸醬 閱讀(1005) 評論(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,就不會聯接錯誤,跟引用有什么關系呢?
              回復  更多評論    
            18禁黄久久久AAA片| 亚洲精品tv久久久久| 久久久久久综合一区中文字幕 | 久久久老熟女一区二区三区| 亚洲va久久久噜噜噜久久男同| 久久国产乱子伦精品免费强| 亚洲国产精品综合久久一线| 久久青青草原精品影院| 亚洲国产另类久久久精品| 久久久久国产精品三级网| 久久偷看各类wc女厕嘘嘘| 手机看片久久高清国产日韩| 狠狠干狠狠久久| 久久99亚洲网美利坚合众国| 日本高清无卡码一区二区久久| 91精品国产色综合久久| 久久精品国产亚洲AV久| 一本一本久久a久久精品综合麻豆| 久久久久久综合一区中文字幕| 久久精品国产第一区二区三区| 怡红院日本一道日本久久 | 久久这里的只有是精品23| 99久久精品国产毛片| 久久99国产亚洲高清观看首页| 漂亮人妻被黑人久久精品| 综合久久国产九一剧情麻豆| 波多野结衣久久精品| 久久久精品久久久久影院| 7777精品伊人久久久大香线蕉| 国内精品久久久久久麻豆| 久久不见久久见免费影院www日本| 91精品国产91久久久久久青草| 一本色道久久88加勒比—综合| 老司机国内精品久久久久| 亚洲国产精品久久久久久| 国产精品无码久久四虎| 三级韩国一区久久二区综合| 久久久久青草线蕉综合超碰| 日本人妻丰满熟妇久久久久久| 久久狠狠高潮亚洲精品| 夜夜亚洲天天久久|