• <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

            統(tǒng)計(jì)

            • 隨筆 - 24
            • 文章 - 0
            • 評(píng)論 - 17
            • 引用 - 0

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            相冊(cè)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            有關(guān)include guard的一個(gè)好帖子(收藏)
            終于弄清楚了原來說的同一個(gè)頭文件不能被兩次或兩次以上包含是針對(duì)同一個(gè)源文件而言的。借用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 小蔥蘸醬 閱讀(1004) 評(píng)論(2)  編輯 收藏 引用

            評(píng)論

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

            EXTERN的時(shí)候,如果剛好main沒有引用它,那就鏈接錯(cuò)誤了- -#

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

            @陳梓瀚(vczh)
            歡迎評(píng)論!
            只要在include "globals.h"前定義了MAIN,就不會(huì)聯(lián)接錯(cuò)誤,跟引用有什么關(guān)系呢?

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久成人永久免费播放| 亚洲精品国产自在久久| 青草影院天堂男人久久| 99久久www免费人成精品| 久久强奷乱码老熟女网站| 久久久久亚洲AV无码永不| 欧美国产成人久久精品| 久久国产欧美日韩精品| 亚洲国产成人精品女人久久久| 久久夜色精品国产网站| 欧美国产精品久久高清| 久久免费高清视频| 亚洲精品乱码久久久久久蜜桃图片| 91久久香蕉国产熟女线看| 97精品国产97久久久久久免费| 99热精品久久只有精品| 99久久99久久精品免费看蜜桃| 久久久久久精品久久久久| 久久久久亚洲AV成人网人人网站| www性久久久com| 久久青青草原精品国产| 久久婷婷色综合一区二区| 久久久久久国产精品免费免费| 久久久久综合网久久| 国内精品久久久久久99| 无码日韩人妻精品久久蜜桃| 久久99热这里只频精品6| 日韩欧美亚洲国产精品字幕久久久 | 色综合久久久久网| 99麻豆久久久国产精品免费| 性高湖久久久久久久久| 精品综合久久久久久97| 久久国产亚洲精品| 久久精品中文字幕一区| 热re99久久6国产精品免费| 久久久久久精品久久久久| 欧美一区二区久久精品| 久久久精品国产| 亚洲香蕉网久久综合影视 | 久久国产精品波多野结衣AV| 国产亚州精品女人久久久久久 |