2015-12-21 114 views
0

我想在OpenGL编写一个非常简单的程序:简单的openGL的程序将无法通过编译

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

using namespace glm; 

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); // Use OpenGL 3.X 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Use openGL X.3 
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 

    // Open a window and create OpenGL context 
    GLFWwindow* window; // May be needed to make it global 
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL); 
    if(window == NULL) 
    { 
      fprintf(stderr, "Failed to pen GLFW window. If you have Intel GPU they are not 3.3 compatible. Try the 2.1 version of the tutorial.\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; 
    } 
} 

然而,当我尝试使用g ++,我发现了以下错误编译:

In function `main': 
playground.cpp:(.text+0x9): undefined reference to `glfwInit' 
playground.cpp:(.text+0x49): undefined reference to `glfwWindowHint' 
playground.cpp:(.text+0x58): undefined reference to `glfwWindowHint' 
playground.cpp:(.text+0x67): undefined reference to `glfwWindowHint' 
playground.cpp:(.text+0x76): undefined reference to `glfwWindowHint' 
playground.cpp:(.text+0x85): undefined reference to `glfwWindowHint' 
playground.cpp:(.text+0xa4): undefined reference to `glfwCreateWindow' 
playground.cpp:(.text+0xd2): undefined reference to `glfwTerminate' 
playground.cpp:(.text+0xe5): undefined reference to `glfwMakeContextCurrent' 
playground.cpp:(.text+0xeb): undefined reference to `glewExperimental' 
playground.cpp:(.text+0xf1): undefined reference to `glewInit' 
collect2: error: ld returned 1 exit status 

我发现了一些针对这个特定问题的帖子,解决方案似乎是将所需的库作为参数传递给g ++。但是,当我这样做时,问题仍然存在。

这是我的编译代码:

g++ -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -o openGLWindow openGLWindow.cpp 

任何帮助,将不胜感激......

+1

在命令行中输入**源文件的名称后,库选项属于**。 –

回答

5

when i try to compile it with g++ i'm getting the following errors:

不,你的编译就好。您正在收到链接错误,而不是编译错误。

链接错误的原因是您的链接命令行完全倒退。试试这个:

g++ -pthread -o openGLWindow openGLWindow.cpp -lglfw3 -lGLEW -lGLU -lGL -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lrt -ldl 

我不确定上面的库的顺序是正确的,但它肯定比你有更好的。命令行matters上的源,目标文件和库的顺序。

+0

感谢您的回复。当我改变参数的顺序到你指定的那个时,我得到这个错误:未定义的符号'dlclose @@ GLIBC_2.2.5'引用 /lib/x86_64-linux-gnu/libdl.so.2:添加符号错误:命令行中缺少DSO collect2:错误:ld返回1退出状态 –

+1

@ M.Kostas这只意味着您还需要添加'-ldl'(在末尾添加它)。 –

+0

好吧解决了,谢谢你的回复! –