我是c ++的新手,我正在尝试使用opengl制作程序来模拟卫星轨道。我目前有三个文件,main.cpp,window.h和window.cpp。我可以创建一个窗口,但是看到我的应用程序可能需要打开多个窗口,我想要实现一个窗口管理器。在尝试这样做时,我遇到了链接错误。下面的代码按预期编译和运行。
的main.cpp
#include "window.h"
#include <iostream>
#include <GLFW/glfw3.h>
int main() {
Window window("Space Sim", 800, 600);
//Sets the background color of the window
glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
std::cout << glGetString(GL_VERSION) << std::endl;
while (!window.closed()) {
window.clear();
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, 0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
window.update();
}
glfwTerminate();
return 0;
}
window.h中
#pragma once
#include <GLFW\glfw3.h>
class Window {
private:
const char *title;
int width, height;
GLFWwindow *window;
bool init();
public:
Window(const char *title, int width, int height);
~Window();
void clear() const;
bool closed() const;
void update();
void setSize(int width, int height);
inline bool isWindow(GLFWwindow* window) { return this->window == window; }
inline int getWidth()const { return width; }
inline int getHeight() const { return height; }
};
window.cpp
#include <GLFW\glfw3.h>
#include "window.h"
#include <iostream>
Window::Window(const char *title, int width, int height) {
this->title = title;
setSize(width, height);
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
//This may be incorrect
window = NULL;
}
void Window::update() {
glfwPollEvents();
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glfwSwapBuffers(window);
}
void Window::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
bool Window::closed() const {
return glfwWindowShouldClose(window) == 1;
}
bool Window::init() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}
window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!window) {
glfwTerminate();
std::cout << "Failed to initialize window" << std::endl;
return false;
}
glfwMakeContextCurrent(window);
return true;
}
void Window::setSize(int width, int height) {
this->width = width;
this->height = height;
}
但是,当一个空的静态函数作为framebuffer_size_callback添加到窗口时,链接器错误会出现在构建中。
window.h中
#pragma once
#include <GLFW\glfw3.h>
class Window {
private:
const char *title;
int width, height;
GLFWwindow *window;
bool init();
public:
Window(const char *title, int width, int height);
~Window();
void clear() const;
bool closed() const;
void update();
void setSize(int width, int height);
static void framebuffer_size_callback();
inline bool isWindow(GLFWwindow* window) { return this->window == window; }
inline int getWidth()const { return width; }
inline int getHeight() const { return height; }
};
window.cpp with static method
#include <GLFW\glfw3.h>
#include "window.h"
#include <iostream>
Window::Window(const char *title, int width, int height) {
this->title = title;
setSize(width, height);
if (!init()) {
glfwTerminate();
}
}
Window::~Window() {
//This may be incorrect
window = NULL;
}
void Window::update() {
glfwPollEvents();
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glfwSwapBuffers(window);
}
void Window::clear() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
bool Window::closed() const {
return glfwWindowShouldClose(window) == 1;
}
bool Window::init() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}
window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!window) {
glfwTerminate();
std::cout << "Failed to initialize window" << std::endl;
return false;
}
glfwMakeContextCurrent(window);
return true;
}
void Window::setSize(int width, int height) {
this->width = width;
this->height = height;
}
void Window::framebuffer_size_callback() {
}
,错误是:
严重级代码描述项目文件行抑制状态 错误LNK2019未解析的外部符号&#34; public:__ thishisall Window :: Window(char const *,int,int)&#34; (?? 0Window @@ @ QAE @ PBDHH Z) 在函数中引用 _main SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ SpaceSim \ main.obj 1错误LNK2019未解决 外部符号&#34; public:__ thiscall Window :: ~Window(void)&#34; (?? 1Window @@ QAE @ XZ)在函数中引用 _main SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ SpaceSim \ main.obj 1错误LNK2019未解决 外部符号&#34; public:void __thiscall Window :: clear(void)const&#34; (?clear @ Window @@ QBEXXZ)在函数中引用 _main SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ SpaceSim \ main.obj 1错误LNK2019未解决 外部符号&#34; public:bool __thiscall Window :: closed(void)const&#34; (?关闭@ Window @@ QBE_NXZ)在函数中引用 _main SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ SpaceSim \ main.obj 1错误LNK2019未解决 外部符号&#34; public:void __thiscall Window :: update(void)&#34; (?update @ Window @@ QAEXXZ)在函数中引用 _main SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ SpaceSim \ main.obj 1错误LNK1120 5未解决 外部SpaceSim C:\ Users \ Cameron \ Documents \ Visual Studio 2015 \ Projects \ SpaceSim \ Debug \ SpaceSim.exe 1
在我的visual studio项目中,我有dll并包含了glfw的文件,但是我不明白为什么当静态函数看起来正确并且没有做任何事情时我会收到链接器错误。非常感谢任何帮助。