MinGW and OpenGL

I know this is an extremely broad question but I've been going nuts trying to get the right setup for MinGW for OpenGL.

My question is straightforward - what must I do to get MinGW to compile the code bellow under a Windows system?:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    ///////////////////////////////////////////////////////////////////////
    //
    // triangles.cpp
    //
    ///////////////////////////////////////////////////////////////////////
    #include <iostream>
    using namespace std;
    
    #include "vgl.h"
    #include "LoadShaders.h"
    
    enum VAO_IDs { Triangles, NumVAOs };
    enum Buffer_IDs { ArrayBuffer, NumBuffers };
    enum Attrib_IDs { vPosition = 0 };
    
    GLuint  VAOs[NumVAOs];
    GLuint  Buffers[NumBuffers];
    
    const GLuint  NumVertices = 6;
    
    //---------------------------------------------------------------------
    //
    // init
    //
    
    void 
    init(void)
    {
        glGenVertexArrays(NumVAOs, VAOs);
        glBindVertexArray(VAOs[Triangles]);
    
        GLfloat  vertices[NumVertices][2] = {
            { -0.90, -0.90 },  // Triangle 1
            {  0.85, -0.90 },
            { -0.90,  0.85 },
            {  0.90, -0.85 },  // Triangle 2
            {  0.90,  0.90 },
            { -0.85,  0.90 }
        };
    
        glGenBuffers(NumBuffers, Buffers);
        glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
                     vertices, GL_STATIC_DRAW);
    
        ShaderInfo  shaders[] = {
            { GL_VERTEX_SHADER, "triangles.vert" },
            { GL_FRAGMENT_SHADER, "triangles.frag" },
            { GL_NONE, NULL }
        };
    
        GLuint program = LoadShaders(shaders);
        glUseProgram(program);
    
        glVertexAttribPointer(vPosition, 2, GL_FLOAT,
                              GL_FALSE, 0, BUFFER_OFFSET(0));
        glEnableVertexAttribArray(vPosition);
    }
    
    //---------------------------------------------------------------------
    //
    // display
    //
    
    void 
    display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glBindVertexArray(VAOs[Triangles]);
        glDrawArrays(GL_TRIANGLES, 0, NumVertices);
    
        glFlush();
    }
    
    //---------------------------------------------------------------------
    //
    // main
    //
    
    int 
    main(int argc, char** argv)
    {
         glutInit(&argc, argv);
         glutInitDisplayMode(GLUT_RGBA);
         glutInitWindowSize(512, 512);
         glutInitContextVersion(4, 3);
         glutInitContextProfile(GLUT_CORE_PROFILE);
         glutCreateWindow(argv[0]);
    
         if (glewInit()) {
             cerr << "Unable to initialize GLEW ... exiting" << endl;
             exit(EXIT_FAILURE);
         }
    
         init();
    
         glutDisplayFunc(display);
    
         glutMainLoop();
    }


To avoid more code clogging I haven't posted the include headers but they can be viewed at the website: http://www.opengl-redbook.com/.

As for my attempts of setting it up myself I have:
downloaded Glut and extracted the folder to a file directory of: C:\freeglut\
downloaded Glew and extracted its include folder and lib folder to C:\freeglut\include\ and C:\freeglut\lib\ respectively
I followed this by using the following makefile:

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
    #OBJS specifies which files to compile as part of the project
    OBJS = triangles.cpp
    
    #OBJ_NAME specifies the name of our exectuable
    OBJ_NAME = triangles
    
    #CC specifies which compiler we're using
    CC = g++
    
    #INCLUDE_PATHS specifies the additional include paths we'll need
    INCLUDE_PATHS = -I"C:\freeglut\include
    
    #LIBRARY_PATHS specifies the additional library paths we'll need
    LIBRARY_PATHS = -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib"
    
    #COMPILER_FLAGS specifies the additional compilation options we're using
    # -w suppresses all warnings
    # -Wl,-subsystem,windows gets rid of the console window
    COMPILER_FLAGS = -w -Wl,-subsystem,windows
    
    #LINKER_FLAGS specifies the libraries we're linking against
    LINKER_FLAGS = -lfreeGLUT -lOpenGL32 -lglu32 -lglew32
    
    #This is the target that compiles our executable
    all : $(OBJS)
    	$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) 


