Archived

This topic is now archived and is closed to further replies.

First OpenGL problem

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I copied this program straight from the book "OpenGL Game Programming" and I get Linker errors heres the program:
  
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

float angle = 0.0f;
HDC g_HDC;

void SetupPixelFormat(HDC hDC)
{
	int nPixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0 };
	
	nPixelFormat = ChoosePixelFormat(hDC, &pfd);

	SetPixelFormat(hDC, nPixelFormat, &pfd);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HGLRC hRC;
	static HDC hDC;
	char string[] = "Hello, World!";
	int width, height;

	switch(message)
	{
		case WM_CREATE:

			hDC = GetDC(hwnd);
			g_HDC= hDC;
			SetupPixelFormat(hDC);

			hRC = wglCreateContext(hDC);
			wglMakeCurrent(hDC, hRC);

			return 0;
			break;

		case WM_CLOSE:

			wglMakeCurrent(hDC, NULL);
			wglDeleteContext(hRC);

			PostQuitMessage(0);

			return 0;
			break;

		case WM_SIZE:
				
			height = HIWORD(lParam);
			width = LOWORD(wParam);

			if (height==0)
			{
				height = 1;
			}
			glViewport(0, 0, width, height);
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();

			gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			return 0;
			break;

		default:
			break;

	}
	
	return (DefWindowProc(hwnd, message, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX windowClass;
	HWND hwnd;
	MSG msg;
	bool done;

	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	= NULL;
	windowClass.lpszMenuName	= NULL;
	windowClass.lpszClassName	= "MyClass";
	windowClass.hIconSm			= LoadIcon(NULL, IDI_WINLOGO);

	if (!RegisterClassEx(&windowClass))
		return 0;

	hwnd = CreateWindowEx(NULL,
						"MyClass",
						"The OpenGL Window Application",
						WS_OVERLAPPEDWINDOW | WS_VISIBLE |
						WS_SYSMENU | WS_CLIPCHILDREN |
						WS_CLIPSIBLINGS,
						100, 100,
						400, 400,
						NULL,
						NULL,
						hInstance,
						NULL);

	if (!hwnd)
		return 0;

	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);

	done = false;

	while (!done)
	{
		
		PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

		if (msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();

			angle = angle + 0.1f;
			if (angle >= 360.0f)
				angle = 0.0f;
			glTranslatef(0.0f,0.0f,-5.0f);
			glRotatef(angle, 0.0f,0.0f,1.0f);

			glColor3f(1.0f,0.0f,0.0f);
			glBegin(GL_TRIANGLES);
				glVertex3f(0.0f,0.0f,0.0f);
				glVertex3f(1.0f,0.0f,0.0f);
				glVertex3f(1.0f,1.0f,0.0f);
			glEnd();

			SwapBuffers(g_HDC);

			TranslateMessage(&msg);
			DispatchMessage(&msg);

		}

	}
	return msg.wParam;
}
  
and these are the linker errors: --------------------Configuration: OpenGLWA1 - Win32 Debug-------------------- Linking... OpenGLWA1.obj : error LNK2005: _WinMain@16 already defined in bah.obj bah.obj : error LNK2001: unresolved external symbol _gluPerspective@32 bah.obj : error LNK2001: unresolved external symbol __imp__glLoadIdentity@0 bah.obj : error LNK2001: unresolved external symbol __imp__glMatrixMode@4 bah.obj : error LNK2001: unresolved external symbol __imp__glViewport@16 bah.obj : error LNK2001: unresolved external symbol __imp__wglDeleteContext@4 bah.obj : error LNK2001: unresolved external symbol __imp__wglMakeCurrent@8 bah.obj : error LNK2001: unresolved external symbol __imp__wglCreateContext@4 bah.obj : error LNK2001: unresolved external symbol __imp__glEnd@0 bah.obj : error LNK2001: unresolved external symbol __imp__glVertex3f@12 bah.obj : error LNK2001: unresolved external symbol __imp__glBegin@4 bah.obj : error LNK2001: unresolved external symbol __imp__glColor3f@12 bah.obj : error LNK2001: unresolved external symbol __imp__glRotatef@16 bah.obj : error LNK2001: unresolved external symbol __imp__glTranslatef@12 bah.obj : error LNK2001: unresolved external symbol __imp__glClear@4 Debug/OpenGLWA1.exe : fatal error LNK1120: 14 unresolved externals Error executing link.exe. OpenGLWA1.exe - 16 error(s), 0 warning(s) I almost possitive I am making it a WIN32 Application But you guys are the OpenGL experts Thx, Jeff Desrosiers

Share this post


Link to post
Share on other sites
That was quick ok thx. I stopped reading the book a while ago because I didn''t understand nething that was going on so I fergot this. Thx again

Jeff

Share this post


Link to post
Share on other sites
I got most of the linker problems down but now I still got these 2:
--------------------Configuration: OpenGLWA1 - Win32 Debug--------------------
Linking...
OpenGLWA1.obj : error LNK2005: _WinMain@16 already defined in bah.obj
Debug/OpenGLWA1.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

OpenGLWA1.exe - 2 error(s), 0 warning(s)

Thx Drizzt

Share this post


Link to post
Share on other sites
You have to create a normal Win32 Console Application, not the Dos Console thingy.... (unless you''re working with glut, which you''re obviously not..)

cya,
Phil


Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states

Share this post


Link to post
Share on other sites
You have a function called WinMain in both "bah.cpp" and and "OpenGLWA1.cpp" (or *.c). You must either remove or rename one of them.

BTW, there is no need for you to include "glaux.h" or link to "glaux.lib" in your program, since you use no functionality from glaux. In general you should avoid using glaux. If you don''t know what glaux is, then never mind, you don''t need to


Drizzt: Linker errors can almost never be solved by including a header.

Share this post


Link to post
Share on other sites
quote:
Original post by phueppl1
You have to create a normal Win32 Console Application, not the Dos Console thingy.... (unless you''re working with glut, which you''re obviously not..)


The code Jeff D posted should reside in a "Win32 Application", *not* a "Win32 Console Application" (and MSVC++ can not compile DOS apps, so there is no way to create a ''Dos Console thingy'' (unless it''s a really old version of MSVC++).

Besides, had the project type been the problem the error would more likely have been something like:
"OpenGLWA1.obj : error LNK2001: unresolved external symbol _main"

Share this post


Link to post
Share on other sites