Problem with #include "stdafx.h"

This is a discussion on Problem with #include "stdafx.h" within the C++ Programming forums, part of the General Programming Boards category; Hi everyone I dont know if this is the right place to post this but... Im new to open gl, ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    84

    Problem with #include "stdafx.h"

    Hi everyone

    I dont know if this is the right place to post this but...

    Im new to open gl, and im having some troubles:

    when i try to run this code:
    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;
    }
    it give me the " stdafx.h: No such file or directory " error...

    Ive been searching for other posts regarding this problem, but what i found told me to delete the #include "stdafx.h" line, but when i delete this line i get this error:
    undefined reference to `_SetTextColor@8'

    So how can i solve this problem??

    help pls

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    SetTextColor requires you to link with gdi32.lib (or gdi32.dll). (EDIT: MSDN will tell you what libraries are needed for the Windows API functions.)

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    If can delete stdafx.h, but you will then have to include the proper include files required for your project.
    In your case
    Code:
     #include <Windows.h>

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    84
    Yes, i have included the windows.h, but it still gives me that error,

    and how can i link that dll??

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Try adding
    Code:
     #include <Wingdi.h>
    as shown at SetTextColor Function (Windows)

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    84
    didnt worked :\
    instead it gives me a bunch of errors like:

    error: `UINT' does not name a type

    help pls

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    That header is included by windows.h. Are you using an IDE other then visual studio for doing this? If so you will want to use their project templates that automatically link in some libraries for a GUI project. The big 3 you will need would be user32.lib, kernel32.lib, and gdi32.lib.

    If you are using visual studio then just make sure you start with a win32 gui application template.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by shiroaisu View Post
    Yes, i have included the windows.h, but it still gives me that error,

    and how can i link that dll??
    The header files and the libraries are not the same thing. You need windows.h as a header, but you need to link with (not include) the library (not header file) gdi32.lib. You'll need to go to your linker properties and add the library.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    - delete the stdafx.h file from your source code
    - make sure that project->settings->pre-processor->use precompiled headers is set to OFF

    I'm not sure of the exact dialog sequence to get to the PCH option, but it's down there somewhere
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    84
    Ok dudes, i solved it,

    i needed to #include "resource.h",
    i did it and now it works, btw im using code blocks as a compiler... i probabily shouldve said this earlier -.- sory bout that..

    Tks for ur help everyone

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    stdafx.h is not a standard header; it's usually just a header that contains a lot of includes for use with PCH. PCH speeds up compile time by a lot when you include many heavy headers (such as windows.h).
    So, it is usually safe to delete it. But in case your IDE (such as VS) is configured to use PCH, it will be looking for that file, in which case you can either a) create it and put all your heavy includes in it or b) disable PCH.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6
    Hi All,

    Newbie having the same problem as is posted here. I'm working in Eclipse (C++) IDE with MinGW/MSYS though .
    Including resources.h does not cut it for me. Anyone know how I can solve this problem?
    Can anyone tell me how to link to user32.lib, kernel32.lib, and gdi32.lib as is suggested in this thread?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Well we can only assume that you didn't read the rules before digging up this old thread with "me too".
    Try posting a NEW thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  2. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  3. include problem
    By Strait in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2005, 04:01 PM
  4. Read and write hanging
    By zee in forum C Programming
    Replies: 8
    Last Post: 08-04-2004, 12:19 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM

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