Click here to Skip to main content
15,848,724 members
Please Sign up or sign in to vote.
2.60/5 (3 votes)
I'm a student working with OpenGL in C++. I'm trying to get freeGLUT to work with MinGW. I'm asking because I've already tried a ton of sites, multiple times and there is no clear solution, at least to my knowledge. I've had this problem for months.

Although using Visual Studio would be the simplest environment to use, I'd prefer to use my own customized environment.

The issue I'm having is the linker. I'm having great difficulty understanding as a newer user how to use the linker.

My main goal is simply to compile any basic program, for example, this test one:
C++
#include <GL/glut.h>

    void displayMe(void)
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_POLYGON);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.5, 0.0, 0.0);
            glVertex3f(0.5, 0.5, 0.0);
            glVertex3f(0.0, 0.5, 0.0);
        glEnd();
        glFlush();
    }

    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE);
        glutInitWindowSize(300, 300);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("Hello world :D");
        glutDisplayFunc(displayMe);
        glutMainLoop();
        re

turn 0;
}
Here are my linker errors:
C++
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x25):
> undefined reference to `__imp___glutInitWithExit'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x4c):
> undefined reference to `__imp___glutCreateWindowWithExit'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x72):
> undefined reference to `__imp___glutCreateMenuWithExit'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x8e):
> undefined reference to `__imp_glClear'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x9c):
> undefined reference to `__imp_glBegin'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0xb1):
> undefined reference to `__imp_glVertex3f'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0xca):
> undefined reference to `__imp_glVertex3f'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0xe7):
> undefined reference to `__imp_glVertex3f'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x100):
> undefined reference to `__imp_glVertex3f'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x109):
> undefined reference to `__imp_glEnd'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x112):
> undefined reference to `__imp_glFlush'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x14b):
> undefined reference to `__imp_glutInitDisplayMode'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x15e):
> undefined reference to `__imp_glutInitWindowSize'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x171):
> undefined reference to `__imp_glutInitWindowPosition'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x18d):
> undefined reference to `__imp_glutDisplayFunc'
> C:\Users\Exhuman\AppData\Local\Temp\ccM8iHrk.o:main.cpp:(.text+0x196):
> undefined reference to `__imp_glutMainLoop' collect2.exe: error: ld
> returned 1 exit status


I'm including a download link to my custom working environment as I feel it would be too time consuming to set up, the components are MinGW, Notepad++ and NppExec programs can be compiled with "f9" if there are no errors, and similarly can be run with "ctrl f9" once compiled. I usually run the program using the .bat in the main directory.

http://www.filedropper.com/env_1

If anyone could help me out with this issue, I'd greatly appreciate it.

What I have tried:

This site: http://www.transmissionzero.co.uk/computing/using-glut-with-mingw/ was the closest I found, and I tried to contact the author for a solution and so far he has been unresponsive.

I also tried to look up MinGW and the linker, but the jargon is outwith my comprehension.
Posted
Updated 22-Apr-20 15:15pm
v2

I'm not a C++ dev, but found a StackOverflow Q&A[^] that should point you in the right direction.
 
Share this answer
 
Comments
Member 13013288 20-Feb-17 20:05pm    
Nope, crazily non-specific and unhelpful.
Graeme_Grant 20-Feb-17 20:06pm    
It is suggesting that you are missing a file in your sol'n... is the glut.cpp file included in the sol'n? [edit:] "undefined reference" is saying it can't find what is referenced in the glut.f header file. Not so "crazily non-specific and unhelpful".
Member 13013288 20-Feb-17 20:08pm    
I have no idea, I downloaded all the freeglut files and installed them where I had to install them and this is a linker issue, there is no glut.cpp to my knowledge, it's a dll. Installing my file would make this clearer
Graeme_Grant 20-Feb-17 20:10pm    
Maybe you need to check how the dll is being used. The error suggests that it is not being referenced/linked in. watch the verbose compiler/linker output.
Member 13013288 20-Feb-17 20:17pm    
it isn't. which is why i need help. it's not linked in at all. and everyone always gives really stupid non-answers.

All I'm asking is how to link freeglut so I can compile a file. I'm getting the output, I've searched for the error, it's a linker error, it's caused by freeglut not being linked to notepad++ mingw, these are all things I already know.

The thing I don't know is how to link/fix it.
I know that this response is over 3 years removed form the original posting, but I ran across this posting while researching and wanted to offer a response.

I've experienced this type of undefined reference before. It can happen when one is trying to link a program that is compiled in debug mode with a library built in release mode (or vice versa). Link with the correct library version and the problem should go away.

Roger
 
Share this answer
 
Here is the solution to your problem. I was experiencing the same problem as you when I first used FreeGLUT. First confirm that you have a OpenGL32 and FreeGLUT library. If you are using MinGW then these should be located in the lib directory with the names of either "libopengl32.a" and "libfreeglut.a"/"libglut32.a"(if you are using glut) or "opengl32.lib" and "freeglut.lib". The FreeGLUT library can have a different name depending on the generator but it will include the word "freeglut" in it. Now what you need to do is add those libraries to your project. If you dont know how to do that just add the following lines in your source code:

#pragma comment("lib", "*freeglut/glut library name*")
#pragma comment("lib", "opengl32")

And be very careful to add the GLUT/FreeGLUT library before the OpenGL library. Doing the reverse will again show those errors. Now you should not receive the undefined reference to __imp_gl* errors.

Now let's move to the glut errors i.e. __imp_glut* errors. Notice that there is "WithExit" suffix in each of them. When you read the GLUT's header (in FreeGLUT it is freeglut_std.h) then somewhere in the last, you will see some lines of comment. That comment contains the solution to this problem. And the solution is to define a macro:

#define GLUT_DISABLE_ATEXIT_HACK

I don't really know what is this exit thing and why does it needs to be disabled but after disabling it, the application will compile and you will have no errors or misfunctioning. Also add the winmm library after OpenGL by:

#pragma comment ("lib", "winmm")

And now you should have your application running like butter both on MinGW and Visual Studio.

And if you are using FreeGLUT then define an extra macro FREEGLUT_STATIC before including the FreeGLUT header.

Hope this helps.
 
Share this answer
 
Comments
VD-Flash 8-Sep-20 0:11am    
I know that this question is over 3 years old. You may have figured it out. But in case you haven't, go give it a try and also download the latest version of FreeGLUT binary release which is 3.2.1 from their official website. If you figured it out even then there are guys in this problem as I was once and I'd they read this answer then they will surely get some help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900