Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL SuperBible 5th edition set-up problems

Ok, so I've recently got the OpenGL SuperBible, and set up the directories on Code::Blocks to freeglut and GLTools.h but these #include statements will not work:

#include <GLtools.h>
#include <GLShaderManager.h>
#include <iostream>
#include <GL/glut.h>

To set the directories I went through "Settings" -> "Compiler and Debugger..." -> "Search Directories" tab and added this under the "Compiler" tab (there's also "Linker" and "resource Compiler" tabs):

C:\CodeBlocks\SB5\Src\GLTools\include

and

C:\CodeBlocks\SB5\freeglut-2.6.0\include

These are the errors I get:

C:\CodeBlocks\SB5\freeglut-2.6.0\include\GL\freeglut_std.h|60|warning: "NOMINMAX" redefined|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\mingw32\bits\os_defines.h|46|warning: this is the location of the previous definition|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|225|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|226|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|227|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|228|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|229|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|230|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|231|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|232|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|233|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|234|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|235|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|236|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|237|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|238|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|239|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|240|error: expected constructor, destructor, or type conversion before 'const'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|241|error: expected constructor, destructor, or type conversion before 'const'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|242|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|243|error: expected constructor, destructor, or type conversion before 'const'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|244|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|245|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|246|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|247|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|248|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|249|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|250|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|251|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|252|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|253|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|254|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|255|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|256|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|257|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|258|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|259|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|260|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|261|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|262|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|263|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|264|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|265|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|266|error: 'GLAPI' does not name a type|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|267|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|268|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|269|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|270|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|271|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|272|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|273|error: expected constructor, destructor, or type conversion before 'void'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\GL\glu.h|274|error: expected constructor, destructor, or type conversion before 'void'|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 2 warnings ===|

It seems as though there's some kind of conflict going on between the header files, although it's coming up with errors at glu.h when I didn't even include that.

Please help! and THANKS! =)

like image 287
Griffin Avatar asked Dec 09 '22 09:12

Griffin


1 Answers

I ran into the same problem as you did when trying to compile the Triangle example in the SuperBible (5th Edition). I also like to use Code::Blocks with MinGW. I have been doing some research, and I think I've managed to solve most issues.

The first thing to do is to correctly set up our environment. We start by configuring the libraries, freeglut and GLTools.

Before getting started, make sure you have the latest version of MinGW installed, with MSYS. Check that you added c:/MinGW/bin to the path variable. Also get a good file extractor like 7z, that can unpack .tar and .gz files.

I'm going to be perhaps too detailed, but these things were not obvious to me until I researched them.

To set up freeglut-2.6.0, the first thing we are going to do is compile the static library from the source:

  1. Go to freeglut's site and download the latest version (currently 2.6.0)

  2. Create a c:/libs directory, and unpack the freeglut package there. You should end up with a directory called c:/libs/freeglut-2.6.0.

  3. Run MSYS (c:/MinGW/msys/1.0/msys.bat). You should be in your home directory (denoted by ~ after your user and host in the top line).

  4. Type "cd /c/libs/freeglut-2.6.0/src" and press [enter]. The ~ will change to /c/libs/freeglut-2.6.0/src to denote your current working directory.

  5. Type "gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include" and press [enter]. What we are doing here is calling the gcc compiler, instructing it to use O2 optimization, set the flag FREEGLUT_STATIC, and compile every source file in this directory into .o object files, and to look for headers in "../include". That means to go down one level (to /c/libs/freeglut-2.6.0) and look for the /include directory there.

  6. Type "ar rcs libfreeglut32_static.a *.o" and press enter. To the best of my understanding, ar is the utility that compiles static libraries, archive files (.a). We're telling it to create "libfreeglut32_static.a" from every object file (.o) we just compiled in the previous step.

  7. Create a /lib directory in /c/libs/freeglut-2.6.0. Then cut the library we just created and paste it in this directory. The final location of the freeglut library should be "c:/libs/freeglut-2.6.0/lib/libfreeglut32_static.a".

Go to this site for further reference on compiling freeglut with MinGW.

Before getting GLTools working, make sure freeglut works.

  1. Open Code::Blocks, and close any open projects. Go to "Settings->Compiler and Debugger" and open the "Search Directory" tab. In Compiler, add the directory "c:/libs/freeglut-2.6.0/include/". In Linker, add "c:/libs/freeglut-2.6.0/lib/".

  2. Create an empty project. Add a test.cpp file. Add the single function int main (){return 0;}.

  3. It should build with no problem.

  4. Add #define FREEGLUT_STATIC and #include <GL/glut.h>.

  5. Try to compile. It will fail.

  6. Right click on the Project and go to "Build Options". Make sure you are editing the options for the whole project, not the Debug or Release targets. Move to the Linker tab, and add to Link libraries "freeglut32_static".

  7. Try to compile. It will fail again. The compiler will throw "undefined reference to" errors, listing a bunch of functions. Copy the name of the first one, and look it up in google. Go to the microsoft search result (first or second result). It will describe the function, and more importantly, tell you what library that function is in.

  8. The library will be called "something.lib". Go to Build Options again, and add "something" to the link libraries.

  9. Try compiling again. It will fail once more. If you bother to check, you'll see that the first "undefined reference" has changed however. Repeat 7 and 8 until it stops failing. The additional libraries you'll have to add to get freeglut working are "Winmm", "Gdi32" and "Opengl32".

  10. Compile once more. test.exe should be created. Yay!

