0

I was trying to convert a project I have in codeblocks to compile in the makefile, but I'm having problems linking the opengl libraries.

I'm trying this for days and haven't been able to get it to work yet.

In my project directory I have the following structure:

  • include/
    • All headers of GL and GLFW
  • lib/
    • libfreeglut32.a
    • libglfw3.a
    • libglfw3dll.a
    • libglu32.a
    • libopengl32.a
  • src/
    • main.cpp
    • others cpp's

My codeblocks project:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
    <FileVersion major="1" minor="6" />
    <Project>
        <Option title="Canvas 2D" />
        <Option pch_mode="2" />
        <Option compiler="gcc" />
        <Build>
            <Target title="Debug">
                <Option output="../__bin/Debug/canvas2d" prefix_auto="1" extension_auto="1" />
                <Option working_dir="../" />
                <Option object_output="../__obj/Debug/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-g" />
                </Compiler>
            </Target>
            <Target title="Release">
                <Option output="../__bin/Release/canvas2d" prefix_auto="1" extension_auto="1" />
                <Option working_dir="../" />
                <Option object_output="../__obj/Release/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-O2 -Wall" />
                </Compiler>
                <Linker>
                    <Add option="-s" />
                </Linker>
            </Target>
        </Build>
        <Compiler>
            <Add option="-Wall" />
            <Add option="-fexceptions" />
            <Add option="-std=c++11" />
            <Add directory="./include" />
        </Compiler>
        <Linker>
            <Add library="./lib/libfreeglut32.a" />
            <Add library="./lib/libopengl32.a"/>
            <Add library="./lib/libglu32.a"/>
        </Linker>
        <Unit filename="src/Bola.h" />
        <Unit filename="src/Botao.h" />
        <Unit filename="src/Relogio.cpp" />
        <Unit filename="src/Relogio.h" />
        <Unit filename="src/Vector2.h" />
        <Unit filename="src/gl_canvas2d.cpp" />
        <Unit filename="src/gl_canvas2d.h" />
        <Unit filename="src/main.cpp" />

    </Project>
</CodeBlocks_project_file>

Makefile:

cpp_source_files = $(shell find -name *.cpp)


all: 
    g++ -Wall -fexceptions -std=c++11 -L lib -l freeglut32 -l glu32 -l opengl32 -l glfw3 -l glfw3dll -I./include $(cpp_source_files) -o run.exe

Error:

g++ -Wall -fexceptions -std=c++11 -L lib -l freeglut32 -l glu32 -l opengl32 -l glfw3 -l glfw3dll -I./include ./src/gl_canvas2d.cpp ./src/main.cpp ./src/Relogio.cpp -o run.exe

./src/main.cpp: In function 'int main()':
./src/main.cpp:164:52: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    bt = new Botao(200, 400, 140, 50, "Sou um botao");
                                                    ^
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7c): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x93): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9b): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xb0): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xc7): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xcf): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xe4): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xfb): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x112): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x11a): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x130): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x147): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x15e): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x175): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x18c): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x194): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x1aa): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x1c1): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x1d8): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x1ef): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x206): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x20e): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x224): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x23b): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x252): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x269): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x280): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x288): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x29e): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x2e0): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x2ee): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x304): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x346): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x354): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x3c4): undefined reference to `glRasterPos2i@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x3e5): undefined reference to `_imp__glutBitmapCharacter@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x419): undefined reference to `glClearColor@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x445): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x493): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x4aa): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x4d3): undefined reference to `glBegin@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x521): undefined reference to `glVertex2d@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x538): undefined reference to `glEnd@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x54e): undefined reference to `glMatrixMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x556): undefined reference to `glLoadIdentity@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x570): undefined reference to `glTranslated@24'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x588): undefined reference to `glMatrixMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x590): undefined reference to `glLoadIdentity@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x5aa): undefined reference to `glTranslated@24'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x5d1): undefined reference to `glColor3d@24'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x5f7): undefined reference to `glColor3fv@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x628): undefined reference to `glColor4d@32'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7b9): undefined reference to `glViewport@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7c8): undefined reference to `glMatrixMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7d0): undefined reference to `glLoadIdentity@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7f0): undefined reference to `gluOrtho2D@32'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x7ff): undefined reference to `glMatrixMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x807): undefined reference to `glLoadIdentity@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x82c): undefined reference to `glClearColor@16'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x843): undefined reference to `glPolygonMode@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x85b): undefined reference to `glClear@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x86a): undefined reference to `glMatrixMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x872): undefined reference to `glLoadIdentity@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x87c): undefined reference to `glFlush@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x881): undefined reference to `_imp__glutSwapBuffers@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x8cd): undefined reference to `_imp__glutSetOption@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x8de): undefined reference to `_imp__glutInitDisplayMode@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x8f5): undefined reference to `_imp__glutInitWindowSize@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x90e): undefined reference to `_imp__glutInitWindowPosition@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x932): undefined reference to `_imp__glutReshapeFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x943): undefined reference to `_imp__glutDisplayFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x954): undefined reference to `_imp__glutKeyboardFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x965): undefined reference to `_imp__glutKeyboardUpFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x976): undefined reference to `_imp__glutSpecialUpFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x987): undefined reference to `_imp__glutSpecialFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x998): undefined reference to `_imp__glutIdleFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9a9): undefined reference to `_imp__glutMouseFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9ba): undefined reference to `_imp__glutPassiveMotionFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9cb): undefined reference to `_imp__glutMotionFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9dc): undefined reference to `_imp__glutMouseWheelFunc@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0x9ed): undefined reference to `glGetString@4'
C:\Users\ADMINI~1\AppData\Local\Temp\ccL56CtJ.o:gl_canvas2d.cpp:(.text+0xa0f): undefined reference to `_imp__glutMainLoop@0'
C:\Users\ADMINI~1\AppData\Local\Temp\ccljMOAH.o:main.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:\Users\ADMINI~1\AppData\Local\Temp\ccljMOAH.o:main.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccljMOAH.o:main.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccSmVNUV.o:Relogio.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:\Users\ADMINI~1\AppData\Local\Temp\ccSmVNUV.o:Relogio.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\ADMINI~1\AppData\Local\Temp\ccSmVNUV.o:Relogio.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit@8'
collect2.exe: error: ld returned 1 exit status
make: *** [all] Error 1
2
  • Do not tag C for questions that do not involve C. Commented Mar 28, 2024 at 12:40
  • One problem (but probably not the main problem) is that link against the static and the dynamic version of glfw (glfw3 vs glfw3dll). You need to pick one. Since you are using glut anyway, there is also the question if you need glfw at all. Commented Mar 28, 2024 at 13:05

1 Answer 1

0

It may be because gcc searches libraries from left to right. If it sees that they are not in use, it will discard them. To be sure, recompile with g++ -Wall -fexceptions -std=c++11 -I./include ./src/gl_canvas2d.cpp ./src/main.cpp ./src/Relogio.cpp -L lib -l freeglut32 -l glu32 -l opengl32 -l glfw3 -l glfw3dll -o run.exe instead. (Always make libraries the last args in your compilation command)

In the event that doesn't work, that set of OpenGL functions I believe are from OpenGL 1.1 . These functions are outdated and may not be included with whatever version of GLUT you are using. (Although I thought FreeGLUT should be able to support them?) I highly recommend to not use them AT ALL unless you are targeting very early OpenGL systems. OpenGL 3.3 is the standard entry point for OGL devs if you are interested https://learnopengl.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.