2016-12-28 1271 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标头。

相关问题