Skip to main content
GameDev.net gamedev.net
🔒 Locked

preprocessor include

Started by boomji Feb 2, 2003 at 11:36 AM 4 replies 300+ views
Original Post
boomji
boomji
hi, this may sound a little daft but i''m still barfed about the preprocessor thing i''ll state what i understood and if and will appreciate any corrections. 1) i first declare my function prototyprs in a header...which i named for e.g header_declaration.h. 2) i define my function code in a cpp file for e.g header_definition.cpp. 3)now if i DONT want the compiler to include my code twice what do i have to do ???. #define help. /*...*/ # ifdef help /*...*/ cout<<"help me"<
remember what you see to see what you rememberb
Shannon Barber
Shannon Barber
The underscores are used to prevent name collisions with existing code. There are rules about what you are allowed to put after an underscore though (__ is reserved for the implementation, and so is _ followed by a capitol letter). The recommended practice is to use ALL_CAPS for macros.

Try using #pragma once at the top of the header. It''s slightly better than include guards, if the compiler supports it.

//header.h
#ifndef HEADER_H
#define HEADER_H
//header code
#endif

//source.cpp
#include "header.h"
//source code
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
boomji
boomji



thanx holmlor.
#pragma directive may or may not be supported.Since i''m learning i wouldn''t want to take chances.

//header.h
#ifndef HEADER_H
#define HEADER_H
//header code
#endif
...thats cool, thanx dude.

It would make more sense if i could just write the name of the header file itself(i.e header.h in this case).So in plain english it would mean "if header.h is already included then dont include it twice"...sorry if i seam to be totally screwd up with my basics...i guess i''ll just go along and learn with making some mistakes eh .

thanx
b
remember what you see to see what you rememberb
LessBread
LessBread
quote:
Original post by boomji
So in plain english it would mean "if header.h is already included then dont include it twice"...


Yes - but plain english isn''t always the most accurate.

//header.h
#ifndef HEADER_H
#define HEADER_H
//header code
#endif

What this says is, if the symbol HEADER_H is not defined, define it and include what ever else comes before the closing endif. If the header is included again and HEADER_H is defined, the entire section between the #ifndef and #endif is left out of the inclusion.

I find it helpful to think of the preprocessor as a text substitution mechanism.

I''ve also found it helpful to use command line switches to direct the compiler to output preprocessed source code rather than obj file. This allows for examining the output produced by the preprocessor, how particular macros expanded into code and so on.

The switches to use with MSVC - "cl.exe" - are /F, /FP, and /P. Open a console to the MSVC bin directory and type "cl /?" for details regarding these switches.

If you''re not using MSVC, consult your compiler docs. Chances are quite likely that similar switches are used.



"It is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship. Voice or no voice, the people can always be brought to the bidding of their leaders. That is easy. All you have to tell them is that they are being attacked and denounce the peacemakers for lack of patriotism and exposing the country to danger. It works the same in any country."
Hermann Goering
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
boomji
boomji



hey wowwww !!!
what a load of info...thanks.Will digest slooowly .
thank you so much for going a lill beyound what i asked...this sort of thing really helps.Ok i''ll stop gushing.
b
remember what you see to see what you rememberb
Skizz
Skizz
As a good habit to get into, when including a header file, do:

#ifndef the_guard_symbol
#include "the_header_file"
#endif

as well as putting the guard block check inside the header file (so it would be a good idea to have a standard way of defining the guard symbol from the filename). If all that typing is going to put you off the idea then create a macro in your ''insert favourite editor here'' that does it for you.

When you start working on large projects with lots of header files, the preprocessor can slow you down if you''re including lots of headers multiple times (especially if your pulling files of a shared network resource) - not opening a file is always quicker than opening it, reading the file and then discarding the contents (the preprocessor has to search for the closing #endif).

Skizz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.