Which to no surprise after hours of tinkering, adjusting, readjusting lead to a long fat list of cease and desist undefined references to various __glut functions.

Any MinGW alternate configurations, advice, or help would be greatly appreciated.
Last edited on
read about what a library is, what a compiler is, and what a linker does, and you'll probrably never have such an issue again.

Also you forget to include{ <sys/gl.h>, <sys/glu.h>, etc. }
for each of those libraries (code interfaces) you MUST either link statically
and the compiler and linker will allow for all functions in the lib to work.
OR under windows you must link dynamically, (using LoadLibrary->GetProcAddress).
Be careful that when you include <windows.h> that you define the WIN32_LEAN_AND_MEAN
macro so that <gl.h> doesnt conflict with <winbase.h>

Library setup can be a pain in the ass, ideally a good project should work out of the box with little effort.
Last edited on
I understand the basics of a library compiler and linker. Compilers turn source into object files then the linker links the object files. I know a library consists of headers with source that are included into user's source for extended programming capabilities.

As for sys.gl.h and sys/glu.h I do not have those files. I do have gl/gl.h and gl/glu.h which have already been included through the #include directives of the header files of the source of vgl.h and loadshaders.h headers which source is listed on the opengl-redbook.com website which I listed.

As for including windows I thought I didn't have to do that since I have to use glew for this project anyways? I don't understand why when setting up OpenGL I have to patch the same code extensively using the WinAPI I thought this was an independent library for rendering...
UPDATE:
After setting up Visual Studio with glut and glew to run the code below I was also able to get MinGW to set up the same code below as well. The big thing is that I'm using the freeglut directories of VS rather than MinGW for both as the MinGW lib folder contains .a files which seem to be unrecognized by MinGW on windows.

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
34
35
36
37
38
39
40
41
42
43
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <cstdio>
#include <iostream>

using namespace std;

void changeViewport(int w, int h) {
	glViewport(0, 0, w, h);
}

void render() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glutSwapBuffers();
}

int main(int argc, char * argv[])
{
	// Initialize GLUT
	glutInit(&argc, argv);

	// Set up some memory buffers for our display
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	// Set the window size
	glutInitWindowSize(800, 600);
	// Create the window with the title "Hello, GL"
	glutCreateWindow("Hello GL");
	// Bind the two functions (above) to respond when necessary
	glutReshapeFunc(changeViewport);
	glutDisplayFunc(render);

	// Very important! This initializes the entry points in the OpenGL driver so we can
	// call the function in the API.
	GLenum err = glewInit();
	if (GLEW_OK != err) {
		fprintf(stderr, "GLEW error");
		return 1;
	}

	// Start up a loop that runs in the background (you never see it)
	glutMainLoop();
	return 0;
}


Now with visual studio I didn't have to use #include <cstdio>, I'm assuming that has something to do with the windows directory being in the include path perhaps?

Going back to the original code I get some new errors for both VS and MinGW.

Under VS I get this something along the lines of an error with freeglut and static linking.

Under MinGW I get this:
1>c:\freeglut\include\gl\freeglut_std.h(68): fatal error C1189: #error : Static linking is not supported with this build. Please remove the FREEGLUT_STATIC preprocessor directive, or download the source code from http://freeglut.sf.net/ and build against that.

So I've decided to ditch that code in exchange for the more simple one for now in diagnosing my problem.

When I try MinGW on the new source I get a blurb of this:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
In file included from C:\freeglut\include/GL/freeglut.h:17:0,
                 from vgl.h:37,
                 from triangles.cpp:9:
