• <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,就不會聯接錯誤,跟引用有什么關系呢?
              回復  更多評論    
            日韩精品久久久久久| 久久福利片| 久久免费视频网站| 精品国产日韩久久亚洲| 国产精品一久久香蕉国产线看观看| 日本道色综合久久影院| 久久久久久精品免费看SSS| 日韩精品国产自在久久现线拍| 久久午夜免费视频| 久久久久九国产精品| 99久久精品费精品国产一区二区| 久久乐国产综合亚洲精品| 欧美久久精品一级c片片| 精品人妻久久久久久888| 久久笫一福利免费导航 | 久久精品国产亚洲一区二区| 久久精品国产亚洲AV香蕉| 久久久久这里只有精品| 久久精品成人免费看| 九九久久自然熟的香蕉图片| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 久久综合色老色| 亚洲欧洲精品成人久久曰影片 | 老色鬼久久亚洲AV综合| 伊人久久大香线蕉综合网站| 久久99精品久久久久久噜噜| 青青草国产精品久久久久| 国产精品久久久久久影院| 国产精品毛片久久久久久久| 久久久久久久97| 99久久99这里只有免费的精品| 久久综合香蕉国产蜜臀AV| 97久久久精品综合88久久| 久久美女人爽女人爽| 久久精品国产精品亚洲下载| 国产精品永久久久久久久久久 | 无码任你躁久久久久久久| 午夜福利91久久福利| 国产精品99久久久久久宅男| 国产午夜精品久久久久九九电影| 久久激情五月丁香伊人|