Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - Header guards, pragma once and build times...

Author
Message
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 15th Jan 2006 22:06
Hi,

I've read a few mixed things about speeding up build times through using 'pragma once' and guards on headers, plus the use of precompiled headers etc. and I wondered if there was any guideline implementation (for MSVC in particular) to ensure a fast build time.

If anyone has had any experience with this I'd be interested to hear their recommendations and thoughts.

Also I tend to have a seperate header file for each class, and I do any needed sub includes in there too, but I've noticed other code doing that in the .cpp instead, does it make a difference?

Another 'trend' I noticed is having one massive include file that everything includes, I can see how that is easy to implement, but not sure its the most efficient way?

Cheers
IanG
20
Years of Service
User Offline
Joined: 25th Sep 2004
Location: Cyberspace
Posted: 15th Jan 2006 23:10
i don't use prcompiled headers, i rarely use inclusion guards but every header i have has the first line as #pragma once, it's not really for a speed increase but to stop compiler warnings about the same thing being declared twice

i tend to have everything defined in a different header with the cpp files implementing, all the headers are usually included into a main header include which tends to simplify the including process at the top of each file

as to speed of it all i haven't really tested it, but i care more about getting rid of compile errors due to the compiler getting confused over headers than speed of compilation - often the slowness of it all is a good excuse to make a cuppa


amd athlon xp 2600+,1280mb,FX 5200 128mb,200gb,xp pro sp2
TKF15H
21
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 16th Jan 2006 00:49
In the case of my Minerva project (15mb of code) I had to put all the declarations in .h files and the actual functions grouped up in seperate .cpp files. Once a cpp file is compiled it will not be recompiled in future builds unless a change is made.

WarBasic Scripting engine for DarkBasicPro
DC emulator code size: 14.3MB, 553,214 lines
Lampton Worm
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: United Kingdom
Posted: 16th Jan 2006 16:35
Hi,

I also have a seperate .h and .cpp in the way you described. But what I tend to find at the moment is that if a change blah.cpp (which for example has a blah.h that includes stuff.h) then stuff.cpp/.h also get processed, even though they didn't change (maybe just because they are dependants of blah)..

Cheers.
Vlad
19
Years of Service
User Offline
Joined: 5th Oct 2005
Location:
Posted: 16th Jan 2006 17:03
Just include #pragma once on top.

I do this for all my header files just in case. I don't know if I'm pulling any more problems, but I'm sure I'm solving a lot of stuff on the way. I use #pragma once and then just load all the headers into my main.cpp file. The other files have their includes done one by one.

(Just in case I'm doing something wrong, please say so, maybe this is not the best way to do it)

V

I'm pretty sure I know everything. Doubts are something rare in me and I am never wrong, as this signature can prove.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Jan 2006 14:08
Try reading this:

http://www.gotw.ca/gotw/007.htm

If you can work through and understand it, then you'll be in a good position to work out your header dependancies and reduce your compile times.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
Gu re gu
22
Years of Service
User Offline
Joined: 14th Dec 2002
Location: United Kingdom
Posted: 23rd Jan 2006 14:54 Edited at: 23rd Jan 2006 15:01
With no realisation, the vision is merely a veil, sailing 'pon misconstrued seas of tangibility. Truely undisturbed emotion leaves only the dull residue of creation. But that is enough...

You should probably look into a gcc compiler and play with makefiles. Use a set of programs called mingw if you insist on using windows.
Makefiles and gcc will allow you full control over your object files. An object file is a partially compiled block of code ... maybe one of your .cpp files? These are compiled individually and remain in your directory until you delete them. So the next time you compile, if they're already there, the compiler doesn't need to re-compile them ... simple! Cuts the compile time considerably, especially for large programs like games.
The problem with all these IDEs is that you have no control over what gets compiled or what's going on in general for that matter. Even if you think you know how it all works, you'll soon realise you know relatively nothing, because it insists on pulling together all sorts of project files and other paraphernalia that just seems totally meaningless. Blah ... I'm ranting. Just look for mingw (gcc for windows) and spend a bit of time learning how to use makefiles ... it's a lot more fun and shows you how much control you should really have over your own project.

Wow ... I think my left hand just fell off and ran out of the room. *Frowns slightly* That's not quite right is it?

Adioses...
Guregu

Editified - Wow ... In my sleep deprivation induced stupor I forgot everything I thought I knew. I even forgot the fact that I thought I knew. So actually ... I forgot everything. Oh no ... memory leakage. Errm.
What I'm trying to say is, try using #ifndef and #define rather than pragma once ... surely it makes more sense using the precompiler commands?
eg.

#ifndef __MY_HEADER__
# define __MY_HEADER__

// declarations go here

#endif

Caution...
This machine has no brain.
Use your own

Login to post a reply

Server time is: 2025-05-20 07:48:07
Your offset time is: 2025-05-20 07:48:07