简体   繁体   English

将GLEW与MinGW64链接-对'__imp_glewExperimental'和'__imp___glewGenVertexArrays'的未定义引用

[英]linking GLEW with MinGW64 - undefined reference to `__imp_glewExperimental' and `__imp___glewGenVertexArrays'

undefined reference to '[various things from GLEW library]' 未定义对“ [GLEW库中的各种内容]”的引用

This is my CMakeLists.txt: 这是我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.7)
project(newerOpenGLTest)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(newerOpenGLTest ${SOURCE_FILES})

find_package(OpenGL)

# tell it where to look for glew
set(CMAKE_PREFIX_PATH "F:/glew-2.0.0")
set(CMAKE_LIBRARY_PATH "F:/glew-2.0.0/lib/Release/x64")

find_package(GLEW REQUIRED)

if (GLEW_FOUND)
    message(${GLEW_INCLUDE_DIRS})
    message(${GLEW_LIBRARIES})

    # Detect and add SFML
    set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
    #Find any version 2.X of SFML
    #See the FindSFML.cmake file for additional details and instructions
    find_package(SFML 2 REQUIRED graphics window system)

    if(SFML_FOUND)
        include_directories(
                ${GLEW_INCLUDE_DIRS}
                ${SFML_INCLUDE_DIR}
        )
        target_link_libraries(
                newerOpenGLTest
                ${GLEW_LIBRARIES}
                ${SFML_LIBRARIES}
                ${SFML_DEPENDENCIES}
                ${OPENGL_gl_LIBRARY}
        )
    endif()

endif()

This is the output from Cmake: 这是Cmake的输出:

F:/glew-2.0.0/include
F:/glew-2.0.0/lib/Release/x64/glew32.lib
-- Found SFML 2.4.2 in F:/SFML-2.4.2 for GCC 6.1.0 MinGW (SEH) - 64-bit/include
-- Configuring done
-- Generating done
-- Build files have been written to: F:/Users/Doug/CLionProjects/newerOpenGLTest/cmake-build-debug

Everything looks fine to me there. 那里对我来说一切都很好。 It finds GLEW and finds locations for the include files and libraries. 它找到GLEW并找到包含文件和库的位置。

Here is my main.cpp: 这是我的main.cpp:

#include <GL/glew.h>

#include <SFML/Window.hpp>

int main() {
    sf::Window window(sf::VideoMode(800, 600), "openGL");
    window.setVerticalSyncEnabled(true);
    window.setActive(true);

    // Initialize GLEW
    glewExperimental = true;
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    bool running = true;
    while (running) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                running = false;
            }
        }
    }
    return 0;
}

Here is the output from linking: 这是链接的输出:

Linking CXX executable newerOpenGLTest.exe
CMakeFiles\newerOpenGLTest.dir\build.make:100: recipe for target 'newerOpenGLTest.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/newerOpenGLTest.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/newerOpenGLTest.dir/rule' failed
Makefile:117: recipe for target 'newerOpenGLTest' failed
CMakeFiles\newerOpenGLTest.dir/objects.a(main.cpp.obj): In function `main':
main.cpp:xx: undefined reference to `__imp_glewExperimental'
main.cpp:xx: undefined reference to `__imp___glewGenVertexArrays'
main.cpp:xx: undefined reference to `__imp___glewBindVertexArray'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [newerOpenGLTest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/newerOpenGLTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/newerOpenGLTest.dir/rule] Error 2
mingw32-make.exe: *** [newerOpenGLTest] Error 2

Undefined reference to 3 GLEW elements. 未定义引用3个GLEW元素。

Notice that it does not say undefined reference to '__imp_glewInit' 请注意,它没有说undefined reference to '__imp_glewInit'

So it is linking some of GLEW, but not everything. 因此,它正在链接某些GLEW,但不是所有链接。

I run glewinfo.exe and the output has glGenVertexArrays: OK I think that means my driver supports that function. 我运行glewinfo.exe ,输出结果为glGenVertexArrays: OK我认为这意味着我的驱动程序支持该功能。

Apparently, the binaries from the GLEW website only work for Visual C++. 显然,GLEW网站上的二进制文件仅适用于Visual C ++。

