include-guards

C++ include guard

纵饮孤独 提交于 2019-12-01 04:14:38
问题 So I know how to place an include guard in my own header files with the standard #ifndef ... #define ... Now, My question is about including libraries that are not my own. would be a good example. I have a header file which requires the use of string, so I do the following foo.h #ifndef FOO_H #define FOO_H #include <string> ... code etc ... #endif Now, if I have another header file called.. lets say, bar.h , which ALSO requires the use of <string> , how can i prevent multiple inclusions? Does

Is there any situation where you wouldn't want include guards?

醉酒当歌 提交于 2019-12-01 04:12:04
I know why include guards exist, and that #pragma once is not standard and thus not supported by all compilers etc. My question is of a different kind: Is there any sensible reason to ever not have them? I've yet to come across a situation where theoretically, there would be any benefit of not providing include guards in a file that is meant to be included somewhere else. Does anyone have an example where there is an actual benefit of not having them? The reason I ask - to me they seem pretty redundant, as you always use them, and that the behaviour of #pragma once could as well just be

Is there any situation where you wouldn't want include guards?

血红的双手。 提交于 2019-12-01 01:16:27
问题 I know why include guards exist, and that #pragma once is not standard and thus not supported by all compilers etc. My question is of a different kind: Is there any sensible reason to ever not have them? I've yet to come across a situation where theoretically, there would be any benefit of not providing include guards in a file that is meant to be included somewhere else. Does anyone have an example where there is an actual benefit of not having them? The reason I ask - to me they seem pretty

Header/Include guards don't work?

别等时光非礼了梦想. 提交于 2019-11-30 17:11:17
问题 For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below: main.c: #include "thing.h" int main(){ printf("%d", increment()); return 0; } thing.c: #include "thing.h" int increment(){ return something++; } thing.h: #ifndef THING_H_ #define THING_H_ #include <stdio.h> int something = 0; int increment(); #endif When I attempt to compile this, GCC says that I have multiple definitions of the something variable.

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

假如想象 提交于 2019-11-29 13:10:18
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 same, so these could be alternatives as prefix for the include guard. So in the Preferences dialog

Customizing include-guard for Eclipse CDT

大憨熊 提交于 2019-11-28 23:23:46
I want an automatically generated include-guard by creating a new C++-class with Eclipse/CDT, but I don't find any way to change the ${include_guard_symbol} attribute. My wish is an include-guard with a namespace prefix like following: #ifndef NAMSPACE1_NAMESPACE2_HEADER_HPP But if I use #ifndef ${namespace_name}_${include_guard_symbol} for this, it will produce: namepace1::namespace2::_HEADER_HPP How can I do this? Dave E I had a dig around in the source for CDT, and found an undocumented preference setting you can use to change what is generated by ${include_guard_symbol} . There's no GUI

Why does global variables in a header file cause link error?

♀尐吖头ヾ 提交于 2019-11-28 09:45:42
问题 I always get the following error, even if I have put include guard in header file. duplicate symbol _Bittorrent in: /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/main.o /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/Handshake.o duplicate symbol _eight_byte in: /Users/tracking/Library

Include guard conventions in C

ぃ、小莉子 提交于 2019-11-28 00:31:00
问题 What is the conventional way to set up your include guards? I usually write them as (for example.h): #ifndef _EXAMPLE_H_ #define _EXAMPLE_H_ #include "example.h" #endif Does underscore convention matter? I've seen conflicting information when I googled this. Does the _EXAMPLE_H_ even have to match the name of the header? 回答1: Does underscore convention matter? Yes. It matters. Identifiers with a leading underscore followed upper case letter is reserved for implementation. So what you have

Customizing include-guard for Eclipse CDT

独自空忆成欢 提交于 2019-11-27 14:44:26
问题 I want an automatically generated include-guard by creating a new C++-class with Eclipse/CDT, but I don't find any way to change the ${include_guard_symbol} attribute. My wish is an include-guard with a namespace prefix like following: #ifndef NAMSPACE1_NAMESPACE2_HEADER_HPP But if I use #ifndef ${namespace_name}_${include_guard_symbol} for this, it will produce: namepace1::namespace2::_HEADER_HPP How can I do this? 回答1: I had a dig around in the source for CDT, and found an undocumented

Adding an include guard breaks the build

a 夏天 提交于 2019-11-27 14:14:53
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 the problem? Sounds like the file is already declared, but I do not know where. I'm fine just removing it, but I really want to know why this is happening. error: expected class-name before ‘{’ token error: ‘QDesignerFormEditorInterface’ has not been declared And a couple of other errors. I am actually using an example from Qt, "Custom Widget Plugin Example". The difference is I am using my own class for the custom widget (