Header file stdafx.h

May 11, 2012 at 4:45pm
Hi all, I'm writing a program that calls other programs, that in-turn calls other programs, and I'm concerned about duplicating #include header files. I'm just learning about header files, and I'm particularly interested in the stdafx.h header file because of what it can do. I Googled stdafx.h and got:
1
2
3
4
'stdafx' is just the default name for the 'precompiled header directive'. The headers that are included in stdafx.h are compiled once,
and the information is saved to disk. When a file includes stdafx.h, the compiler just loads the precompiled data instead of
re-parsing all the headers. Typically, more times is spent parsing header files than actually compiling your code, so using
precompiled headers can dramatically speed up your build times.


However, I'm a little confused when writing my own. . . specifically the Header Guards, which I understand is:
1
2
3
4
5
6
#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
 
// your declarations here
 
#endif 

Since I do not plan to enter any declarations, only #include statements; how do I code for Header Guards, since I plan to enter all of the following #include statements inside of my home-made stdafx.h statement?
1
2
3
4
5
6
#include <iostream>
#include <fstream>
#include <string>
#include <ios>
#include <stdio.h>
#include <stdlib.h> 


Also, I'm not sure if I can include *using namespace std;* in the stdafx.h header file. An example would be greatly appreciated....thanks
May 11, 2012 at 5:17pm
You shouldn't need to worry about the header guards in the includes you list. Doesn't this work?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#ifndef THERRY1_UNIQUE_NAME
#define THERRY1_UNIQUE_NAME

#include <iostream>
#include <fstream>
#include <string>
#include <ios>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#endif


May 11, 2012 at 6:41pm
Those includes already have their own header guards so including them multiple times isn't a problem. Include away!
May 11, 2012 at 8:16pm
kooth, could you explain?
1
2
#ifndef THERRY1_UNIQUE_NAME
#define THERRY1_UNIQUE_NAME 

I thought
1
2
#ifndef THERRY1_UNIQUE_NAME
#define THERRY1_UNIQUE_NAME 
were supposed to be the unique name of a header files? Since all this will be going inside the header file *stdafx.h* should THERRY1_UNIQUE_NAME be replaced with stdafx.h, or STDAFX_H?

Stewbond, Thanks for the info.
May 15, 2012 at 2:15pm
It really can be anything that you define so that your headers are included only once. I think replacing what I said with STDAFX_H is just fine and dandy!
Topic archived. No new replies allowed.