PDA

View Full Version : linker errors, even after correct linker options



10-09-2002, 02:34 AM
i tried to compile NeHe's tutorial one under devcpp, however, line 254
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?

and line 261
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?

cause linker errors "undefined reference to blah"

after i set the linker options
"-lopengl32 -lglu32 -lglaux"

nexusone
10-09-2002, 03:26 AM
The error must be in blah routine, find the blah routine and check for errors. Or maybe you need to include the blah.lib to your linker.

http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by Dan123:
i tried to compile NeHe's tutorial one under devcpp, however, line 254
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?

and line 261
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?

cause linker errors "undefined reference to blah"

after i set the linker options
"-lopengl32 -lglu32 -lglaux"

10-09-2002, 03:28 AM
oh yeah, heres the compile log

Building Makefile: "C:\Dev-Cpp\Dan\openGL\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Dan\openGL\Makefile.win" all
g++.exe Lesson1.o ogl.res -o "ogl.exe" -L"C:\DEV-CPP\lib" -L"C:\Dev-Cpp\lib" -lopengl32 -lglu32 -lglaux -I"C:\DEV-CPP\include" -I"C:\DEV-CPP\include\g++-3" -I"C:\DEV-CPP\include" -I"C:\Dev-Cpp\Dan\Include" -g3

Lesson1.o: In function `CreateGLWindow(char *, int, int, int, bool)':
//C/Dev-Cpp/Dan/openGL/Lesson1.cpp:254: undefined reference to `ChoosePixelFormat@8'
//C/Dev-Cpp/Dan/openGL/Lesson1.cpp:261: undefined reference to `SetPixelFormat@12'

Execution terminated

10-09-2002, 03:30 AM
when i said blah, i meant insert the two routines choosepixelformat and setpixelformat

nexusone
10-09-2002, 05:05 AM
I have not used the dev-cpp compiler, but it looks like it can not find a windows function. There maybe an extra library that is needed for windows, with that compiler.

Have you included #include<windows.h>

Also check the help files for your compiler?



Originally posted by Dan123:
oh yeah, heres the compile log

Building Makefile: "C:\Dev-Cpp\Dan\openGL\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Dan\openGL\Makefile.win" all
g++.exe Lesson1.o ogl.res -o "ogl.exe" -L"C:\DEV-CPP\lib" -L"C:\Dev-Cpp\lib" -lopengl32 -lglu32 -lglaux -I"C:\DEV-CPP\include" -I"C:\DEV-CPP\include\g++-3" -I"C:\DEV-CPP\include" -I"C:\Dev-Cpp\Dan\Include" -g3

Lesson1.o: In function `CreateGLWindow(char *, int, int, int, bool)':
//C/Dev-Cpp/Dan/openGL/Lesson1.cpp:254: undefined reference to `ChoosePixelFormat@8'
//C/Dev-Cpp/Dan/openGL/Lesson1.cpp:261: undefined reference to `SetPixelFormat@12'

Execution terminated

Deiussum
10-09-2002, 05:41 AM
The ChoosePixelFormat, DescribePixelFormat, and SetPixelFormat functions are in the gdi32.dll, so I'm guessing you need to add something like -lgdi32.

*Aaron*
10-09-2002, 06:40 AM
You always have to link libgdi32.a (with -lgdi32) in OpenGL programs. Whenever you get an "undefined reference" error, the first thing you should do is search for the name of the function in the Windows documentation to find out what library is missing. If you don't have windows docs, you can get them here:

ftp://ftp.cs.virginia.edu/pub/lcc-win32/win32hlp.exe


[This message has been edited by *Aaron* (edited 10-09-2002).]

marcus256
10-09-2002, 11:31 AM
Originally posted by *Aaron*:
You always have to link libgdi32.a (with -lgdi32) in OpenGL programs.

Not 100% true. If you're coding OpenGL using win32 directly, then yes. If you're using DLL versions of GLUT or any other toolkit, you probably don't have to do it (the DLLs are linked with gdi32, so your exe doesn't have to be).

BTW this is a common misstake for me - I always use the static link version of GLFW, which uses gdi32. The next GLFW release will load gdi32.dll itself, so even if you link with the static link version of GLFW you won't have to link with gdi32 http://www.opengl.org/discussion_boards/ubb/smile.gif

And on another note: most compilers (including Dev-CPP/MinGW) automatically link with gdi32 when you build a windows app (not a console app), which is done with '-mwindows' for MinGW. If you're building a console app, you need to specify -lgdi32 yourself (which is why NeHe says that you should "create a Windows Application, *NOT* a Cosnole Application" in MSVC).

*Aaron*
10-09-2002, 12:04 PM
I guess you're right. The only GL toolkit I have used is FLTK, and I always link the static lib and use console apps, so I'm in the habit of adding -lgdi32.


The next GLFW release will load gdi32.dll itself, so even if you link with the static link version of GLFW you won't have to link with gdi32Do you mean it will call LoadLibrary and then call GetProcAddress for every GDI function it uses? What's the point of that?

10-10-2002, 01:22 AM
Thanks, it works now!
i had selected empty project, and added the source file.
once i made a windows app, all was good!

marcus256
10-10-2002, 02:25 AM
Originally posted by *Aaron*:

The next GLFW release will load gdi32.dll itself, so even if you link with the static link version of GLFW you won't have to link with gdi32
Do you mean it will call LoadLibrary and then call GetProcAddress for every GDI function it uses? What's the point of that?

Yes, that's what I mean, and the point is exactly what I said in the original post - you don't have to worry about linking with GDI32 yourself. GLFW only uses five gdi32 functions, so it's not a big deal. They are not used frequently either, so there will be no performance hit.

Basically, even with the static version of GLFW it's now this simple to compile a GLFW program:

gcc myprog.c -lglfw -lopengl32 -o myprog.exe

(except for under MSVC, where you also have to specify user32.lib)