Ok, now that we have freeglut working, it's time to break it again. We are first going to recreate the GLTools library, since the SB5 only distributes the .lib version.

  1. The SB5 download should have 4 main directories; freeglut, linux, visualstudio and src. Go into src. Copy the GLTools directory and paste it in "c:/libs".

  2. Create a lib directory within GLTools. There should now be three directories within it, /include, /src and /lib.

  3. Bring up MSYS again. Type "cd /c/libs/GLTools/src" and press [enter].

  4. Like we did with freeglut, we're going to grab all the tools' code and link it into a single library. Type "g++ -c *.cpp -I../include" and press [enter], followed by "ar rcs libGLTools.a *.o".

  5. Cut the output, libGLTools.a, into "c:/libs/GLTools/lib".

  6. We do the same for glew. Type "g++ -c glew.c -I../include" followed by [enter]. Then "ar rcs libglew.a glew.o".

  7. In Code::Blocks, go back to "Settings->Compiler and Debugger" and open the "Search Directory" tab. In Compiler, add the directory "c:/libs/GLTools/include/". In Linker, add "c:/libs/GLTools/lib/".

  8. I promised we'd break what we had done before, didn't I? Add to our test program #include <GLTools.h> and try to build the project. You should get a slew of "'GLAPI' does not name a type" errors. This is happening because, at some point (not sure where myself) GLAPI is not getting defined. Or it is getting defined wrong.

This is worth investigating, if you've never worked with libraries like this (I haven't, so it's a learning experience for me as well). When we include "glut.h", if you open the header file you'll see it is simply including "freeglut_std.h". So you open that file, and it has a bunch of preprocessor commands.

Run a search for GLAPI in the document, and we find out it doesn't define it here. It does, however, include <GL/gl.h> and <GL/glu.h>. So open gl.h and run a search there.

Bingo! We can see a few commands that play around with our GLAPI definition. This is a little confusing, but we're going to copy what the preprocessor normally does. So we have these lines in there:

/* GLAPI, part 1 (use WINGDIAPI, if defined) */
#if defined(__WIN32__) && defined(WINGDIAPI)
#  define GLAPI WINGDIAPI
#endif

/* GLAPI, part 2 */
#if !defined(GLAPI)
#  if defined(_MSC_VER)                        /* Microsoft Visual C++ */
#    define GLAPI __declspec(dllimport)
#  elif defined(__LCC__) && defined(__WIN32__) /* LCC-Win32 */
#    define GLAPI __stdcall
#  else                                        /* Others (e.g. MinGW, Cygwin, non-win32) */
#    define GLAPI extern
#  endif
#endif

We can search freeglut_std.h and find out WINGDIAPI is not defined. So we skip the first block. From the second block, we can see that the line we want is #define GLAPI extern.

What seems to be happening is, then, that one of the headers in GLTools is messing up this definition for us. There's little to do but try to figure out where the problem is.

GLTools doesn't say anything about GLAPI, but includes <windows.h> and <GL/glew.h> before <GL/gl.h>. glew.h defines GLAPI a couple of times, but because there's no identation in the code it's kind of confusing to tell if the code is being entered or not. What is clear, though, is that for our purposes GLAPI has to be defined as extern. So we're going to do a quick hack in our test.cpp and define GLAPI as extern right ahead of the command to include .

#include <GLTools.h>

#define FREEGLUT_STATIC
#define GLAPI extern
#include <GL/glut.h>

It's still going to be broken, but we should get an exiting NEW error! So we compile and....

Huh. It compiled with no errors. That shouldn't have happened. But then, we're not calling any of the functions in GLTools, so maybe it's not so strange. We'll press our luck and add the second include in triangle.ccp, #include <GLShaderManager.h>. And compile, and again no errors.

Ok, so we are almost ready to build the example. Now we start filling test.cpp with the contents of triangle.cpp, and test when it fails. I start by adding

GLBatch triangleBatch;
GLShaderManager shaderManager;

outside of main, and there we go, a couple of undefined references. That's more like it. So we open Build options (remember to specify the whole project, not the release or debug targets) and add GLTools to Link libraries. We do that, and now (as you should be expecting if you've been paying attention) we get a bunch of undefined references to glew functions.

Wee! So exciting. So now we add the glew library to the Build options. If you linked glew at the end, we learn something new. You have to remember that when linking libraries, those that have their own dependencies have to be linked ahead of the ones they depend on. It would be helpful if we knew what they all depend on ahead of time, so in the future draw a map or something.

So we want to link freeglut, that depends on winmm, gdi32 and opengl32. We also want GLTools, that depends on glew, and glew, that depends on opengl32. The order to link them in should be, then... freeglut, GLTools, glew, winmm, gdi32 and opengl32. Let's see if that works....

Ha! It compiled with no errors. Damn I'm good (not really :( ).

Ok, now that we set up the environment, I copied the rest of the Triangle.cpp file over, saved, build, and got no errors.

I've tested these steps in two computers, and they worked. I think the steps above should enable anyone with Code::Blocks and MinGW to build the tutorials in the OpenGL SuperBible, 5th Edition.

like image 145
Kian Avatar answered Dec 11 '22 23:12

Kian