#include "stdafx.h"???

In VSC++, If I create a project with "Empty project" which is without "Precompiled header", the project will be empty, but if I wanted to use that #include "stdafx.h" inside my "Empty" project, it doesn't works...

but if I create a project with "Precompiled header", it can work fine.

both the condition above having exactly the same code(Both have #include "stdafx.h" ), but only the one with "Precompile header" Project can work...

May I know how to solve this problem in "Empty Project"??

This is the error when creating the project in "Empty Project"

fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
1>


lastly, Can I know what is #include "stdafx.h" for? Thanks...
but if I wanted to use that #include "stdafx.h" inside my "Empty" project, it doesn't works...


Of course not. stdafx is the precompiled header. If you don't have a precompiled header, then how can you include it?

May I know how to solve this problem in "Empty Project"??


It's not really a problem. Just don't include that header.

Or if you must (if you have dozens of files and you don't want to modify them all), you can "fake" it by creating a blank file named "stdafx.h".


Can I know what is #include "stdafx.h" for?


When you #include a header file, it's sort of like copy/pasting that header file into the cpp file. The compiler will walk through, examine, and compile that header whenever you compile the cpp file.

The compiler will do this process for every cpp file that includes that header. So if you have 100 cpp files in your project and they all include the same header, that header is being compiled 100 times.

If the header is very large, this might cause the computer to take a long time to compile your program. So compilers give you an option to "precompile" a header so that it compiles only once. Then each cpp file that #includes it does not have to re-compile it every time, they can just use the precompiled version. It can speed up compile time.

MSVS likes to name the default precompiled header "stdafx.h". But that's all it is.


It's worth noting that precompiled headers are completely worthless unless you are seeing slow compile times. I avoid them unless they make a significant difference in compile time.
Thanks...
Is that means #include "stdafx.h" is called precompiled header where as #include <iostream> , #include <conio.h> and so on is a library header? Or it's call precompiled header as well?

From what u said, it's completely worthless, is that means, without that #include "stdafx.h" , my system can works fine also?
Is that means #include "stdafx.h" is called precompiled header where as #include <iostream> , #include <conio.h> and so on is a library header?


Not really. A precompiled header can be named anything. "stdafx" is just the default name for it in VS.

Precompiling is a compiler setting. You can precompile any header, even library headers like <iostream>.

From what u said, it's completely worthless, is that means, without that #include "stdafx.h" , my system can works fine also?


Yes. Enabling precompiled headers does absolutely nothing other than change how fast the compile can build the exe. The final program will be identical whether or not you use precompiled headers.
A precompiled header can be named anything.


If I named it as "ABC", it will works too?

Enabling precompiled headers does absolutely nothing other than change how fast the compile can build the exe.


U mean if have that #include "stdafx.h" , the system can build exe file faster? Is that what u mean? I though if we put more header, the time require to compile will be longer, am I right?

TQ....
If I named it as "ABC", it will works too?


Yes. The header can be named anything, as long as the compiler knows to precompile that header.

U mean if have that #include "stdafx.h" , the system can build exe file faster?


Yes, that's the idea. Whether or not it REALLY makes it faster depends. Usually it won't make much/any difference unless stdafx.h is a very big header.

I though if we put more header, the time require to compile will be longer, am I right?


Yes you are right.

Compile time is longer with more headers because the compiler had to compile each header when it is included.

However, if the headers are precompiled it doesn't have to compile them because they have been precompiled already. Which is why it's faster.
However, if the headers are precompiled it doesn't have to compile them because they have been precompiled already. Which is why it's faster.


But u said precompiled header has nothing to do with our program right? Then how to precompile my header since my header used is like #include <iostream> and #include <conio> but not #include "stdafx.h" ...

Is that u mean in one program, I use many (let said 3) #include <iostream> and #include <conio> and if I include that Precompile header (#include "stdafx.h"), then it runs #include <iostream> and #include <conio> only once instead of 3 is it?

And another condition is I use many(let said 3) #include <iostream> and #include <conio> , but not include that Precompile header (#include "stdafx.h") this time, so, is that mean my program will runs #include <iostream> and #include <conio> 3 times?

Yes, that's the idea. Whether or not it REALLY makes it faster depends. Usually it won't make much/any difference unless stdafx.h is a very big header.


U mean stdafx.h is a big header? That header can be big? I thought it's just a constant header which is same size(won't change) with everyone? And somehow, if header is bigger, the time to compile should be longer right? But why big header become faster? Not really understand...can u help explain...

TQ...
Last edited on
The basic idea of precompiled header is: once parsed, used ever after.

Everything in this precompiled header (definitions or other includes) are put together in a file that is used instead of the precompiled header (#include "stdafx.h") itself. It's supposed to be faster, because the compiler doesn't need to open those includes and no more parsing is required.

You can put everything that doesn't change (like #include <iostream> and #include <conio>) in that precomiled header. Only the project specific headers are left out.

That's the idea.

The problem is that the precompiled file quickly becomes that hugh that navigating within is not faster then opening and parsing the headers when needed. Plus it turns out that the precompiled header file is error prone and you get problems while compiling.

The best is to disable precompiled header and forget about that once and forever.
Thanks coder776,
You can put everything that doesn't change (like #include <iostream> and #include <conio>) in that precomiled header. Only the project specific headers are left out.


From here, can I know how to put them #include <iostream> and #include <conio> inside precompiled header (#include "stdafx.h")? Example to the code below, how to put that 2 header inside stdafx.h? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio>

int main()
{
	int a,b,c;


   cin>>a;
   cin>>b;
   c=a+b;
   cout<<"The value c is \n"<<c;
   getch();
   return 0;
}
Well, create a file in that directory where your main file is. Name it stdafx.h. Cut and copy line 1/2 to that file. the first line of your main file (and all other implementation files) must be #include "stdafx.h"
Thanks...

u mean create one main file which have the 1st line as #include "stdafx.h" and the next line will be the library header like the one below is it?

1
2
3
#include "stdafx.h"
#include <iostream>
#include <conio> 


how about the rest? I mean is there anything after line 3? Or just need line 1-3? TQ...
Last edited on
no I mean that you create a file stdafx.h and place #include <iostream> and #include <conio> there.

in the file stdafx.h there're only the lines 2 and 3.

the main file is the file where 'int main()' is. I don't know how you named it. This would look like so:

Your main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"

int main()
{
	int a,b,c;


   cin>>a;
   cin>>b;
   c=a+b;
   cout<<"The value c is \n"<<c;
   getch();
   return 0;
}


stdafx.h
1
2
#include <iostream>
#include <conio> 
Last edited on
I think you're missing some fundamental concept.

The compiler is going to parse and compile every included file every time it compiles a source file (which is every time you change a source file). So for an example, let's say you have the following files:

1
2
3
4
// myclass.h
#include <string>

class MyClass {  /*... */ };

1
2
3
4
// stdafx.h
#include <windows.h>
#include <iostream>
#include "myclass.h" 

1
2
3
4
// main.cpp
#include "stdafx.h"

// ... 

1
2
3
4
// support.cpp
#include "stdafx.h"

//... 


When you build this project, if you do not have a precompiled header the compiler is going to compile the 2 source files main.cpp and support.cpp. When it does, it will do this:

Compiling main.cpp
- found #include "stdafx.h" in main.cpp, so start compiling stdafx.h
- found #include <windows.h> in stdafx.h, so start compiling windows.h
- found XXX other includes in windows.h, so compile all those headers as well
- found #include <iostream> in stdafx.h, so start compiling that header
- found XXX other includes in iostream, so compile all those as well
- found #include "myclass.h" in stdafx.h, so compile that header
- stdafx.h finish, continue compiling main.cpp

Compiling support.cpp
- found #include "stdafx.h" in support.cpp, so start compiling stdafx.h
- found #include <windows.h> in stdafx.h, so start compiling windows.h
- found XXX other includes in windows.h, so compile all those headers as well
- found #include <iostream> in stdafx.h, so start compiling that header
- found XXX other includes in iostream, so compile all those as well
- found #include "myclass.h" in stdafx.h, so compile that header
- stdafx.h finish, continue compiling support.cpp



Now... if you make stdafx.h a precompiled header, the compiler will do this instead:

PreCompiling stdafx.h
- found #include <windows.h> in stdafx.h, so start compiling windows.h
- found XXX other includes in windows.h, so compile all those headers as well
- found #include <iostream> in stdafx.h, so start compiling that header
- found XXX other includes in iostream, so compile all those as well
- found #include "myclass.h" in stdafx.h, so compile that header
- stdafx.h is now compiled, put compiled information in a new "stdafx.pch" file

Compiling main.cpp
- found #include "stdafx.h", but I already compiled that header, so no need to compile it again. Just use the info in the existing pch file.
- continue compiling main.cpp

Compiling support.cpp
- found #include "stdafx.h", but I already compiled that header, so no need to compile it again. Just use the info in the existing pch file.
- continue compiling support.cpp



That's all it does. It just compiles the stuff once so it doesn't need to be compiled every time.
Last edited on
Thanks coder777 and Disch

I think I understand already...

just another question for coder777, as u said

Well, create a file in that directory where your main file is. Name it stdafx.h. Cut and copy line 1/2 to that file. the first line of your main file (and all other implementation files) must be #include "stdafx.h"


first line of your main file (and all other implementation files) must be #include "stdafx.h"


is that must be 1st line? or any line before int main () are allow(maybe put the header after other library file) ? Is that ok? Thanks...
Topic archived. No new replies allowed.