If you want to use it with MinGW, you have to compile your own. 如果要与MinGW一起使用,则必须自己编译。

Instructions copied from here: 从此处复制的说明:

https://www.opengl.org/discussion_boards/showthread.php/198730-How-do-I-build-GLEW-for-mingw-w64-using-cmake-on-Windows https://www.opengl.org/discussion_boards/showthread.php/198730-How-do-I-build-GLEW-for-mingw-w64-using-cmake-on-Windows

1) Download the GLEW Source files. 1)下载GLEW源文件。 DO NOT download the 32/64bit Window Binaries unless you're using Visual Studio, and if you are, you wouldn't be reading this. 除非您使用的是Visual Studio,否则请不要下载32/64位窗口二进制文件,如果您使用的是Visual Studio,则不会阅读此文件。

2) Download, install, and update msys2. 2)下载,安装和更新msys2。 Unfortunately, I was unable to find a way to compile GLEW (for mingw-w64) without it, but trust me, it's painless, and once you're done, you don't need msys2, we're only using it to compile the GLEW lib files. 不幸的是,没有它我无法找到一种编译GLEW(针对mingw-w64)的方法,但是请相信我,这很轻松,一旦完成,您就不需要msys2了,我们只使用它来进行编译GLEW lib文件。 Follow the update instructions at the official msys2 site before you do anything else. 在执行其他任何操作之前,请按照msys2官方网站上的更新说明进行操作。 It's only 3 steps long. 它只有3步长。

Warning: For some strange reason, once you pass the second update step, the msys2 command prompt shortcut stops working on my computer. 警告:由于某些奇怪的原因,一旦通过第二步更新,msys2命令提示符快捷方式将在我的计算机上停止工作。 Just navigate to the c:/msys2 folder and run msys2_shell.cmd if it does. 只需导航到c:/ msys2文件夹,然后运行msys2_shell.cmd即可。

3) You'll need mingw-w64 for msys2. 3)对于msys2,您将需要mingw-w64。 Some Stack Overflow answers suggest downloading both 64bit and 32bit versions of mingw, but since I'm no expert on the matter, it turned into a pain, so I was better off just downloading the 64bit version which is what I'm using and targeting. 一些Stack Overflow的答案建议下载mingw的64位和32位版本,但是由于我对此问题并不熟悉,所以这很痛苦,所以我最好还是下载我正在使用和定位的64位版本。 。 Run the following cmd in the msys2 cmd prompt: $ pacman -S gcc make mingw-w64-x86_64-gcc 在msys2 cmd提示符下运行以下cmd:$ pacman -S gcc make mingw-w64-x86_64-gcc

3-a) You'll need make and gcc for msys2. 3-a)对于msys2,需要make和gcc。 Run this in the msys2 cmd line: $ pacman -S gcc make 在msys2 cmd行中运行此命令:$ pacman -S gcc make

4) Once you're done downloading mingw-w64, gcc, and make. 4)完成下载mingw-w64,gcc和make之后。 You're ready to compile GLEW. 您已经准备好编译GLEW。 Unzip the source files you downloaded earlier, and put it into you're C:\\msys64\\home\\yourusername folder. 解压缩先前下载的源文件,并将其放入C:\\ msys64 \\ home \\ yourusername文件夹中。

5) Open the msys2 cmd prompt, navigate to the glew2.xx folder and run the following CMDs in the following order (as soon as each one is done): 5-a) make 5-b) make install 5-c) make install.all 5)打开msys2 cmd提示符,导航到glew2.xx文件夹,并按以下顺序运行以下CMD(每完成一次):5-a)make 5-b)make install 5-c)make install.all

6) You might get some errors and warnings, don't worry. 6)您可能会得到一些错误和警告,请放心。 Look in the lib folder within the glew2.xx folder, and you'll see the lib files you'll need. 在glew2.xx文件夹中的lib文件夹中查找,您将看到所需的lib文件。

7) Copy glew32.dll into the same folder where your programs executable is, and libglew32.a into your program's lib folder. 7)将glew32.dll复制到程序可执行文件所在的文件夹中,并将libglew32.a复制到程序的lib文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM