Thread: stdafx.h question

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    stdafx.h question

    OK i was going through the graphics tutorial stuff on the main site and copy and pasted code into compiler to run it and it said it didnt have the stdafx.h so i did a search on my computer for it and found 254 instances of stdafx.h none of which in the include folder.


    so my question is out of the 254 copies of that file which all seem to be different when i click on them why do i have so many copies of this file and which file is the actual one i want to stick in the include folder?
    hooch

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ok well i decided to just dump a random stdafx.h in there and now when i try and execute it tells me this:


    Code:
    /*	Trim fat from windows*/
    #define WIN32_LEAN_AND_MEAN	
    #pragma comment(linker, "/subsystem:windows")
    /*	Pre-processor directives*/
    #include <stdafx.h>
    #include <windows.h>
    /*	Windows Procedure Event Handler*/
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	PAINTSTRUCT paintStruct;
    	/*	Device Context*/
    	HDC hDC; 
    	/*	Text for display*/
    	char string[] = "Hello, World!"; 
    	/*	Switch message, condition that is met will execute*/
    	switch(message)
    	{
    		/*	Window is being created*/
    		case WM_CREATE: 
    			return 0;
    			break;
    		/*	Window is closing*/
    		case WM_CLOSE: 
    			PostQuitMessage(0);
    			return 0;
    			break;
    		/*	Window needs update*/
    		case WM_PAINT: 
    			hDC = BeginPaint(hwnd,&paintStruct);
    			/*	Set txt color to blue*/
    			SetTextColor(hDC, COLORREF(0x00FF0000));
    			/*	Display text in middle of window*/
    			TextOut(hDC,150,150,string,sizeof(string)-1);
    			EndPaint(hwnd, &paintStruct);
    			return 0;
    			break;
    		default:
    			break;
    	}
    	return (DefWindowProc(hwnd,message,wParam,lParam));
    }
    /*	Main function*/
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	WNDCLASSEX  windowClass;		//window class
    	HWND		hwnd;				//window handle
    	MSG			msg;				//message
    	bool		done;				//flag saying when app is complete
    	/*	Fill out the window class structure*/
    	windowClass.cbSize = sizeof(WNDCLASSEX);
    	windowClass.style = CS_HREDRAW | CS_VREDRAW;
    	windowClass.lpfnWndProc = WndProc;
    	windowClass.cbClsExtra = 0;
    	windowClass.cbWndExtra = 0;
    	windowClass.hInstance = hInstance;
    	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	windowClass.lpszMenuName = NULL;
    	windowClass.lpszClassName = "MyClass";
    	windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
    	/*	Register window class*/
    	if (!RegisterClassEx(&windowClass))
    	{
    		return 0;
    	}
    	/*	Class registerd, so now create window*/
    	hwnd = CreateWindowEx(NULL,		//extended style
    		"MyClass",			//class name
    		"A Real Win App",		//app name
    		WS_OVERLAPPEDWINDOW |		//window style
    		WS_VISIBLE |
    		WS_SYSMENU,
    		100,100,			//x/y coords
    		400,400,			//width,height
    		NULL,				//handle to parent
    		NULL,				//handle to menu
    		hInstance,			//application instance
    		NULL);				//no extra parameter's
    	/*	Check if window creation failed*/
    	if (!hwnd)
    		return 0;
    	done = false; //initialize loop condition variable
    	/*	main message loop*/
    	while(!done)
    	{
    		PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
    		if (msg.message == WM_QUIT) //check for a quit message
    		{
    			done = true; //if found, quit app
    		}
    		else
    		{
    			/*	Translate and dispatch to event queue*/
    			TranslateMessage(&msg); 
    			DispatchMessage(&msg);
    		}
    	}
    	return msg.wParam;
    }
    --------------------Configuration: first graphics - Win32 Debug--------------------
    Linking...
    LINK : fatal error LNK1104: cannot open file "nafxcwd.lib"
    Error executing link.exe.

    first graphics.exe - 1 error(s), 0 warning(s)
    hooch

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    stdafx.h is something msvc++ generates...I've only used Visual Studio.Net 2005 Beta a little, but I always just make an empty project so I don't have to bother with it.

    Someone who uses an MS compiler could probably tell you more...If you're not using an MS compiler you shouldn't need it.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    stdafx.h is used for precompiled headers (it makes your build faster). If the tutorial doesn't use it, and you don't want to use it, then you can turn it off and remove the file from your project. To turn it off go to Project Settings or Properties and look for the Precompiled Headers options. You can also create a new project without them on depending on which version of VC++ you are using. If you want to keep the precompiled headers, then all your source files should include stdafx.h first, and stdafx.h should have all the #includes that you use a lot.

    To fix the link error you have to link to the library. I'm not familiar with that one, but if you have it you should make sure it is included in your project settings under additional libraries.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ah yea im using 6.0 and i knew .net generated it for some reason or another

    but yep i just tried deleting that right before you replied and it works fine now lol

    so that file is useless unless its in .net?
    hooch

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, its the same in 6.0 and .Net. If your application is small and your build times are small, then you don't need it so its fine to leave it out, plus your code will be more portable if you do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM