Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of a C++ symbol

Tags:

c++

scope

symbols

AFAIK, symbols are useful to prevent multiple parsing. If both a.h and b.h include c.h, a

#ifndef C_H
#define C_H
...
// c.h definition would go here
...
#endif

will prevent c.h from being "parsed" (I believe it's not the right word) more than once.

However, I have seen something like

#ifdef WIN32
...

in other people's code. That symbol must have been defined somewhere else because a search for

#define WIN32

in the whole project returns empty. My question is: where are these symbols actually defined? Does the OS keep something similar to a pool of symbols that different programs can use to query for OS or other processes properties?

like image 552
roymcclure Avatar asked Nov 05 '15 18:11

roymcclure


1 Answers

There are two options where those which are not in the code itself can originate from:

  1. The compiler suite itself sets it as a default when you start compiling your code.
  2. You give the compiler (or preprocessor, to be exact) a list of those definitions when you compile the code (or your IDE project preferences do, when you are using an IDE. For example, in Visual Studio 2013 you will find those when you open Project > Properties > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions).

In general, those definitions are not only used for the reason you describe (as include guards), but also to enable or disable code based on the platform you develop for - for example, you can have code branches only compiled for windows, or only if you are using a 64 bit compiler.

like image 62
Cybran Avatar answered Oct 20 '22 01:10

Cybran