include-guards

Variable definition in header files

喜夏-厌秋 提交于 2019-11-27 08:02:25
My very basic knowledge of C and compilation process has gone rusty lately. I was trying to figure out answer to the following question but I could not connect compilation, link and pre-processing phase basics. A quick search on the Google did not help much either. So, I decided to come to the ultimate source of knowledge :) I know: Variables should not be defined in the .h files. Its ok to declare them there. Why: Because a header file might get included from multiple places, thus redefining the variable more than one time (Linker gives the error). Possible work-around: Use header-guards in

Naming Include Guards

自作多情 提交于 2019-11-27 05:14:51
How are C++ include guards typically named? I tend to see this a lot: #ifndef FOO_H #define FOO_H // ... #endif However, I don't think that's very intuitive. Without seeing the file name it's difficult to tell what FOO_H is there for and what its name refers to. What's considered best practice? From my own experience, the convention is to name the inclusion guards after the header file containing them with the exception that the name is all in caps and the period is replaced with an underscore. So test.h becomes TEST_H . Real life examples of this include Qt Creator, which follows this

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

久未见 提交于 2019-11-26 22:57:27
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 from the editor, since slashes and backslashes in the macro are not the best thing. When I have to rename a

Creating your own header file in C

笑着哭i 提交于 2019-11-26 21:08:35
Can anyone explain how to create a header file in C with a simple example from beginning to end. Oliver Charlesworth 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 = foo(3); /* Use the function here */ printf("%d\n", y); return 0; } To compile using

in C++ , what's so special about “_MOVE_H”?

眉间皱痕 提交于 2019-11-26 19:08:52
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 thinking that I did something wrong, but when I try to use another key (like _MOVE_Ha , everything is back

Purpose of Header guards

拥有回忆 提交于 2019-11-26 19:04:41
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. 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 the following declarations will included in the source code seen by the compiler. On subsequent #include 's

Adding an include guard breaks the build

久未见 提交于 2019-11-26 16:38:23
问题 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

Header guards in C++ and C

孤街醉人 提交于 2019-11-26 16:25:01
At LearnCpp.com | 1.10 — A first look at the preprocessor . Under Header guards , there are those code snippets: add.h: #include "mymath.h" int add(int x, int y); subtract.h: #include "mymath.h" int subtract(int x, int y); main.cpp: #include "add.h" #include "subtract.h" In implementing the header guard , it is mentioned as follows: #ifndef ADD_H #define ADD_H // your declarations here #endif What could the declaration be here? And, should int main() come after #endif ? Is adding _H a convention or a must do thing? Thanks. The Communist Duck The FILENAME_H is a convention. If you really wanted

Variable definition in header files

旧时模样 提交于 2019-11-26 14:00:07
问题 My very basic knowledge of C and compilation process has gone rusty lately. I was trying to figure out answer to the following question but I could not connect compilation, link and pre-processing phase basics. A quick search on the Google did not help much either. So, I decided to come to the ultimate source of knowledge :) I know: Variables should not be defined in the .h files. Its ok to declare them there. Why: Because a header file might get included from multiple places, thus redefining

Naming Include Guards

别来无恙 提交于 2019-11-26 11:28:48
问题 How are C++ include guards typically named? I tend to see this a lot: #ifndef FOO_H #define FOO_H // ... #endif However, I don\'t think that\'s very intuitive. Without seeing the file name it\'s difficult to tell what FOO_H is there for and what its name refers to. What\'s considered best practice? 回答1: From my own experience, the convention is to name the inclusion guards after the header file containing them with the exception that the name is all in caps and the period is replaced with an