[英]MinGW Linking GLFW gets errors
嘗試將 GLFW 與 MinGW 鏈接時遇到問題。 代碼是 GLFW 的簡單使用示例。 我正在運行以下命令進行編譯:
g++ -std=c++0x -osimple simple.cpp -lglfw3 -lopengl32 -lglu32 -DGLFW_DLL
這是源代碼:
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
MinGW 返回一些與對 GLFW 庫函數的未定義引用相關的錯誤。
我用 g++ 嘗試了你的程序,並用 g++ 添加了標志 -lgdi32。 我能夠使用這個選項成功地完成這項工作。
通常,如果您使用 g++ 進行編譯,則通常需要 -lglfw3 、 -lopengl32 和 lgdi32 。 這在很大程度上還取決於您將 glfw 的庫和頭文件放在哪里。 只要它們在 gcc/g++ 可以找到的文件夾中,編譯和構建通常就沒有任何問題。 如果沒有標志,您將繼續收到未定義的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.