2016-12-28 48 views
0

您是否可以向我解釋爲什麼在構建以下程序時添加「legacy_stdio_definitions.lib」可以解決錯誤? 當我試圖使用GLFW庫時發生錯誤。 我對C++和OpenGL世界還很陌生,經過幾個小時的在線搜索和數小時的試驗和錯誤,我偶然發現了將「legacy_stdio_definitions.lib」添加到其他依賴關係的建議。這確實解決了錯誤,但我仍然不完全明白問題是什麼以及.lib做了什麼來解決它。通過添加legacy_stdio_definitions.lib解決了C++ GLFW錯誤。爲什麼?

我正在使用Microsoft Visual Studio 2015社區版btw。

所有我做的步驟是:

  1. C/C++ - >常規加入包括用於GLEW,GLM和GLFW
  2. 鏈接器>常規添加GLEW和GLFW庫
  3. 鏈接器>輸入添加 glew32.lib,glfw3.lib和legacy_stdio_definitions.lib
  4. 添加glew32.dll到我的項目文件夾

#include <stdio.h> 
#include <stdlib.h> 

#include <GL/glew.h> 

#include <GLFW/glfw3.h> 

#include <glm/glm.hpp> 
using namespace glm; 

int main() { 

    // Initialise GLFW 
    if (!glfwInit()) 
    { 
     fprintf(stderr, "Failed to initialize GLFW\n"); 
     return -1; 
    } 

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

                    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    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); // Initialize GLEW 
    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK) { 
     fprintf(stderr, "Failed to initialize GLEW\n"); 
     return -1; 
    } 

    // Ensure we can capture the escape key being pressed below 
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 

    do { 
     // Draw nothing, see you in tutorial 2 ! 

     // Swap buffers 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 

    } // Check if the ESC key was pressed or the window was closed 
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && 
     glfwWindowShouldClose(window) == 0); 

} 

任何幫助將不勝感激。

+1

這是一個VS2015問題:https://msdn.microsoft.com/en-us/library/bb531344.aspx。可能你正在使用一個已編譯的glew或glfw庫,它是用以前的VS版本編譯的。嘗試自己編譯glew和glfw。 – Ripi2

回答

1

Microsoft在Visual Studio 2015中製作了several changes,可能會破壞現有的代碼庫。根據你對問題的描述,以下是可能的罪魁禍首。引自here

所有printf和scanf函數的定義都被內嵌到stdio.h,conio.h和其他CRT頭文件中。對於任何在本地聲明瞭這些函數而沒有包含適當的CRT標頭的程序,這是一個重大變化,會導致鏈接器錯誤(LNK2019,未解析的外部符號)。如果可能的話,你應該更新代碼以包含CRT頭文件(即添加#include stdio.h)和內聯函數,但是如果你不想修改代碼來包含這些頭文件,另一種解決方案是將其他庫添加到鏈接器輸入legacy_stdio_definitions.lib。

GLFW必須在本地定義這些函數,而不包括CRT標頭。

相關問題