include-guards

C++ Include Guards for Standard Headers

≡放荡痞女 提交于 2019-12-19 18:29:12
问题 I am wondering if/ what include guards on files like windows.h , math.h , iostream , stdio ... etc. Since I have those headers included multiple times in different files. Do those files already have guards built in or is there a definition defined? I am just wondering what the standards are for that kind of thing. 回答1: If you open the file to read the contents (you can even right click the include directive in most editors to open the file), you will see that include files usually start with

C++ cyclic inclusion issue [duplicate]

走远了吗. 提交于 2019-12-19 09:52:48
问题 This question already has answers here : Resolve build errors due to circular dependency amongst classes (11 answers) Closed 2 years ago . I have this file logger.hpp: #ifndef _LOGGER_HPP_ #define _LOGGER_HPP_ #include "event.hpp" // Class definitions class Logger { public: /*! * Constructor */ Logger(); /*! * Destructor */ ~Logger(); /*! * My operator */ Logger& operator<<(const Event& e); private: ... }; #endif And this file event.hpp #ifndef _EVENT_HPP_ #define _EVENT_HPP_ #include <string

C header file loops

百般思念 提交于 2019-12-19 05:24:34
问题 I have a couple of header files, which boil down to: tree.h: #include "element.h" typedef struct tree_ { struct *tree_ first_child; struct *tree_ next_sibling; int tag; element *obj; .... } tree; and element.h: #include "tree.h" typedef struct element_ { tree *tree_parent; char *name; ... } element; The problem is that they both reference each other, so tree needs element to be included, and element needs tree to be included. This doesn't work because to define the 'tree' structure, the

Are redundant include guards necessary?

江枫思渺然 提交于 2019-12-18 06:56:25
问题 Are 'redundant include guards' necessary in Codegear RAD Studio 2009? Is the compiler smart enough to deal with this on it's own? For example, I might have the following 'include guard' in foo.h: #ifndef fooH #define fooH // ... declaration here #endif and the following 'redundant include guard' in use_foo.h: #ifndef fooH #include "foo.h" #endif Additionally, if the compiler is not smart enough, are 'redundant include guards' necesarry if they are being included in a source file. e.g. use_foo

Is there any mechanism in Shell script alike “include guard” in C++?

余生颓废 提交于 2019-12-14 00:16:25
问题 let's see an example: in my main.sh, I'd like to source a.sh and b.sh. a.sh, however, might have already sourced b.sh. Thus it will cause the codes in b.sh executed twice. Is there any mechanism alike "include guard" in C++? 回答1: If you're sourcing scripts, you are usually using them to define functions and/or variables. That means you can test whether the script has been sourced before by testing for (one of) the functions or variables it defines. For example (in b.sh ): if [ -z "$B_SH

How to reduce compile time for large C++ library of individual .cpp files?

筅森魡賤 提交于 2019-12-13 07:57:37
问题 We're developing a C++ library with currently over 500 hundred individual .cpp files. These are each compiled and archived into a static library. Even with a parallel build, this takes some minutes. I'd like to reduce this compilation time. Each file is on average 110 lines with a function or two inside. However, for each .cpp file there is a corresponding .h header and these are often included by many of the .cpp files. For example, A.h might be included by A.cpp , B.cpp , C.cpp , and so on.

Why can std::max and std::min still be used even if I didn't #include <algorithm>?

耗尽温柔 提交于 2019-12-12 08:19:51
问题 #include <iostream> int main() { int value1 = 1, value2 = 10; std::cout << "Min = " << std::min(value1,value2) <<std::endl; std::cout << "Max = " << std::max(value1,value2)<< std::endl; } As far as I know, the min and max functions are defined in <algorithm> . If I didn't tell the pre-processor to include <algorithm> why does the code still work? 回答1: Most likely, something inside of iostream has directly or indirectly included some other header that defines std::min and std::max . (Perhaps

code guards fail

北慕城南 提交于 2019-12-11 08:59:19
问题 Take this files: a.h #ifndef A_H #define A_H char EL[] = "el"; #endif a.cpp #include "a.h" b.h #ifndef B_H #define B_H #include "a.h" #endif b.cpp #include "b.h" main.cpp #include "b.h" #include "a.h" int main() { } This is only an example, but I've really this problem: g++ -c a.cpp g++ -c b.cpp g++ -c main.cpp g++ -o main main.o a.o b.o a.o:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first defined here b.o:(.data+0x0): multiple definition of `EL' main.o:(.data+0x0): first

C++: Understanding Header Files & Header Guards with Easy Addition Example

久未见 提交于 2019-12-11 07:23:46
问题 I can't get my head around headers and header guards. I've read other questions and their answers but I still can't make this work in Visual Studio 2013: main.cpp #include "stdafx.h" #include <iostream> #include "add.h" int _tmain(int argc, _TCHAR* argv[]) { std::cout << "3 + 4 = " << add(3, 4) << std::endl; system("pause"); return 0; } add.cpp #include "stdafx.h" //ADDED LATER; NOW WORKING (AND SEE QUESTION 2 BELOW) #include "add.h" //ADDED LATER; NOR WORKING (AND SEE QUESTION 2 BELOW) int

Include guard style, C++

丶灬走出姿态 提交于 2019-12-11 05:34:23
问题 I have a .h file which contains several class definitions. I'd like to use C++'s include guards in this file; however, I was wondering which way of using the include guards is considered proper/correct? One guard protecting everything #ifndef FOO_BAR #define FOO_BAR class Foo { }; class Bar { }; #endif or multiple separate guards. #ifndef FOO #define FOO class Foo { }; #endif #ifndef BAR #define BAR class Bar { }; #endif 回答1: They are include guards, preventing double inclusion of files . So