Should variable definition be in header files?

One thing that I’ve used in the past (when global variables were in vogue): var.h file: … #ifdef DEFINE_GLOBALS #define EXTERN #else #define EXTERN extern #endif EXTERN int global1; EXTERN int global2; … Then in one .c file (usually the one containing main()): #define DEFINE_GLOBALS #include “var.h” The rest of the source files just include … Read more

Naming Include Guards

I personally follow Boost’s recommendation. It’s perhaps one of the largest collection of C++ libraries of good quality around and they don’t have problem. It goes like: <project>_<path_part1>_…_<path_partN>_<file>_<extension>_INCLUDED // include/pet/project/file.hpp #ifndef PET_PROJECT_FILE_HPP_INCLUDED which is: legal (note that beginning by _[A-Z] or containing __ is not) easy to generate guaranteed to be unique (as a include … Read more

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

just run grep _MOVE_H in /usr/include/c++ on your machine for me : c++/4.5.0/bits/move.h:#ifndef _MOVE_H As a rule of thumb, don’t use things (really anything) prefixed by _ or __. It’s reserved for internal usage. Use SOMETHING_MOVE_H (usually name of the company, …). I guess it’s a new header used to add the move semantic to … Read more

Purpose of Header guards

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 … Read more

Header guards in C++ and C

The FILENAME_H is a convention. If you really wanted, you could use #ifndef FLUFFY_KITTENS as a header guard (provided it was not defined anywhere else), but that would be a tricky bug if you defined it somewhere else, say as the number of kittens for something or other. In the header file add.h the declarations … Read more

C++ #include guards

The preprocessor is a program that takes your program, makes some changes (for example include files (#include), macro expansion (#define), and basically everything that starts with #) and gives the “clean” result to the compiler. The preprocessor works like this when it sees #include: When you write: #include “some_file” The contents of some_file almost literally … Read more