我試圖運行一個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中成功運行它。
請發佈鏈接器無法找到的功能。你有沒有對GLFW/GLEW做過任何修改?他們在哪裏(我在問'.lib'文件,而不是頭文件)? – 2015-03-31 20:56:53
如果它們以'_imp'開頭,則表示它們被標記爲已導出。你已經定義了'GLEW_STATIC',但已經編譯爲靜態的?爲什麼你甚至不打擾手動構建?首先,嘗試刪除'#define GLEW_STATIC'行。 – 2015-03-31 21:18:05
對不起,第一個評論 - 我還沒有完成。每個人都以__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