簡體   English   中英

GLFW3看似隨機的LNK2005錯誤

[英]Seemingly Random LNK2005 Errors With GLFW3

我收到錯誤LNK2005的原因似乎沒有原因,或者至少沒有我可以識別的原因。 本質上,使用以下代碼,我可以毫無問題地進行編譯。

測試

#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 )以及其他一些代碼時,它會給出錯誤:

已在Main.obj OpenGL D:\\ Users \\ Matthew \\ documents \\ visual studio 2015 \\ Projects \\ OpenGL \\ OpenGL \\ System.obj中定義的錯誤LNK2005“ struct GLFWwindow * window”(?window @@ 3PAUGLFWwindow @@ A)

已在Main.obj OpenGL D:\\ Users \\ Matthew \\ documents \\ visual studio 2015 \\ Projects \\ OpenGL \\ OpenGL \\ System中定義的錯誤LNK2005“ struct GLFWwindow * System :: window”(?window @ System @@ 3PAUGLFWwindow @@ A) .obj

錯誤LNK1169找到一個或多個乘法定義的符號OpenGL D:\\ Users \\ Matthew \\ documents \\ visual studio 2015 \\ Projects \\ OpenGL \\ Debug \\ OpenGL.exe

因此,我想知道導致錯誤的原因,我假設這與“上下文”有關。

在頭文件中,您需要定義變量,何時需要在頭文件中聲明變量並在一個源文件中對其進行定義。

在test.h中

namespace test {
    extern GLFWwindow* window;
}

和在main.cpp中

namespace test {
    GLFWwindow* window;
}

在標頭中沒有extern情況下,您將在每個包含test::window的源文件中創建一個test::window實例,如果存在兩個或多個定義,這將是一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM