include-guards

What exactly do C include guards do?

旧街凉风 提交于 2019-11-26 10:02:21
问题 I have a question regarding include guards in C. I\'ve done a bit of reading but would appreciate a little bit of clarification. Let\'s say I have a header file \"header.h\" with a function definition. #ifndef HEADER_FILE #define HEADER_FILE int two(void){ return 2; } #endif This header file has an include guard. However, I\'m kind of confused as to what #define HEADER_FILE is actually doing. Let\'s say I were to forget the include guard, it would have been perfectly legal for me to

Why isn't C/C++'s “#pragma once” an ISO standard?

。_饼干妹妹 提交于 2019-11-26 08:28:50
问题 I am currently working on a big project and maintaining all those include guards makes me crazy! Writing it by hand is frustrating waste of time. Although many editors can generate include guards this doesn\'t help much: Editor generates guard symbol based on a filename. The problem occurs when you have headers with the same filename in different directories. Both of them will get the same include guard. Including directory structure into the guard symbol would require some fancy approach

Creating your own header file in C

久未见 提交于 2019-11-26 07:50:50
问题 Can anyone explain how to create a header file in C with a simple example from beginning to end. 回答1: foo.h #ifndef FOO_H_ /* Include guard */ #define FOO_H_ int foo(int x); /* An example function declaration */ #endif // FOO_H_ foo.c #include "foo.h" /* Include the header (not strictly necessary here) */ int foo(int x) /* Function definition */ { return x + 5; } main.c #include <stdio.h> #include "foo.h" /* Include the header here, to obtain the function declaration */ int main(void) { int y

in C++ , what&#39;s so special about “_MOVE_H”?

谁说胖子不能爱 提交于 2019-11-26 06:48:47
问题 I have a C++ file like this #ifndef _MOVE_H #define _MOVE_H class Move { int x, y; public: Move(int initX = 0, int initY = 0) : x(initX), y(initY) {} int getX() { return x; } void setX(int newX) { x = newX; } int getY() { return y; } void setY(int newY) { y = newY; } }; #endif And to my amazement, all the code between #ifndef and #endif is simply ignored by the compiler (I swear that I am not defining _MOVE_H anywhere else), and I have all kinds of errors about missing definitions. I was

Purpose of Header guards

一笑奈何 提交于 2019-11-26 06:47:39
问题 In C++ what is the purpose of header guard in C++ program. From net i found that is for preventing including files again and again but how do header guard guarantee this. 回答1: The guard header (or more conventionally "include guard") is to prevent problems if header file is included more than once; e.g. #ifndef MARKER #define MARKER // declarations #endif The first time this file is #include -ed, the MARKER preprocessor symbol will be undefined, so the preprocessor will define the symbol, and

Prevent direct access to a php include file

心不动则不痛 提交于 2019-11-26 01:23:49
问题 I have a php file which I will be using as exclusively as an include. Therefore I would like to throw an error instead of executing it when it\'s accessed directly by typing in the URL instead of being included. Basically I need to do a check as follows in the php file: if ( $REQUEST_URL == $URL_OF_CURRENT_PAGE ) die (\"Direct access not premitted\"); Is there an easy way to do this? 回答1: The easiest way for the generic "PHP app running on an Apache server that you may or may not fully

C++ #include guards

删除回忆录丶 提交于 2019-11-25 23:37:33
问题 SOLVED What really helped me was that I could #include headers in the .cpp file with out causing the redefined error. I\'m new to C++ but I have some programming experience in C# and Java so I could be missing something basic that\'s unique to C++. The problem is that I don\'t really know what\'s wrong, I will paste some code to try to explain the issue. I have three Classes, GameEvents, Physics and GameObject. I have headers for each of them. GameEvents has one Physics and a list of

Why aren&#39;t my include guards preventing recursive inclusion and multiple symbol definitions?

笑着哭i 提交于 2019-11-25 23:09:37
问题 Two common questions about include guards: FIRST QUESTION: Why aren\'t include guards protecting my header files from mutual, recursive inclusion ? I keep getting errors about non-existing symbols which are obviously there or even weirder syntax errors every time I write something like the following: \"a.h\" #ifndef A_H #define A_H #include \"b.h\" ... #endif // A_H \"b.h\" #ifndef B_H #define B_H #include \"a.h\" ... #endif // B_H \"main.cpp\" #include \"a.h\" int main() { ... } Why do I get

Is #pragma once a safe include guard?

久未见 提交于 2019-11-25 22:49:21
问题 I\'ve read that there is some compiler optimization when using #pragma once which can result in faster compilation. I recognize that is non-standard, and thus could pose a cross-platform compatibility issue. Is this something that is supported by most modern compilers on non-windows platforms (gcc)? I want to avoid platform compilation issues, but also want to avoid the extra work of fallback guards: #pragma once #ifndef HEADER_H #define HEADER_H ... #endif // HEADER_H Should I be concerned?