【发布时间】: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
所以,我想知道导致错误的原因,我假设它与“上下文”有关。
【问题讨论】: