include-guards

When to use include guards?

感情迁移 提交于 2019-12-10 11:08:41
问题 I know that the use of include guards in header files is to prevent something from being defined twice. Using this code sample though, was completely fine: foo.c #include <stdio.h> #include <string.h> #include "bar.h" int main() { printf("%d", strlen("Test String")); somefunc("Some test string..."); return 0; } bar.h #ifndef BAR_H_INCLUDED #define BAR_H_INCLUDED void somefunc(char str[]); #endif bar.c #include <stdio.h> #include <string.h> #include "bar.h" void somefunc(char str[]) { printf(

Difference between pragma once inside and outside include guards?

家住魔仙堡 提交于 2019-12-10 02:08:10
问题 Is there any difference between placing the #pragma once inside the include guards as opposed to outside? case 1: #ifndef SOME_HEADER_H #define SOME_HEADER_H #pragma once case 2: #pragma once #ifndef SOME_HEADER_H #define SOME_HEADER_H I'm just wondering out of curiosity if there's any special cases where I should prefer one or the other (case 1 or case 2) since I've decided to combine both (pragma and header guards) in my code. EDIT: I think you guys are misinterpreting my question... I am

When not to use include guard in header file?

馋奶兔 提交于 2019-12-10 01:23:59
问题 We all know when to use include guard, but when shall we not use it in our project? Recently, I saw a project with mix compilation (CUDA + GCC), one header file (CUDA file) is deliberately left without include guard. I am just curious about it. 回答1: There are 2 scenarios off the top of my head: when you want to turn on/off debugging capabilities (as how assert.h works) for 'x-macro' type of functionality where you have the include file perform 2 parts of problem, such as defining an enum then

Do (Cross-compile) platform files require an include guard?

你说的曾经没有我的故事 提交于 2019-12-09 03:26:12
问题 I'm writing a Cross-compiling Toolchain file for VxWorks. Since it's an unknown system to cmake a also have write platform files (those in ../Modules/Platform ). Beside my toolchain file I have written these platform files so far: VxWorks.cmake (VxWorks OS settings) VxWorks-gcc.cmake (WindRiver (Gnu) compiler settings) VxWorks-gcc- [CPU] .cmake (CPU specific settings, is the processor as specified in the toolchain file) Everything works fine with my files at the moment. But some of the

Difference Between includes and imports [duplicate]

感情迁移 提交于 2019-12-08 15:44:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What is the difference between #import and #include in Objective-C? What is the difference between #include< > #include" " #import< > #import" " 回答1: The #import directive is an improved version of #include . #import ensures that a file is only ever included once so that you never have a problem with recursive includes. #import "" first check the header in project folder then goes to system library, and the

Double include solution?

一世执手 提交于 2019-12-07 14:47:36
问题 In C++, I have a problem with a double include: File stuffcollection.h #pragma once #ifndef STUFFCOLLECTION_H #define STUFFCOLLECTION_H #include "Stage.h" class Stuffcollection { public: bool myfunc( Stage * stage ); }; #endif // STUFFCOLLECTION_H File stage.h: #pragma once #ifndef STAGE_H #define STAGE_H #include "Stuffcollection.h" class Stage { // stuffcollection used in stage.cpp }; #endif // STAGE_H Compiler Error: \Stuffcollection.h|(line were bool myfunc is declared)|error: 'Stage' has

“Multiple include guards may be useful for” what, exactly?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 05:11:48
问题 I've been playing around with the -H option of gcc, which prints out information about direct and indirect includes in C and C++ compilation (relevant section of the gcc manual). As part of the output there's a section "Multiple include guards may be useful for:", which lists a number of project and system headers. What does this mean, how is this list determined, and why is it saying "Multiple" include guards may be useful? (I'm familiar with the general concept of include guards, and why

Multiple inclusion in multiple files

血红的双手。 提交于 2019-12-06 16:15:25
问题 I am making a small game. In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTER_H_ #include "Character.h" #endif class BattleRecord { public: Character Attacker; Character Defender; Status status; int DamageDealt; int GoldEarned; int ExpGained; }; In Character.h: #ifndef _EQUIPMENT_H_ #define _EQUIPMENT_H_ #include "Equipment.h" #endif class BattleRecord; class Character { BattleRecord AttackEnemy(Character &Enemy); } In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTEr_H_

Double include solution?

与世无争的帅哥 提交于 2019-12-05 21:33:21
In C++, I have a problem with a double include: File stuffcollection.h #pragma once #ifndef STUFFCOLLECTION_H #define STUFFCOLLECTION_H #include "Stage.h" class Stuffcollection { public: bool myfunc( Stage * stage ); }; #endif // STUFFCOLLECTION_H File stage.h: #pragma once #ifndef STAGE_H #define STAGE_H #include "Stuffcollection.h" class Stage { // stuffcollection used in stage.cpp }; #endif // STAGE_H Compiler Error: \Stuffcollection.h|(line were bool myfunc is declared)|error: 'Stage' has not been declared| ||=== Build finished: 1 errors, 0 warnings ===| Can someone please explain why this

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

ⅰ亾dé卋堺 提交于 2019-12-05 02:38: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? 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, even if it does not produce any errors. Although less common, I believe this is considered to be acceptable