在Windows上使用mingw32构建glew

3
这是一个重复的问题,我很抱歉,但我没有其他选择,因为我不能在答案上发表评论,他们也没有解决我的问题。以下是原始帖子:Building glew on windows with mingw
这是我的问题:
1)当我尝试@LightningIsMyName的答案时,我会收到此错误:
Makefile:1: *** missing seperator. Stop.
2)当我尝试@anon的答案时,我会收到此错误:
Makefile:1: Makefile:1: *** commands commence before first target. Stop.
人们说这个答案对他们有用,但它们对我没用。我很抱歉我不小心重复了一个问题,但由于评论的限制,我别无选择。希望你能帮忙。谢谢。
3个回答

9
我进行了许多搜索,以找到解决我的问题的答案。这花费了很多时间,所以我在此发布它,以帮助其他人。
要使GLEW与MinGW配合使用,您应该从GLEW网站下载源代码,并将
gcc.exe来自MinGW \ bin ar.exe来自MinGW32 \ mingw32 \ bin
放入GLEW的源文件夹中,并创建并运行一个.bat文件在该文件夹中,像这样:
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

您将在lib文件夹中获得.dll和.a文件。请将.dll文件放入system32文件夹中,并将.a文件放入MinGW lib文件夹中。

最后,如果您正在使用SFML,请在链接GLEW之前链接SFML库,并在最后链接OpenGL。如果更改链接顺序,则会出现链接器错误。

在创建窗口后不要忘记调用glewInit()函数。


3
你是一个传奇。完美地运作了。 - K_Finlay
为什么 mx 版本和非 mx 版本的大小相同?我应该将它们都链接起来吗? - Shambhav Gautam
@ShambhavGautam 很抱歉,我不再使用Windows和SFML了。我查看了我的存档,但找不到项目文件。 - Arda Kara
@ArdaKara 没问题,使用GLAD要容易得多,我只是下载了源代码并构建它,然后完成了,没有复杂的链接或动态混乱,建议每个人在使用MinGW时都使用GLAD。顺便说一下,你写的好像SFML是使用OpenGl的唯一库一样,这很奇怪。 - Shambhav Gautam
@ArdaKara你的回答提供了信息但没有给出原因,比如“把dll文件放在system32中”,没有解释为什么。你可以直接和exe文件放在一起。此外,你提到了GLEW网站,但没有连接它。你说要把gcc.exe放在源代码中,这既不必要也不好,正确的方法是将mingw_location \ bin添加到路径变量中或者给出gcc.exe的完整路径。请更新您的答案。想象一下有多少初学者由于这个而被误导。 - Shambhav Gautam
显示剩余2条评论

2
如果您无法按照那种方式使其工作,请尝试我的方法(适用于Borland编译器):
  1. download GLEW source code (not the binaries)

    • glew.c source code mine is ~900KB (I think GLEW 1.6)
    • glew.h header mine is ~900KB (I think GLEW 1.6)
  2. I use local project path to store GLEW

    I know it is redundant but I do not need to make changes after system changes like reinstall OS or new compiler version etc... but to get it work you need to do some changes inside glew.c:

    • change #include <glew.h> to #include "glew.h"

    if you want to use global path then add it to your compiler,copy the files there and leave the <> as are

  3. copy these 2 files to your project

    this is how to correctly include it into project just add this to your main source file (where your winmain or main function is):

    #include <windows.h> // optional windows
    #include <math.h>    // optional
    #define GLEW_STATIC  // this configure header and source of GLEW to compile correctly 
    #include "glew.c"    // header is included inside so no need to duplicate include
    // here are the OpenGL includes like: gl.h,glext.h,....
    
  4. Now it should work

    do not forget to call glewInit(); after OpenGL is initialized and before any extension is used ...


我尝试过这个,但它没有帮助我。但是我在网上找到了一个解决方案,并为MinGW构建了glew,但现在它给出了链接器错误。经过三天,我仍然无法使OpenGL运行 :( - Arda Kara
@Senhor,您是否遇到了OpenGL或GLEW的问题?因为在glewInit()之前未初始化OpenGL上下文,所以GLEW无法正常工作。因此,首先尝试创建基本的OpenGL应用程序而不使用GLEW(OpenGL 1.0),然后再添加GLEW...就像这样:http://stackoverflow.com/a/20679773/2521214 - Spektre
我不知道为什么我的OpenGL在没有GLEW的情况下无法工作,所以我安装了它。然后我解决了链接器错误,问题是连接顺序。现在它运行得很好。谢谢你的帮助。 - Arda Kara

2

请尝试以下命令:
1:gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c
2: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
3:ar cr lib/libglew32.a src/glew.o


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接