【问题标题】:Seemingly Random LNK2005 Errors With GLFW3GLFW3 看似随机的 LNK2005 错误
【发布时间】:2016-06-18 01:58:43
【问题描述】:

我似乎无缘无故地收到错误 LNK2005,或者至少我无法识别。本质上,使用以下代码我可以毫无问题地编译。

test.h

#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>

namespace test
{
    //Objects and variables
    GLFWwindow* window;

    //Function prototypes
    bool Initialize();
}

test.cpp

#include "test.h"
#include <iostream>

bool test::Initialize()
{
    std::cout << "Initializing GLFW: OpenGL version 3.3 \n";

    //Initialize GLFW
    glfwInit();
    //Set window properties (version 3.3, core profile, not resizeable)
    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 Window
    window = glfwCreateWindow(800, 800, "Learn OpenGL", nullptr, nullptr);
    if (window = nullptr)
    {
        std::cout << "Failed to create GLFW window \n";
        glfwTerminate();
        return false;
    }

    return true;
}

int main()
{
    test::Initialize();

    return 0;
}

但是,当编译几乎完全相同的东西 (http://pastebin.com/VpPep9pM) 以及其他一些代码时,它会给出错误:

错误 LNK2005 "struct GLFWwindow * window" (?window@@3PAUGLFWwindow@@A) 已在 Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj 中定义

错误 LNK2005 "struct GLFWwindow * System::window" (?window@System@@3PAUGLFWwindow@@A) 已在 Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\ 中定义OpenGL\System.obj

错误 LNK1169 发现一个或多个多重定义符号 OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\Debug\OpenGL.exe

所以,我想知道导致错误的原因,我假设它与“上下文”有关。

【问题讨论】:

    标签: c++ glfw


    【解决方案1】:

    在你的头文件中,你定义了你的变量,当它需要在头文件中声明并在一个源文件中定义时。

    在test.h中

    namespace test {
        extern GLFWwindow* window;
    }
    

    在 main.cpp 中

    namespace test {
        GLFWwindow* window;
    }
    

    如果标题中没有extern,您会在包含它的每个源文件中创建一个test::window 实例,如果有两个或多个定义,这将是一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2011-06-16
      • 1970-01-01
      相关资源
      最近更新 更多