简体   繁体   中英

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

undefined reference to '[various things from GLEW library]'

This is my 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:

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.

Here is my 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.

Notice that it does not say undefined reference to '__imp_glewInit'

So it is linking some of GLEW, but not everything.

I run glewinfo.exe and the output has glGenVertexArrays: OK I think that means my driver supports that function.

Apparently, the binaries from the GLEW website only work for Visual C++.

If you want to use it with MinGW, you have to compile your own.

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

1) Download the GLEW Source files. DO NOT download the 32/64bit Window Binaries unless you're using Visual Studio, and if you are, you wouldn't be reading this.

2) Download, install, and update 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. Follow the update instructions at the official msys2 site before you do anything else. It's only 3 steps long.

Warning: For some strange reason, once you pass the second update step, the msys2 command prompt shortcut stops working on my computer. Just navigate to the c:/msys2 folder and run msys2_shell.cmd if it does.

3) You'll need mingw-w64 for msys2. 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. Run the following cmd in the msys2 cmd prompt: $ pacman -S gcc make mingw-w64-x86_64-gcc

3-a) You'll need make and gcc for msys2. Run this in the msys2 cmd line: $ pacman -S gcc make

4) Once you're done downloading mingw-w64, gcc, and make. You're ready to compile GLEW. Unzip the source files you downloaded earlier, and put it into you're C:\\msys64\\home\\yourusername folder.

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

6) You might get some errors and warnings, don't worry. Look in the lib folder within the glew2.xx folder, and you'll see the lib files you'll need.

7) Copy glew32.dll into the same folder where your programs executable is, and libglew32.a into your program's lib folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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