Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Linking openGL/Glut in Code::Blocks

This thread is locked; no one can reply to it. rss feed Print
Linking openGL/Glut in Code::Blocks
Skywhy
Member #9,243
November 2007
avatar

As the topic says, having problems with linking files. Again. I never seem to learn this. Oh well... Here we go. The idea is to setup opengl and glut properly, sounds simple.

Files I got and places I threw them in:
<WINDOWS>\system32:
- glu32.dll
- opengl32.dll
- glut32.dll

<CodeBlocks>\include\GL:
- glu.h
- gl.h
- glut.h

<CodeBlocks>\lib:
- glu32.lib
- glut32.lib
- opengl32.lib

I think I've got the files in right places, correct me if I'm wrong. But simple examples won't compile. I get loads of errors and I think it's because I don't really know what to link...

In Project > Project Options ... > Linker Settings > Other linker options I've tried using: -lopengl32 -lglu32 -lglut32.

And still I get the same errors, which are:

Quote:

\CodeBlocks\include\GL\glut.h|58|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|66|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|67|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|68|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|76|warning: ignoring #pragma warning |
\CodeBlocks\include\GL\glut.h|77|warning: ignoring #pragma warning |
\CodeBlocks\include\GL\glut.h|549|warning: 'glutCreateMenu_ATEXIT_HACK' defined but not used|
obj\hello.o||In function `glutInit_ATEXIT_HACK':|
\CodeBlocks\include\GL\glut.h|486|undefined reference to `___glutInitWithExit@12'|
obj\hello.o||In function `glutCreateWindow_ATEXIT_HACK':|
\CodeBlocks\include\GL\glut.h|503|undefined reference to `___glutCreateWindowWithExit@8'|
obj\hello.o||In function `glutCreateMenu_ATEXIT_HACK':|
\CodeBlocks\include\GL\glut.h|549|undefined reference to `___glutCreateMenuWithExit@8'|
obj\hello.o||In function `main':|
hello.c|51|undefined reference to `_glutInitDisplayMode@4'|
hello.c|52|undefined reference to `_glutInitWindowSize@8'|
hello.c|53|undefined reference to `_glutInitWindowPosition@8'|
hello.c|56|undefined reference to `_glutDisplayFunc@4'|
hello.c|57|undefined reference to `_glutMainLoop@0'|
||=== Build finished: 8 errors, 7 warnings ===|

###

The actual code is opengl/glut example, hello.c:

1#include <windows.h>
2#include <GL/gl.h>
3#include <GL/glut.h>
4#include <GL/glu.h>
5#include <stdlib.h>
6 
7void display(void)
8{
9/* clear all pixels */
10 glClear (GL_COLOR_BUFFER_BIT);
11 
12/* draw white polygon (rectangle) with corners at
13 * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
14 */
15 glColor3f (1.0, 1.0, 0.0);
16 glBegin(GL_POLYGON);
17 glVertex3f (0.25, 0.25, 0.0);
18 glVertex3f (0.75, 0.25, 0.0);
19 glVertex3f (0.75, 0.75, 0.0);
20 glVertex3f (0.25, 0.75, 0.0);
21 glEnd();
22 
23/* don't wait!
24 * start processing buffered OpenGL routines
25 */
26 glFlush ();
27}
28 
29void init (void)
30{
31/* select clearing color */
32 glClearColor (0.0, 0.0, 0.0, 0.0);
33 
34/* initialize viewing values */
35 glMatrixMode(GL_PROJECTION);
36 glLoadIdentity();
37 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
38}
39 
40/*
41 * Declare initial window size, position, and display mode
42 * (single buffer and RGABA). Open window with "hello"
43 * in its title bar. Call initialization routines.
44 * Register callback function to display graphics.
45 * Enter main loop and process events.
46 */
47int main(int argc, char** argv)
48{
49 glutInit(&argc, argv);
50 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
51 glutInitWindowSize (250, 250);
52 glutInitWindowPosition (100, 100);
53 glutCreateWindow ("hello");
54 init ();
55 glutDisplayFunc(display);
56 glutMainLoop();
57 return 0; /* ANSI C requires main to return int. */
58}

So as far as code comes, no magic there really... Just having the time of my life linking these files. I tried this: Using OpenGL & GLUT in Code::Blocks and read through several other stuff but couldn't really solve this...

Someone, point me in the right direction. What am I doing wrong? How do I get these to compile?

</code>

Aaron Bolyard
Member #7,537
July 2006
avatar

According to this, you need to add the following to the commandline:

-D_STDCALL_SUPPORTED -D_M_IX86

And then it should link properly.

edit:
Oh, wait, you included the windows header before then... Try adding the library as it says so in the tutorial, not with the linker options.

wiseguy
Member #44
April 2000
avatar

Aaron Bolyard said:

Oh, wait, you included the windows header before then... Try adding the library as it says so in the tutorial, not with the linker options.

That's right, instead of using the linker options, browse for the libraries in the other half of that tab, and add them to the project one at a time, preserving relative paths.

Skywhy
Member #9,243
November 2007
avatar

Ok, I did so but still having problems. Now it just crashes when I run it. I get few warnings but I think they are not the cause of this crash.

Quote:

