Dev C ++中的链接器错误:未定义引用' glfwInit'

时间:2015-10-09 01:50:22

标签: c++ opengl glfw

我正在尝试关注OpenGL的this教程。我最初是手工复制代码,但这不起作用,所以我直接从网站上复制粘贴代码。我一直收到这个错误:

[Linker error] undefined reference to 'glfwInit'

来自此代码(感觉比必要时间长):

//C++ standard headers
#include <stdio.h>
#include <stdlib.h>
//GLEW header
#include <GL/glew.h>
//GLFW header
#include <GL/glfw3.h>

int main()
{
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL); 
    if( window == NULL )
    {
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
            glfwTerminate();
                return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental=true; // Needed in core profile 
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
    // Draw nothing, see you in tutorial 2 !

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

    }
    // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
    glfwWindowShouldClose(window) == 0 );
}

我不知道它为什么不编译。任何人都知道发生了什么事吗? 编辑:我正在使用Dev-C ++,正如标题中所述。

1 个答案:

答案 0 :(得分:2)

undefined reference to 'glfwInit'

表示链接器未找到定义glfwInit()的库。您必须将glfw3.a添加到链接器输入。实际上,Dev-C ++使用MinGW,因此与Visual Studio不同,库不能是.lib

要使用Dev-C ++,请转到您的项目选项&#39;,&#39;参数&#39;,然后添加图书馆&#39;。然后浏览资源管理器以查找我提到的glfw3.a(通常在GLFW-<version>/lib/)。