使用 MinGW、freeglut 和 glew 编译简单的静态 OpenGL 4.0 程序

2021-12-18 00:00:00 opengl winapi c++ glew freeglut

问题出在标题上,我会尽量在下面列出我已经尝试过的等等.

The problem is in the title, I'll try to list what I've already tried and so on below.

首先,根据我的理解,为了在 Windows 上使用 OpenGL 4.0,您必须扩展和/或绕过默认的 Windows 库,因为它只提供 OpenGL 1.1.

First off, in my understanding, in order to use OpenGL 4.0 on windows you must extend and or bypass the default windows library as it only ships OpenGL 1.1.

所以我们在 C:/MinGW/ 安装了 MinGW.接下来我通过下载 tarball 来自 项目站点.根据说明运行makefile来提取和编译,并添加少量--prefix./configure 命令.

So we have MinGW installed at C:/MinGW/. Next I setup FreeGLUT by downloading the tarball from the project site. Extract and compile by running the makefiles according to the instructions with a minor addition of --prefix to the ./configure command.

./configure --prefix=/c/Users/Owner/root/
make all
make install 

现在我在 /c/Users/Owner/root/lib//c/Users/Owner/root/include/ 等中有 freeglut.接下来是 GLEW,据我所知,我的问题孩子.

Now I have freeglut in /c/Users/Owner/root/lib/, /c/Users/Owner/root/include/ and so on. Next up is GLEW, my problem child as far as I can tell.

从项目站点(直接 1.7.0.zip 链接).编译有点复杂,我目前的配方来自堆栈溢出问题在 Windows 上构建 glew与 mingw".下面列出了一个缩写形式:

Download the source archive from the project site (direct 1.7.0.zip link). Compiling is a tad more complicated, my current recipe is derived from the stack overflow question " Building glew on windows with mingw ". An abbreviated form is listed below:

mkdir lib/
mkdir bin/
gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
gcc -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
ar cr lib/libglew32.a src/glew.o
gcc -DGLEW_NO_GLU -DGLEW_MX -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c
gcc -shared -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
ar cr lib/libglew32mx.a src/glew.mx.o

并且应该从 /path/to/glew-1.7.0/ 的根"运行.

and should be run from the "root" of /path/to/glew-1.7.0/.

现在库的设置完成"(假设没有错误......)编译我的简单程序是用这一行完成的.

Now with setup of libraries "done" (assuming no mistakes... ) compiling my simple program is done with this line.

${G++}  -DFREEGLUT_STATIC -DGLEW_STATIC -m32 main.cpp -o main.exe -lfreeglut_static -lopengl32 -lwinmm -lgdi32 -lglew32  -I ${ROOTPATH}/include -L ${ROOTPATH}/lib --static

现在稍微分解一下并解释为什么我有各种额外"参数,并向您展示我已经遇到并解决了哪些错误和问题.

Now to decompose this a bit and walk through why I have various "extra" arguments and to show you what errors and problems I've already run into and solved.

    使用
  1. -DFREEGLUT_STATIC-lfreeglut_static 代替普通的 -lfreeglut ,因为我们在这里需要静态构建.不这样做会导致与 freeglut 相关的链接器错误.
  2. -DGLEW_STATIC 也是出于同样的原因添加的.
  3. 添加
  4. -lwinmm 以修复链接器错误:freeglut_init.c:(.text+0x5d9): undefined reference to '_timeBeginPeriod@4'.
  5. 添加
  6. -lgdi32 以修复链接器错误:c:/Users/Owner/root//liblibfreeglut_static.a(freeglut_init.o):freeglut_init.c:(.text+0x58c): 对'_GetDeviceCaps@8'的未定义引用
  1. -DFREEGLUT_STATIC and -lfreeglut_static are used instead of the normal -lfreeglut as we want a static build here. Failure to do this gives linker errors relating to freeglut.
  2. -DGLEW_STATIC is added for the same reason.
  3. -lwinmm is added to fix the linker error: freeglut_init.c:(.text+0x5d9): undefined reference to '_timeBeginPeriod@4'.
  4. -lgdi32 is added to fix the linker error: c:/Users/Owner/root//liblibfreeglut_static.a(freeglut_init.o):freeglut_init.c:(.text+0x58c): undefined reference to '_GetDeviceCaps@8'

现在我被以下链接器错误难住了:

Now I'm stumped with the following linker error:

c:/Users/Owner/root//liblibglew32.a(glew.o):glew.c:(.text+0x83e8): undefined reference to `_glGetString@4'
c:/Users/Owner/root//liblibglew32.a(glew.o):glew.c:(.text+0xa1b2): undefined reference to `_glGetString@4'
c:/Users/Owner/root//liblibglew32.a(glew.o):glew.c:(.text+0xa290): undefined reference to `_glGetString@4'

产生此错误的最小测试用例 (main.cpp) 是.

The minimal test case that produces this error (main.cpp) is.

#include <GL/glew.h>
#include <GL/freeglut.h>

int main(int argc, char **argv) {
  glEnableVertexAttribArray(0);
}

想法?

推荐答案

尝试在最后一行添加 -lopengl32 以编译您的程序,看看是否有帮助.

Try adding -lopengl32 last on the line to compile your program and see if it helps.

相关文章