\CodeBlocks\include\GL\glut.h|58|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|66|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|67|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|68|warning: ignoring #pragma comment |
\CodeBlocks\include\GL\glut.h|76|warning: ignoring #pragma warning |
\CodeBlocks\include\GL\glut.h|77|warning: ignoring #pragma warning |
\CodeBlocks\include\GL\glut.h|549|warning: 'glutCreateMenu_ATEXIT_HACK' defined but not used|
||=== Build finished: 0 errors, 7 warnings ===|

It runs and instantly crashes before showing anything. Nice. :)
Any ideas what's wrong?

EDIT #1:
Ooohkay, this is getting interesting. I don't know what's the matter, but I think I've somehow managed to mess up something real bad :P

Before this linking / testing I tried out the Code::Blocks own openGL template, it worked and compiled fine and showed me the piece of openGL neatnes it created. Now when I try compiling it, it pops up and instantly crashes... Hmm... Something wrong with my precious openGL now? Oh boy, this is hard. :D

wiseguy
Member #44
April 2000
avatar

Are you sure that the DLLs you have match the libraries you're using? When I had problems with this, I found out that I had older DLL files than the libraries I was using.

Other than that, I'm not sure. Maybe something in your display function?

Skywhy
Member #9,243
November 2007
avatar

I'd go as far as blaming the dll files... Obviously I don't about the code for sure, since I'm trying to learn OpenGL but this monster is fighting me with all of it's might.

Any advices where to find the latest dll's ? Since it seems that everything opengl / glut related is spread among the internet in random bits and pieces, your supposed to collect them and put the pieces together.

Any site that actually has like.. I don't know, one rar that contains example .dll, .lib and .h files for openGL? And same thing for glut and glu.

Oh well, It's already past midnight here, I'll hit the sack and fight with this some more tomorrow when I get off work / school. This is really starting to tick me off :)

[rand] Can't really understand why this is so darn hard. [/rand]

Any advices, sites and so on are welcome.

wiseguy
Member #44
April 2000
avatar

Well, what I did was delete the DLLs in the system32 directory, and then copy over the ones from your compiler's dll/lib directory. If they aren't there you might try the MinGW or CodeBlocks site.

Thomas Fjellstrom
Member #476
June 2000
avatar

You did not overwrite opengl32.dll in system did you? :o each system32/opengl32.dll is specific to the drivers you have installed. Overwriting it with, say MESA's opengl32.dll will leave you with software rendering (or just a broken opengl setup).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Skywhy
Member #9,243
November 2007
avatar

Thomas said:

You did not overwrite opengl32.dll in system did you? each system32/opengl32.dll is specific to the drivers you have installed. Overwriting it with, say MESA's opengl32.dll will leave you with software rendering (or just a broken opengl setup).

Heh, no I did not. :)

Finally, I got it working. I don't know for sure where the problem was but I think it was in the libraries. I even tried using libglut32.a but in the end, after thousand copies of glut32.lib I got it working. The current glut32.lib is from here. This was the first glut I tried but I think I had some other linker settings missing then... Oh joy, this is HELL. I can't believe how hard this was.

Okay, still one question remains, now that the library paths are relative in the linker options, what if I'm doing the project with a group and they have different kind of file structure? I suppose it doesn't work since The compiler can't find the libraries in the specified paths.

How do I get around this? With use of -l<something>?

Thomas Fjellstrom
Member #476
June 2000
avatar

You should tell your IDE about the libs via its global (or project local) library paths settings.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

gnolam
Member #2,030
March 2002
avatar

Quote:

Finally, I got it working. I don't know for sure where the problem was but I think it was in the libraries. I even tried using libglut32.a but in the end, after thousand copies of glut32.lib I got it working. The current glut32.lib is from here [xmission.com]. This was the first glut I tried but I think I had some other linker settings missing then... Oh joy, this is HELL. I can't believe how hard this was.

It's GLUT. It actually working is a rare sight to behold.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Skywhy
Member #9,243
November 2007
avatar

Thomas said:

You should tell your IDE about the libs via its global (or project local) library paths settings.

Forgive me for my stupidity, care to throw in any urls/examples/explanations on this?

With our current Allegro project we are using -lalleg(+few other) and it works like a wonder with both of our IDE's. And if we do similiar linking as in the example above with our current Allegro project it won't work on my friends IDE. Since the path's differ.

So do you mean we should use like.. err... -lglut32 with the GLUT so it would work on both of our IDE's? :P OR do we need to change the library path's everytime the project gets passed to my friend or he passes it to me? Holy hell, my small head can't hold this stuff...

Somebody, hold my hand and explain this to me like you are talking to a five year old. :o

gnolam said:

It actually working is a rare sight to behold.

Really..? Should I be scared? I know it's a bit ancient but the examples I've got, start out with it, so I'll live with it. I have to start somewhere. And now I'll just keep hoping it won't blow up again.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

So do you mean we should use like.. err... -lglut32 with the GLUT so it would work on both of our IDE's? :P OR do we need to change the library path's everytime the project gets passed to my friend or he passes it to me? Holy hell, my small head can't hold this stuff...

Uh, IF you have the gl libs installed properly, your ide will find it naturally if you just say "link to foo". IF you don't have things installed properly, you're hosed. Simple as that.

IDEs like MSVC and Code:Blocks have two sets of library paths it searches, Global (for all projects), and local, per project paths. I suggest setting up GL to be in the global library paths.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Skywhy
Member #9,243
November 2007
avatar

Thank you. Solved.

Go to: