簡體   English   中英

GLEW 和 GLFW 未定義引用

[英]GLEW and GLFW undefined references

我正在嘗試使用 glfw 和 glew 設置 openGl。 這是源代碼:

#define GLFW_INCLUDE_GLU
#ifdef _WIN32
    #define GLFW_DLL // -> windows
    #include <GL/glew.h>
#elif __linux__
    #include <GL/glew.h>
#endif


#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>

GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;

int window_width = 800;
int window_height = 600;

static void window_hints() {
    const GLFWvidmode* mode = glfwGetVideoMode(monitor);

    glfwWindowHint(GLFW_RED_BITS, mode->redBits);
    glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
    glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
    glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);

    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
}


static void set_window_callbacks() {
    glfwSetWindowCloseCallback(window, [] (GLFWwindow *window) {
        std::cout << "closing window!";
    });
    glfwSetKeyCallback(window, [] (GLFWwindow *window, int key,int scancode, int action, int mods) {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
            glfwSetWindowShouldClose(window, GLFW_TRUE);
        }
    });
    glfwSetWindowSizeCallback(window, [] (GLFWwindow *window, int width, int height) {
        glViewport(0, 0, width, height);
        window_width = width;
        window_height = height;
        glLoadIdentity();
        GLdouble aspect = (GLdouble)window_width / window_height;
        glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
        glTranslatef(0, 0, -10);
    });
}

void GLAPIENTRY
MessageCallback( GLenum source,
                 GLenum type,
                 GLuint id,
                 GLenum severity,
                 GLsizei length,
                 const GLchar* message,
                 const void* userParam )
{
  fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
           ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
            type, severity, message );
}



int main(int argc, char *argv[])
{

    glfwSetErrorCallback([] (int code, const char * err_msg) {
        std::cerr << "GLFW Error " << code << ": \n\t" << err_msg << std::endl;
    });

    if(!glfwInit())
        throw std::runtime_error("glfwInit failed");

    monitor = glfwGetPrimaryMonitor();
    window_hints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    window = glfwCreateWindow(window_width, window_height, "test window", NULL, NULL);

    if(!window)
        throw std::runtime_error("glfwOpenWindow failed.");


    set_window_callbacks();

    // GLFW settings
    glfwMakeContextCurrent(window);
    glewInit(); 

    glfwSwapInterval(1);

    std::cout << glGetString(GL_VERSION) << std::endl;
    
    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
    glDebugMessageCallback(MessageCallback, 0);

    glMatrixMode(GL_PROJECTION);

    glViewport(0, 0, window_width, window_height);
    glLoadIdentity();
    GLdouble aspect = (GLdouble)window_width / window_height;
    glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
    glTranslatef(0, 0, -10);    


    while(!glfwWindowShouldClose(window))
    {
        // process pending events
        glfwPollEvents();
        glClearColor(0, 0, 0, 1); // black
        glClear(GL_COLOR_BUFFER_BIT);

        //glRotatef(1, 0, 0, 0.1);

        glBegin(GL_TRIANGLES);

        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(-0.5, -0.5);

        glColor3f(0.0, 1.0, 0.0);
        glVertex2f( 0.5, -0.5);

        glColor3f(0.0, 0.0, 1.0);
        glVertex2f( 0.0,  0.5);

        glEnd();

        glfwSwapBuffers(window);
    }

    // clean up and exit
    glfwTerminate();
    std::cerr << std::endl;
    return 0;
}

這在 Linux 下使用以下 makefile 完美運行:

main: main.cpp makefile
    g++ main.cpp -g -o main -lglfw -lGL -lX11 -lGLEW -std=c++2a

然后,我也想讓它在 windows 下編譯,所以我嘗試使用這個 makefile:

main.exe: main.cpp makefile
    g++ main.cpp -g -o main.exe -I"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\include" -I"D:\cpp_libraries\glew-2.1.0\include" -L"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\lib-mingw-w64" -L"D:\cpp_libraries\glew-2.1.0\lib\Release\x64" -lglfw3dll -lglew32 -lopengl32

這里,“glew-2.1.0”和“glfw-3.3.5.bin.WIN64”是從預編譯Windows二進制文件的相應下載頁面下載的。 我已將 "D:\\cpp_libraries\\glew-2.1.0\\bin\\Release\\x64" 和 "D:\\cpp_libraries\\glfw-3.3.5.bin.WIN64\\lib-mingw-w64" 添加到路徑環境變量中它們包含glew32.dll 和glfw3.dll。

當我嘗試編譯時,g++ 會為每個 glew/gl/glfw 函數調用提供“未定義的引用”。 我還嘗試通過 VS Code 的 tasks.json 運行相同的 g++ 命令,結果相同。

然后我注意到互聯網上的每個人都說我必須鏈接到“glfw3.lib”,我注意到 g++ 的第一個 -L 參數是一個實際上不包含任何 .lib 文件的文件夾。 但是我在之前下載的 zip 文件中的任何地方都找不到“glfw3.lib”。 我什至嘗試使用 CMake、Make 和 Code::Blocks 從源代碼構建 glfw,但它們都沒有生成我可以鏈接的 .lib 文件。

編輯:我忘了提到我沒有將 dll 添加到我的項目目錄中,但這確實沒有必要。 無論如何,我嘗試了它並沒有解決問題。

編輯 2:最好與 -lglfw 鏈接,而不是與 -lglfwdll 鏈接,所以我改變了它。

編輯 3:實際上,gl 函數鏈接得很好。 問題僅在於 glfw 和 glew。

我終於讓它工作了。 事實證明,glew 和 glfw 的預編譯二進制文件在我的機器上不起作用。 我必須下載這兩個源代碼並自己編譯這些庫。 這是最終有效的makefile:

main.exe: main.cpp makefile
    g++ main.cpp -g -o main.exe -I"D:/cpp_libraries/glfw-3.3.5-source/include" -I"D:/cpp_libraries/glew-2.1.0-source/include" -L"D:/cpp_libraries/glfw-3.3.5-compiled/src" -L"D:/cpp_libraries/glew-2.1.0-compiled/lib" -lglfw3 -lglew32 -lopengl32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM