Like Tree1Likes
  • 1 Post By CornedBee

undefined reference to `_SetTextColor@8'

This is a discussion on undefined reference to `_SetTextColor@8' within the C++ Programming forums, part of the General Programming Boards category; Im new to open gl, and im having some troubles: when i try to run this code: Code: Code: #pragma ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6

    undefined reference to `_SetTextColor@8'

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

    when i try to run this code:

    Code:
    Code:
    #pragma comment(linker, "/subsystem:windows")
    /*	Pre-processor directives*/
    #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;
    }
    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 these errors:
    undefined reference to `_SetTextColor@8'
    undefined reference to `GetStockObject@4'
    undefined reference to `TextOutA@20'

    How can i solve this 'undefined reference to `_SetTextColor@8'
    problem??

    PS> I'm a newbie, using Eclipse IDE and MINGW/MSYS toolchain.



    help pls

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why is this thread so incredibly similar to this one?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know Eclipse, but this line is the one that is, theoretically, in charge of what you want:
    Code:
    #pragma comment(linker, "/subsystem:windows")
    If this is Eclipse's way of bringing in other libraries, then fine. If not, then you need to figure out what is Eclipse's way of bringing in other libraries, and use that to bring in the needed library (in this case, gdi32).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This line just ensures in-source that the program is linked not as a console app, but a win32 app. Nothing to do with libraries.

    Also, it has nothing to do with Eclipse, only with the underlying compiler. I actually don't think MinGW GCC supports pragma comment.
    tabstop likes this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yeah, I don't know why I thought Eclipse had a compiler of their own underneath it.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6
    @CornedBee : Yes this thread is similar to the one you linked to..., but I was asked to open a new thread since the other is too old, you'll see that in the last post.

    Indeed Eclipse ingnores the #pragma line.
    It is stated in the build warnings.
    In the other thread there is also some comment about linking some libraries, so that might be the problem.
    The problem is that I don't know how to link other libraries, and what libraries to link...

    Anyone got a clue on how I do that in Eclipse or in code?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A quick check of the eclipse documentation suggests they use makefiles to control compilation; do you have something called "Makefile" in your directory? If so, find the rule for this target and add "-lwindows -lgdi32" to the end of it.

    EDIT: Or if it's not a makefile project: http://help.eclipse.org/indigo/index...al_pns_lib.htm
    Last edited by tabstop; 07-15-2011 at 08:03 AM.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6
    Hi tabstop,

    Thanks so far.
    It's a non-makefile project. So I went to the form as in the help you pointed to....
    So what exactly do I need to fill in libraries form? I can enter a library, or one tab further a library path. When I click on library, it asks me to add a file. Would this be a .dll from the system dir. or something like that?



    PS. As you might notice this is my first time doing such things, I'm a complete noob on this subject....

  9. #9
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6
    OK, forget the last comment.
    I enterred gdi32 in the library list, that did the trick.

    Since I'd like to learn something from this, can someone explain what I'm actually doing by this. Yeah.... Linking a library, but what does that mean in terms of the Building process....

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you go to MSDN, it will tell you which library each of your functions belong to. So yeah, you'll have to add those system libraries to your project.

    ETA: So you're calling the function, so you have to actually at some point have the code that does what you want. That's what the libraries are, bunches of code that do things.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Location
    Netherlands
    Posts
    6
    Maybe a stupid question, but why isn't such a (system) library always linked?
    Does that result in extra time/effort for the linker/compiler, even when the functions are not called?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because there are 11732 such libraries (perhaps a slight overestimate).

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by 0_AintLifeGrand View Post
    .....
    Since I'd like to learn something from this, can someone explain what I'm actually doing by this. Yeah.... Linking a library, but what does that mean in terms of the Building process....
    Take a look here, cprogramming.com's very own explaination.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Quote Originally Posted by tabstop View Post
    Because there are 11732 such libraries (perhaps a slight overestimate).
    And, of course, there can be custom libraries--IE libraries YOU make. Obviously the compiler cannot be aware of all those libraries, so you have to help it find some code sometimes.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. : undefined reference to `pow'
    By mohitsaxena019 in forum C Programming
    Replies: 13
    Last Post: 07-25-2010, 08:35 AM
  2. undefined reference to `
    By lehe in forum C Programming
    Replies: 3
    Last Post: 03-27-2009, 04:59 AM
  3. Undefined Reference to...
    By JJFMJR in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2008, 03:09 AM
  4. undefined reference
    By B_Love in forum C++ Programming
    Replies: 6
    Last Post: 09-26-2005, 08:13 AM
  5. undefined reference
    By laasunde in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2002, 11:44 AM

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