简体   繁体   中英

Errors during linking GLFW files

I have built GLFW from source and trying to build applications with it on Ubuntu Linux. But, g++ is always throwing errors about undefined references. I have followed many sites and posts, but they didn't solve my problem. This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

using namespace glm;

int main() {
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window;
    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);
    glewExperimental=true;
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    return 0;
}

I am compiling this program with this command: g++ glfw_prog.cpp -lGL -lglfw3 -o glfw_prog .

But, I am getting this long error https://pastebin.com/1b8fB2h7 .

How to fix this problem?

I got your program to compile with the following flags on Ubuntu:

g++ glfw_prog.cpp -lGL -lglfw -lGLEW -o glfw_prog

I have followed this site and solved my problem. I had to use the command gcc -o glfw_prog glfw_prog.cpp $(pkg-config --static --libs glfw3) -lGLEW . I just had to add the extra -lGLEW flag along with the pkg-config search path.

More specifically, this is the complete and system-independent command:
gcc -o glfw_prog glfw_prog.cpp -L/usr/local/lib -lglfw3 -lrt -lm -ldl -lX11 -lpthread -lxcb -lXau -lXdmcp -lGLEW -lGL

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