Adding an include guard breaks the build

后端 未结 2 816
广开言路
广开言路 2020-12-03 09:40

I added #ifndef..#define..#endif to a file of my project and the compiler fails. As soon as I remove it or put any other name in the define it compiles fine. What could be

2条回答
  •  被撕碎了的回忆
    2020-12-03 10:05

    Is this macro used as an include guard? If so, it sounds like you're duplicating a name used elsewhere. This is a common problem when people don't think about the scope an include guard must have—you should include much more information in it than just the file name.

    Include guard goals:

    • generate once, when creating a header
    • never have to think about again
    • chance of duplicating is less than your chance of winning the lottery

    Bad include guard names (for file "config.h"):

    • CONFIG_H
      • much too general
    • _CONFIG_H, CONFIG__H, CONFIG_H__, __CONFIG_H__, etc.
      • all reserved, still much too general
    • PROJECT_CONFIG_H
      • better, much less likely to duplicate in unrelated projects
      • but still no path information, easy to duplicate in large projects

    Good include guard names (for file "config.h"):

    • PATE_20091116_142045
      • that's __
      • no project, path, filename information even needed
      • easy to type
        • if your editor has an insert-date feature, you can "type" it very fast
      • easy to generate
        • include a sequence number when generating, if you need to generate more than one per second
      • strong guarantee of being universally unique
    • INCLUDE_GUARD_726F6B522BAA40A0B7F73C380AD37E6B
      • generated from an actual UUID
        • strong guarantee of being universally unique
      • if it turns up unexpectedly, "INCLUDE_GUARD" is a good hint about what it is, while serving to put it in a separate namespace (though by convention rather than recognized by the language)
      • prepend a project name, if desired (which is often required by project guidelines for macros)
      • easy to write your own sample program to generate

提交回复
热议问题