include-guards

Names used for include guards?

孤者浪人 提交于 2020-05-23 21:10:12
问题 Are there any guidelines people tend to follow when selecting a name for their include guard? I don't understand why the name for the .h file will slightly differ from the name used in the include guard. For example, I have seen sphere.h and then #ifndef SPHERE_H_ . Could I just as easily have used SPHERE_ or do the names have to match? Are the underscores also necessary? 回答1: To pick a good name for a header guard, consider these points: 1) the name has to be unique, so make it long and

code guards fail and template from string literal

独自空忆成欢 提交于 2020-01-23 18:59:25
问题 I know the only way to pass a string literal as template argument is to declare it before: file a.h #ifndef A_H #define A_H #include <string> char EL[] = "el"; template<char* name> struct myclass { std::string get_name() { return name; } }; typedef myclass<EL> myclass_el; #endif file a.cpp #include "a.cpp" main.cpp #include "a.h" ... g++ -c a.cpp g++ -c main.cpp g++ -o main main.o a.o and I got: a.o:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first defined here collect2: ld

Eclipse-CDT: Use Namespace in automatic generated include-guards

末鹿安然 提交于 2019-12-29 08:27:32
问题 Is it possible (and how) to add the namespace in the name of the automatic generated include guards in Eclipse CDT, when creating a new class using the .hpp/.cpp templates? For me Eclipse generates a new class with a namespace nicely, but the include guards do not contain the namespace, so if the same header file exists twice in two different directories, only one can be included. In my case the name of the namespace, the Eclipse project name and the name of the source directory are all the

C include guard [duplicate]

非 Y 不嫁゛ 提交于 2019-12-23 19:07:50
问题 This question already has answers here : Header file included only once in entire program? (4 answers) Closed last year . When file1.c includes inc.h (containing the include guard #ifndef INC_H ) for the first time, the #define INC_H is performed. But now, when another file2.c includes the same inc.h , is the macro INC_H already defined, all it's the same story and previous definition is not propagated here? 回答1: But now, when another file2.c includes the same inc.h, is the macro INC_H

Does “#pragma once” have the potential to cause errors?

时光怂恿深爱的人放手 提交于 2019-12-23 09:49:15
问题 All of my header files use include guards as well as pragma once : #pragma once #ifndef FILE_NAME_H #define FILE_NAME_H class foo { //foo interface.. }; #endif /* FILE_NAME_H */ I understand that pragma once is not standard and may not be the same across compilers, but is there any chance it will cause and error? Would it be better to somehow test if it's available first? #ifdef THIS_COMPILER_SUPPORTS_PRAGMA_ONCE #pragma once #endif #ifndef FILE_NAME_H #define FILE_NAME_H class foo { //foo

Are tokens after #endif legal?

て烟熏妆下的殇ゞ 提交于 2019-12-23 07:46:10
问题 I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while. Is this bad practice that I should go back through all of my headers and fix (it's a cross

Are tokens after #endif legal?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:44:04
问题 I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while. Is this bad practice that I should go back through all of my headers and fix (it's a cross

When to use include guards or #pragma once C++ [closed]

删除回忆录丶 提交于 2019-12-22 05:09:18
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Is it good practice to use your choice of either/both include guards and #pragma once in every header file, or just those with something such as a class declaration? I am tempted to put it in every header, but I'm afraid it would be unneeded and only add to the compile time.

Is an #include before #ifdef/#define Include-Guard okay?

痞子三分冷 提交于 2019-12-22 03:59:13
问题 I always placed my #include after the #ifdef / #define Include-Guard. Now the refactor mechanism of my IDE (Qt Creator) put it before the Include-Guard e.g. #include "AnotherHeader.h" #ifndef MYHEADER_H #define MYHEADER_H Can this cause any problems or can I leave it this way? 回答1: If the header in question has include guards itself, you won't run into problems. Putting it inside the include guards may still speed up compilation. Something the compiler does not see takes less time to compile,

Should I still use #include guards AND #pragma once?

会有一股神秘感。 提交于 2019-12-21 06:46:20
问题 http://en.wikipedia.org/wiki/Pragma_once Should I still use include guards when all of these compilers support #pragma once ? A lot of responses on stack overflow say to use both for compatibility, but I'm not sure if that still rings true. What compilers today don't support #pragma once ? I am not sure if using both was just a recommendation before it became widley adopted, or if there are still very good reasons to use both methods. Any examples of when only using #pragma once will cause