2016-06-18 80 views
0

我得到错误LNK2005看似没有理由,或至少没有我能够识别。本质上,使用下面的代码我可以编译没有问题。看似随机的LNK2005与GLFW3错误

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 @@ 3PAU GLFWwindow @@ 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:\用户\马修\文件\视觉工作室2015 \项目\ OpenGL \调试\ OpenGL.exe

所以,我想知道是什么原因造成的错误,我假设它有东西用“背景”来做。

回答

0

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

在test.h

namespace test { 
    extern GLFWwindow* window; 
} 

和main.cpp中

namespace test { 
    GLFWwindow* window; 
} 

没有在标题中extern,你在包括它的每一个源文件创建test::window一个实例,它是如果存在两个或更多个定义,则是问题。