Trying to use example code, is it broken?

I have a query in relation to this website:

http://en.wikibooks.org/wiki/OpenGL_Programming#The_basics_arc

From an example page i found a link to the source code for the examples and decided to download it. I was sure that if ever anything should just work, it would be with fully working examples which had been set up by an expert that I could just open and run.

FYI, the download link page is here: https://gitorious.org/wikibooks-opengl/modern-tutorials/source/90bf72991caf34ea6cdbed044a9616b7bbdf2d0f:

Unfortunately (as expected), they did not work, none of them, from the simplest code to the more complex stuff, compile errors everywhere (usually starting with "___IMP___" for some reason), Here's the build log for tut01_intro\triangle.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x3e): undefined reference to `_imp____glutCreateWindowWithExit@8'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x60): undefined reference to `_imp____glutCreateMenuWithExit@8'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0xb1): undefined reference to `_imp____glewCreateShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0xd5): undefined reference to `_imp____glewShaderSource'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0xfe): undefined reference to `_imp____glewCompileShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x110): undefined reference to `_imp____glewGetShaderiv'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x16a): undefined reference to `_imp____glewCreateShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x18e): undefined reference to `_imp____glewShaderSource'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x1b7): undefined reference to `_imp____glewCompileShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x1c9): undefined reference to `_imp____glewGetShaderiv'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x223): undefined reference to `_imp____glewCreateProgram'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x238): undefined reference to `_imp____glewAttachShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x253): undefined reference to `_imp____glewAttachShader'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x26e): undefined reference to `_imp____glewLinkProgram'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x282): undefined reference to `_imp____glewGetProgramiv'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x2e2): undefined reference to `_imp____glewGetAttribLocation'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x3d0): undefined reference to `glClearColor@16'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x3df): undefined reference to `glClear@4'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x3e7): undefined reference to `_imp____glewUseProgram'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x3fb): undefined reference to `_imp____glewEnableVertexAttribArray'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x424): undefined reference to `_imp____glewVertexAttribPointer'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x476): undefined reference to `glDrawArrays@12'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x47e): undefined reference to `_imp____glewDisableVertexAttribArray'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x492): undefined reference to `_imp__glutSwapBuffers@0'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x4c9): undefined reference to `_imp____glewDeleteProgram'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x519): undefined reference to `_imp__glutInitContextVersion@8'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x52a): undefined reference to `_imp__glutInitDisplayMode@4'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x543): undefined reference to `_imp__glutInitWindowSize@8'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x55c): undefined reference to `_imp__glewInit@0'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x572): undefined reference to `_imp__glewGetErrorString@4'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x5b4): undefined reference to `_imp__glutDisplayFunc@4'
...\tut01_intro\triangle.o:triangle.cpp:(.text+0x5be): undefined reference to `_imp__glutMainLoop@0' 


glut (freeglut is installed), glew is installed, GLM is installed now as well. What else needs to be installed?

For the record I have not edited the code at all. I literally just opened codeblocks, opened the .cpp file and ran it. No adjustments.

Can anyone confirm that any of the code from the above site works at all? I'm starting to think its actually not me now...

If anyone can make any of this code run, please advise what else is needed to be installed in codeblocks...
Last edited on
These are linker errors. It looks like the gl* libraries are not included in the link command.
But this isnt code I have setup, I just downloaded it, so it should work, shouldnt it? If not why have they got code as examples that doesnt work?
There is nothing wrong with the code, you aren't actually getting compiler errors. The code compiled to object files just fine, its linking that you are getting errors. You need to link to the libraries, in this case GLUT, GLEW and GL. To learn how to do that, either look at your compiler's documentation or use your favourite search engine.
Ok I have a lot of problem understanding this. Why do i need to link things which are referenced in the code?

In the olden days, you could just say "include<file>" and it would include the file no problem and any function in it would just work.

Why now have we gone backwards and have to not just code right, but tell the IDE where the files its loading are? It loads them fine, because it will autocomplete when you start typing them. So I have no idea what I have to do now to make this code work.

Why did someone decide it needed to be more complicated? In the old days you just coded the file and everything worked. Now you have to code it, mess around creating a "project", and then configure that project.... all in order to run a simple piece of code. Why? What is the reason for this?

Additionally if anyone can write a step by step list of what I have to actually do to make this code work in codeblocks, i'd appreciate it. Or recommend an IDE where you can just open a .cpp file and just run it, without this pointless complication of setting things up multiple times first. That'd be even better...
In the olden days, you could just say "include<file>" and it would include the file no problem and any function in it would just work.

Wrong. That does not make them "work".

The 'include' is a preprocessor directive. Preprocessor concatenates the included header files with our source code. Therefore the compiler can see declarations of types and functions and can compile the code. This happens with example code too; it does compile.

The output of compilation is one or more object files. The linker combines the object files and adds necessary implementations for functions (from libraries that it knows about) in order to produce the executable. Your previous projects probably did depend only the standard library, which the linker does know about by default.

You did give a link. That page had other links, like: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction
There is a section Makefile.

You do use some IDE. IDE either has a Makefile for the project, or something equivalent. IDE has thus some method to set it up. I don't use that IDE. Does it have a manual?
A while ago i put together a simple diagram of the C++ program build process. Even if your program is just a basic "Hello World", it still goes through these stages.

Diagram: http://imgur.com/ve23ptc
Last edited on
When you use an external library, there are two steps:

1. You need to #include the header files. This forward-declares everything used in that library and tells the compiler that these functions exist.
2. You need to Link to the libraries. This tells the linker where these functions are.

There are two ways to link: statically, or dynamically. Dynamic linking would use glut.dll/libglut.o and static linking would use a pre-compiled library (glut.lib for visual studio) and embed that code into your program.

To link, it depends on your setup. Are you using Visual Studio? GCC? MinGW? Let us know and we can tell you the steps needed. What did you download? They could be Header/DLL/O/LIB or sources.
Topic archived. No new replies allowed.