2016-06-18 8 views
0

一見何の理由もなくLNK2005エラーが表示されます。基本的には、私は問題なくコンパイルすることができます次のコードを使用しています。GLFW3でランダムに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(); 
} 

#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)をコンパイルTEST.CPP、それが与えますエラー:

エラーLNK2005 "struct GLFWwindow * 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スタジオ2015 \ Projects \ OpenGL \ OpenGL \ System.obj

エラーLNK1169 1つ以上の乗算OpenGL D:¥Users¥Matthew¥documents¥visual studio 2015¥Projects¥OpenGL¥Debug¥OpenGL.exe

エラーの原因を知りたいと思っています。 「文脈」でやってください。

答えて

0

ヘッダーファイルでは、ヘッダで宣言して1つのソースファイルで定義する必要がある場合に、変数を定義しています。ヘッダー内のexternなしTEST.H

namespace test { 
    extern GLFWwindow* window; 
} 

でとmain.cppに

namespace test { 
    GLFWwindow* window; 
} 

、あなたがある、それを含んでいるすべてのソースファイルにtest::windowの1つのインスタンスを作成します2つ以上の定義がある場合は問題です。

関連する問題