-
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?
-
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)
-
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.
-
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.
-
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?
-
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.