在windows下用Opengl和Glut编译C++程序

问题描述 投票:0回答:1

我试图将代码块中的项目转换为在 makefile 中进行编译,但我在链接 opengl 库时遇到问题。

我已经尝试了好几天了,但还没能让它发挥作用。

在我的项目目录中,我具有以下结构:

  • 包括/
    • GL 和 GLFW 的所有标题
  • lib/
    • libfreeglut32.a
    • libglfw3.a
    • libglfw3dll.a
    • libglu32.a
    • libopengl32.a
  • src/
    • 主.cpp
    • 其他cpp的

我的代码块项目:

<?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>

生成文件:

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

错误:

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
c++ opengl linker
1个回答
0
投票

可能是因为gcc从左到右搜索库。如果它发现它们没有被使用,它就会丢弃它们。可以肯定的是,重新编译

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
代替。 (始终将库作为编译命令中的最后一个参数)

如果不起作用,我相信这组 OpenGL 函数来自 OpenGL 1.1 。这些函数已经过时,并且可能不包含在您使用的任何版本的 GLUT 中。 (虽然我认为 FreeGLUT 应该能够支持它们?)我强烈建议不要使用它们,除非您的目标是非常早期的 OpenGL 系统。如果您有兴趣,OpenGL 3.3 是 OGL 开发人员的标准入口点 https://learnopengl.com/

© www.soinside.com 2019 - 2024. All rights reserved.