C:\freeglut\include/GL/freeglut_std.h:605:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_std.h:606:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_std.h:607:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_std.h:614:33: error: expected initializer before
 'glutInit_ATEXIT_HACK'
C:\freeglut\include/GL/freeglut_std.h:616:32: error: expected initializer before
 'glutCreateWindow_ATEXIT_HACK'
In file included from C:\freeglut\include/GL/freeglut.h:17:0,
                 from vgl.h:37,
                 from triangles.cpp:9:
C:\freeglut\include/GL/freeglut_std.h:618:32: error: expected initializer before
 'glutCreateMenu_ATEXIT_HACK'
In file included from C:\freeglut\include/GL/freeglut.h:18:0,
                 from vgl.h:37,
                 from triangles.cpp:9:
				 
C:\freeglut\include/GL/freeglut_ext.h:186:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:187:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:188:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:216:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:217:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:218:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:223:1: error: 'FGAPI' does not name a type

C:\freeglut\include/GL/freeglut_ext.h:224:1: error: 'FGAPI' does not name a type

triangles.cpp: In function 'int main(int, char**)':
triangles.cpp:84:26: error: 'glutInit_ATEXIT_HACK' was not declared in this scop
e
triangles.cpp:85:35: error: 'glutInitDisplayMode' was not declared in this scope

triangles.cpp:86:33: error: 'glutInitWindowSize' was not declared in this scope
triangles.cpp:87:33: error: 'glutInitContextVersion' was not declared in this sc
ope
triangles.cpp:88:46: error: 'glutInitContextProfile' was not declared in this sc
ope
triangles.cpp:89:30: error: 'glutCreateWindow_ATEXIT_HACK' was not declared in t
his scope
triangles.cpp:98:29: error: 'glutDisplayFunc' was not declared in this scope
triangles.cpp:100:19: error: 'glutMainLoop' was not declared in this scope
Makefile:26: recipe for target 'all' failed
mingw32-make: *** [all] Error 1


The commong line of error being this:
fatal error C1189: #error : Static linking is not supported with this build. Please remove the FREEGLUT_STATIC preprocessor directive, or download the source code from http://freeglut.sf.net/ and build against that.

Could someone shed some light about how to fix this? It seems like just blatantly removing #define FREEGLUT_STATIC doesn't fix the problem.
Last edited on
Build freeglut library yourself from source code with the same compiler as the one you use to build your final executable.
I just did so using VS since I couldn't do it with MinGW. I've finally gotten some compliance with OpenGL through the GLFW windowing library but now I just need to resolve this last error:
1
2
3
g++ triangles.cpp vgl.h LoadShaders.h -IC:\freeglut-2.8.1\include -IC:\glew-1.9.
0\include -LC:\freeglut-2.8.1\lib\x86 -LC:\glew-1.9.0\lib -w -Wl,-subsystem,wind
ows -lOpenGL32 -lfreeglut -lglu32 -lglew32 -o triangles

1
2
3
4
5
6
7
8
9
10
11
12
13
C:\Users\Alex\AppData\Local\Temp\ccYXpbHD.o:triangles.cpp:(.text+0x1c): undefine
d reference to `__glutInitWithExit'
C:\Users\Alex\AppData\Local\Temp\ccYXpbHD.o:triangles.cpp:(.text+0x37): undefine
d reference to `__glutCreateWindowWithExit'
C:\Users\Alex\AppData\Local\Temp\ccYXpbHD.o:triangles.cpp:(.text+0x52): undefine
d reference to `__glutCreateMenuWithExit'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\A
lex\AppData\Local\Temp\ccYXpbHD.o: bad reloc address 0x0 in section `.ctors'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Makefile:26: recipe for target 'all' failed
mingw32-make: *** [all] Error 1
You said you used VS to compile the library, but the errors you posted comes from g++ compiler. Use VS to compile all the libraries and your final executable and you must use the same settings for each.
Topic archived. No new replies allowed.