Hi, I am using Visual studio 2008 express edition now. It does not have stdafx.h. so I download stdafx.h and three other head files: AFXWIN.h, AFXEXT.h and AFXCMN.h from google. I put these four headfiles into C:\Program Files\Microsoft Visual Studio 9.0\VC\include. And then I wrote a small program just to test if it is working.
-------------------------------------------------code start

#include <iostream>
#include <stdafx.h>
using namespace std;

int main()
{
    cout << "GHHH"<< endl;
}

---------------------------------------------------code ends

I got these compile information:

-------------------------------------------------compile message
1>------ Build started: Project: Hello, Configuration: Debug Win32 ------
1>Compiling...
1>Hello.cpp
1>c:\program files\microsoft visual studio 9.0\vc\include\afxwin.h(19) : fatal error C1083: Cannot open include file: 'afx.h': No such file or directory
1>Build log was saved at "file://c:\Users\leo\Documents\Visual Studio 2008\Projects\Hello\Hello\Debug\BuildLog.htm"
1>Hello - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
---------------------------------------------------compile msg ends

Could anyone tell me why and how to solve the problem. Thanks a lot!

"stdafx.h" is a header that VC++ automatically creates for every program created using one of the templates/wizards. You can safely leave it out of your program completely. It has nothing of any value in it unless you add to it.

Thank you Fbody. How about I change it to another headfile instead of stdafx.h? Is this method to add headfiles right?

What would you like to change it to? What would be the purpose of it?

You only specify a header because you need to functions it references. You don't add headers because you like adding headers.

For example, If I want to use windows.h and Visual studio 2008 does not have this headfile. So what I do is to download it from google and then paste it to C:\Program Files\Microsoft Visual Studio 9.0\VC\include. However, I know this does not work.
I want to ask how to add headfiles into VC in a easy way.

To use a header, you have to include it in your code. Like you did in your OP...

//I want to use the windows.h header

#include "windows.h"

//this makes the functions and classes available in windows.h
//available in your program

int main()
{
}

Or am I misunderstanding your question?

For example, If I want to use windows.h and Visual studio 2008 does not have this headfile. So what I do is to download it from google and then paste it to C:\Program Files\Microsoft Visual Studio 9.0\VC\include. However, I know this does not work.
I want to ask how to add headfiles into VC in a easy way.

You can't just "download a header" and expect it to work if the associated libraries are not installed as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.