2015-03-31 130 views
1

我试图运行一个OpenGL程序,使用我自己构建的GLFW和GLEW库。我用的起动器代码是GLFW和GLEW的OpenGL - 用windows上的gcc编译

#include <iostream> 

// GLEW 
#define GLEW_STATIC 
#include <glew.h> 

// GLFW 
#include <glfw3.h> 


// Function prototypes 
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); 

// Window dimensions 
const GLuint WIDTH = 800, HEIGHT = 600; 

// The MAIN function, from here we start the application and run the game loop 
int main() 
{ 
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl; 
    // Init GLFW 
    glfwInit(); 
    // Set all the required options for GLFW 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); 

    // Create a GLFWwindow object that we can use for GLFW's functions 
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", 0, 0); 
    glfwMakeContextCurrent(window); 
    if (window == NULL) 
    { 
     std::cout << "Failed to create GLFW window" << std::endl; 
     glfwTerminate(); 
     return -1; 
    } 

    // Set the required callback functions 
    glfwSetKeyCallback(window, key_callback); 

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions 
    glewExperimental = GL_TRUE; 
    // Initialize GLEW to setup the OpenGL Function pointers 
    if (glewInit() != GLEW_OK) 
    { 
     std::cout << "Failed to initialize GLEW" << std::endl; 
     return -1; 
    }  

    // Define the viewport dimensions 
    glViewport(0, 0, WIDTH, HEIGHT); 

    // Game loop 
    while (!glfwWindowShouldClose(window)) 
    { 
     // Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions 
     glfwPollEvents(); 

     // Render 
     // Clear the colorbuffer 
     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 
     glClear(GL_COLOR_BUFFER_BIT); 

     // Swap the screen buffers 
     glfwSwapBuffers(window); 
    } 

    // Terminate GLFW, clearing any resources allocated by GLFW. 
    glfwTerminate(); 
    return 0; 
} 

// Is called whenever a key is pressed/released via GLFW 
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) 
{ 
    std::cout << key << std::endl; 
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 
     glfwSetWindowShouldClose(window, GL_TRUE); 
} 

然后I型

g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 

这产生了许多 '未定义参考' 错误。

代码应该没问题,因为我以前在Visual Studio中成功运行它。

+0

请发布链接器无法找到的功能。你有没有对GLFW/GLEW做过任何修改?他们在哪里(我在问'.lib'文件,而不是头文件)? – 2015-03-31 20:56:53

+0

如果它们以'_imp'开头,则表示它们被标记为已导出。你已经定义了'GLEW_STATIC',但已经编译为静态的?为什么你甚至不打扰手动构建?首先,尝试删除'#define GLEW_STATIC'行。 – 2015-03-31 21:18:05

+0

对不起,第一个评论 - 我还没有完成。每个人都以__imp_开头。然后glGetString,glGetInteger,CreateDCW,GetDeviceCaps,DeleteDC,GetDeviceGammaRanp,SetDeviceGammaRamp,CreateDIBSection,CreateBitmap,DeleteObject,wglGetProcAddress,DescribePixelFormat,SetPixelFormat,wglCreateContext,wglShareLists,wglDeleteContext,wglMakeCurrent,SwapBuffers,wgl_GetCurrentDC。我没有修改图书馆的来源。这些库位于与源文件夹平行的lib文件夹中。 – Ylfaue 2015-03-31 21:22:11

回答

5

此命令:

g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32

不会在Windows中是不够的。您将需要链接其他系统库。例如,在Visual Studio 2012的每个项目都连接这些默认:

kernel32.lib 
user32.lib 
gdi32.lib 
winspool.lib 
comdlg32.lib 
advapi32.lib 
shell32.lib 
ole32.lib 
oleaut32.lib 
uuid.lib 
odbc32.lib 
odbccp32.lib 

那就是为什么在VS编译罚款

kernel32.libuser32.lib应始终链接。当您执行任何图形操作时,需要使用gdi32.lib

解决方案一:

手动链接这些库:

g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -lkernel32 -luser32 -lgdi32 -lws2_32

如果我没有记错,ws2_32.a是使用MinGW提供的WinSock2库的名称。

解决方法二:

如果你使用MinGW的,您可以使用-mwindows标志:

g++ -I<path to headers> Tma.cpp -L<path to libraries> -lglu32 -lopengl32 -lglfw3 -lglew32 -mwindows

这将链接,以及其他一些,gdi32.akernel32.auser32.aws2_32.a

+0

谢谢,我会尽力的。 – Ylfaue 2015-03-31 22:15:53