带有Eclipse CDT + MinGW + GLEW + GLFW的OpenGL:未定义的引用 [英] OpenGL with Eclipse CDT + MinGW + GLEW + GLFW: Undefined References

查看:102
本文介绍了带有Eclipse CDT + MinGW + GLEW + GLFW的OpenGL:未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:在此期间,我已经弄清楚了,并在下面写了详细的答案.

I have in the meantime figured this out and written a detailed answer below.

我刚刚尝试从Win7的Express版本的MSVC 10切换到Eclipse CDT,并且在配置时遇到了以下简单的OpenGL代码(在Visual Studio中可以正常工作)的问题:

I just tried switching from the Express version of MSVC 10 to Eclipse CDT on Win7, and while configuring I encountered a problem with the following simple OpenGL code (which works fine in Visual Studio):

#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
    GLFWwindow* w;

    if (!glfwInit())
        return -1;

    w = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!w)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(w);

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
    {
        return -1;
    }

    while (!glfwWindowShouldClose(w))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwSwapBuffers(w);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

在Visual Studio中,我包括GLFW和GLEW的库路径,并按顺序链接(除了VS内置的东西之外)opengl32.lib,glew32s.lib,glfw3.lib.

In Visual Studio, I include the library paths for GLFW and GLEW, and link (in addition to the stuff that VS does built-in) opengl32.lib, glew32s.lib, glfw3.lib, in that order.

现在,如果我在Eclipse CDT中执行相同的操作,则无法使其正常工作.发生以下错误:

Now if I do the same in Eclipse CDT, I can't get it to work. The following errors occur:

Info: Internal Builder is used for build
g++ "-LD:\\lib\\cpp\\glfw-3.0.1.bin.WIN32\\lib-mingw" "-LD:\\lib\\cpp\\glew-1.10.0binaries\\lib\\Release\\Win32" -o glfwcheck.exe main.o -lopengl32 -lglew32s -lglfw3 
Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_2]+0x4): undefined reference to `_imp__wglGetProcAddress@4'
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_3]+0x4): undefined reference to `_imp__wglGetProcAddress@4'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj): bad reloc address 0x4 in section `.text[__glewInit_GL_VERSION_1_3]'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

当然,我尝试更改三个库的顺序,但这只会使情况更糟. (顺便说一句:我觉得很奇怪,我无法重现以上错误,因此当我将库重新排列为初始顺序时,它们是唯一的错误.我停留在20多个错误,直到删除所有库为止. ,构建并再次添加它们.)

Of course I tried changing the order of the three libraries, but that only mad it worse. (By the way: I think it's strange that I can't reproduce these errors above such that they're the only ones when I re-arrange the libs to the initial order. I stay at 20+ errors until I delete ALL the libs, build, and add them again.)

摸索并浏览论坛中的帖子后,我发现GLEW二进制文件可能存在问题,因此我自己使用MinGW对其进行了编译.仅这次,我得到了所有未找到"的错误.我不知道如何与自己编译的GLEW静态链接,因为现在不再有glew32s.lib,只有libglew32.a和libglew32mx.a.动态链接glew32并将dll放入我的项目文件夹也不起作用.

After fumbling around and looking through forum posts, I figured that maybe it's a problem with the GLEW binaries, and compiled them on my own with MinGW. Only this time, I get all the 'not found' errors. I don't know how I can link statically with my self-compiled GLEW, since there's now no glew32s.lib any longer, but only libglew32.a and libglew32mx.a. Linking dynamically with glew32 and putting the dll into my project folder didn't work either.

我觉得我在Eclipse中做错了什么,或者只是忘了一些其他的库(尽管我曾经尝试将VS使用的所有库都放在那里,并且仍然做完全一样的事情).

I feel like I'm doing something very wrong here in Eclipse, or just forget some additional libraries (although I once tried putting all the ones VS uses in there as well and it still did the exact same thing).

你能帮帮我吗? :)否则,我认为我必须呆在VS上,或者切换到Linux + make.

Can you help me out? :) Otherwise I think I'd have to stay with VS, or switch to Linux + make.

推荐答案

我已经弄清楚了,对于任何遇到此问题的人,我都会尽力弄清楚我到底做了什么,最终能够使用该设置创建OpenGL项目 MinGW + GLEW + GLFW .就我而言,我将Eclipse CDT用作IDE,但是我将写下生成的g++命令行,以便可以轻松地适应其他IDE.

I figured it out, and for anyone ever encountering the issue I will try and make clear what exactly I did to finally be able to create OpenGL projects with the setup MinGW + GLEW + GLFW. In my case, I used Eclipse CDT as IDE, but I'll write down the resulting g++ command line so it should be easy to adapt to other IDE's.

  1. 我将假设 MinGW

  1. I'll suppose MinGW and MSYS (can be chosen to be installed from within the MinGW GUI; thus no need to download separately) are installed.

下载 GLFW 并将其解压缩到您选择的外部库文件夹中(在我的情况下,是D:\external\cpp,因此可能类似于D:\external\cpp\glfw,在这里我将glfw-3.0.3.bin.WIN32文件夹重命名为glfw).

Download GLFW and unzip it in your external libraries folder of choice (in my case, this is D:\external\cpp, so it would be something along the lines of D:\external\cpp\glfw, where I renamed the glfw-3.0.3.bin.WIN32 folder to simply glfw).

