About precompiled headers

May 7, 2011 at 12:22pm
As far as i understood if we add headers (for example from STL) like this:
1
2
3
4
#include <iostream>
#include <sstream>
#include <string>
...and so on

the order of inclusion doesn't matter? and times added into various files too?

Let say i have some common functions in one or more headers that i want to have same behavior as above ones, how do i create one?

Hope you understand what i meant.
Thanks in advance for your time.
Last edited on May 7, 2011 at 12:23pm
May 7, 2011 at 4:56pm
... the order of inclusion doesn't matter?

You are correct, the order does not matter, because they have been designed that way.

how do i create [the same behavior for my files]?

Use include guards. Each one of your header files should have the following layout:

1
2
3
4
5
6
7
8
9
10
// foo.hpp
// ...

#pragma once
#ifndef SAVAMPIRS_FOO_HPP
#define SAVAMPIRS_FOO_HPP

// class definitions, function prototypes, and extern declarations go here

#endif 

The item on line 4 is useful on a lot of compilers to do what you want, but it doesn't work with all compilers. (It is still good to have there just in case it does work.)

Line 5 says "everything from here to the matching #endif on line 10 only gets compiled if the symbol 'SAVAVAMPIRS_FOO_HPP' is not already defined."
The first time the file is #included in your project, your special symbol is not yet defined, so everything between lines 5 and 10 does get compiled.

Line 6 actually defines the symbol, so if the file gets #included again, nothing between lines 5 and 10 can get compiled.

Make sure to use a different symbol for each header file.
Also, make sure that your symbol is unique -- you don't want anyone else to #define it elsewhere.

About precompiled headers

The term "precompiled headers" actually means something other than what you want. A precompiled header does not actually need to be included, because the compiler already knows what is in it. This is another topic entirely so you can forget about it for now.

What you are asking about is how to "prevent multiple includes" or how to "prevent the same file from being included more than once".

Hope this helps.
May 7, 2011 at 5:23pm
Ok.
1
2
3
4
5
6
7
8
9
10
// foo.hpp
// ...

#pragma once
#ifndef SAVAMPIRS_FOO_HPP
#define SAVAMPIRS_FOO_HPP

// class definitions, function prototypes, and extern declarations go here

#endif  

Is there a need for "include guard" if my compiler suport #pragma once directive? So that i use just that, and work without "include guard" because right now i am not planing to port my code to different compilers then my own.

I am (more or less) familiar with "prevent multiple includes" & "circular dependency" problems, with witch i have so far no trouble with, i was just interested if i can create headers with behavior like STL ones (read-only).
Last edited on May 7, 2011 at 5:24pm
May 7, 2011 at 11:16pm
Yes. Always use the #ifndef ... #define ... #endif include guards. The #pragma once thing is just for compilers that can optimize on that, and it prevents errors when it breaks on those compilers also.

But now I am confused. What do you mean by "read-only" STL headers?
Topic archived. No new replies allowed.