GLEW 下载为zip文件夹解压缩它,在我的情况下,它位于D:\external\cpp\glew中.现在启动 MSYS cdglew文件夹并调用make all.

Download the GLEW source as a zip folder an unzip it, in my case it's in D:\external\cpp\glew. Now start MSYS, cd to the glew folder and invoke make all.

第3步应该已经在文件夹glew\lib中创建了文件libglew32.aglew32.dll.现在,右键单击Eclipse CDT C ++项目,转到属性-C/C ++常规-路径和符号.在包含标签中,将路径添加到GLFW和GLEW的include文件夹.同样,对我来说,这是D:\external\cpp\glew\include,与GLFW类似.在库路径中,对文件夹lib(GLEW)和lib-mingw(GLFW)进行相同的操作.

Step 3 should have created (among others) the files libglew32.a and glew32.dll inside the folder glew\lib. Now right click your Eclipse CDT C++ project, go to Properties - C/C++ General - Paths and Symbols. In the Includes tab, add the paths to the include folders of GLFW and GLEW. Again, for me this is D:\external\cpp\glew\include and analogous for GLFW. In Library Paths, do the same for the folders lib (GLEW) and lib-mingw (GLFW).

现在,我们必须添加要与我们的项目链接的库.如果希望动态链接GLEW,请确保在可执行文件所在的文件夹中包含glew32.dll.在Eclipse CDT中,通常是项目结构中的Debug(或Release)文件夹.在我们之前打开的选项窗口的 Libraries 标签中,添加(顺序很重要!)glfw3glew32opengl32glu32gdi32.现在,构建项目应该可以正常工作.如果要与GLEW静态链接,请添加相同的库,但glew32除外.而是在项目属性中转到 C/C ++构建-设置,然后在工具设置-MinGW C ++链接器-其他选项卡中,将libglew32.a的路径添加到其他对象字段.就我而言,这是D:\external\cpp\glew\lib\libglew32.a.现在,为了使静态链接起作用,您必须在#include <GL/glew.h>上方添加#define GLEW_STATIC或使用预处理程序命令-DGLEW_STATIC. GLEW主页上说,还可以将glew.cglew.h文件包含到您的项目中以进行静态链接,但是在某种程度上,这对我来说并没有真正的作用.

Now we have to add the libraries we want our project to be linked with. If you wish to link with GLEW dynamically, make sure to include the glew32.dll in the folder where your executable will be. In Eclipse CDT, that's usually the Debug (or Release) folder in your project structure. In the Libraries tab in the options window we opened before, add (the order is important!) glfw3, glew32, opengl32, glu32, gdi32. Now building the project should work hopefully. In case you want to link statically with GLEW, add the same libraries with the exception of glew32. Instead, in the project properties go to C/C++ Build - Settings and in the Tool Settings - MinGW C++ Linker - Miscellaneous tab add the path to libglew32.a to the Other objects field. In my case, this is D:\external\cpp\glew\lib\libglew32.a. Now in order for the static linking to work, you have to either add #define GLEW_STATIC above #include <GL/glew.h> or use the preprocessor command -DGLEW_STATIC. The GLEW homepage says that it's also possible to include the glew.c and glew.h files into your project in order to link statically, but somehow this didn't really work out for me.

这些步骤对我有用,并且它们产生了类似于以下内容的命令行(我只有一个名为main.cpp的文件,并且使用与GLEW的静态链接),如果您想弄清楚这件事,这可能会很有用.没有Eclipse CDT.

These steps worked for me, and they produced command lines similar to the following (I have only one file named main.cpp and used static linking with GLEW), which could be useful if you're trying to figure this matter out without Eclipse CDT.

g++ -ID:\external\cpp\glew\include -ID:\external\cpp\glfw\include -c -o main.o main.cpp
g++ -LD:\external\cpp\glew\lib -LD:\external\cpp\glfw\lib-mingw -o minimalexample.exe main.o D:\external\cpp\glew\lib\libglew32.a -lglfw3 -lopengl32 -lglu32 -lgdi32

在动态链接的情况下,只需在第二行中删除包含libglew32.a的部分,然后在-lglfw3-lopengl32之间添加-lglew32.作为一个小示例源文件,您可以只使用上述问题中的代码.

In the dynamic linking case, simply remove the part containing libglew32.a in the second line, and add -lglew32 between -lglfw3 and -lopengl32. As a little example source file, you could just use the code in my above question.

我希望我能为所有人提供帮助,因为我确定我在解决数十个未解析符号的错误消息与其他各种问题之间遇到了很多麻烦:-)

I hope I can help anyone with this, as I sure saw me having a lot of trouble figuring this out between tens of error messages of unresolved symbols and various other problems :-)

更新:几天前,我试图再次进行遍历,并遇到了适用于Windows的预编译GLFW二进制文件的问题(我现在使用的是Win8.1).但是,您可以将 CMake mingw32-make结合使用,以自行进行编译.另外,GLEW似乎不再有更新,因此我改用 glad .也可以使用 MinGW-w64 将库和最终项目编译为64-位应用程序.

Update: I tried to go over this again some days ago and ran into problems with the pre-compiled GLFW binaries for Windows (I'm now using Win8.1). But you can just use CMake in combination with mingw32-make to compile it on your own. Also, GLEW seems to not be getting updates anymore, so I switched to glad instead. It's also possible to use MinGW-w64 to compile the libraries and your final project as 64-bit application.

这篇关于带有Eclipse CDT + MinGW + GLEW + GLFW的OpenGL:未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