我试图在我的OpenGL项目中使用Nanovg,并且会重复出现多个定义错误,例如
CMakeFiles \ Game.dir / objects.a(Game.cpp.obj):Game.cpp :(。text + 0x2e91):`nvgCreateGL3'的多重定义
CMakeFiles \ Game.dir / objects.a(main.cpp.obj):main.cpp :(。text + 0x2e91):首先在这里定义
class Game {
public:
void Run();
Game(std::string Title, ScreenMode ScreenMode, int Width, int MSAASamples, bool VSync);
private:
GLFWwindow* Window;
NVGcontext* VGContext;
std::string Title;
ScreenMode Mode;
int Width, Height;
int WWidth, WHeight;
int FBWidth, FBHeight;
int MSAASamples;
bool VSync;
bool Exit;
float PixRatio;
void Process();
void Render();
void KeyCallback(GLFWwindow* Window, int Key, int Scancode, int Action, int Mode);
void SaveScreenShot(const std::string* Path);
};
//various #includes .. (STL GlaD, GLFW)
#ifndef NANOVG_INCLUDED
#define NANOVG_INCLUDED
#include <nanovg.h>
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h>
#endif
// More #includes ...
#include <Game.h>
Game::Game(std::string Title, ScreenMode ScreenMode, int Width, int MSAASamples, bool VSync)
{
// constructor here
}
void Game::Run(){
// Initialise openGl and NanoVG, then into main game loop calling `Render();`
}
//various #includes .. (STL GlaD, GLFW)
#ifndef NANOVG_INCLUDED
#define NANOVG_INCLUDED
#include <nanovg.h>
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h>
#endif
// More #includes ...
#include <Game.h>
void Game::Render() {
//Definition using Nanovg
}
以下是一些可能有用的其他内容
CMakeLists Available Here
Full Console output Available Here
#define NANOVG_GL3_IMPLEMENTATION
放入Game.h #define ...
在Game.h #includes ...
(导致未知类型错误)非常感谢您对此问题的帮助
答案 0 :(得分:1)
你应该添加这一行:
#define NANOVG_GL3_IMPLEMENTATION
只在一个.cpp文件中,因为它看起来然后包含实现。在其他文件中只使用标题。
希望这会有所帮助。
答案 1 :(得分:0)
我没有看到任何:
#ifndef GAME_H
#define GAME_H
current content of game.h
#endif
所以你可能多次调用game.h。您必须在代码中包含此内容,因为可能会多次调用.h文件。
答案 2 :(得分:0)
我遇到了同样的问题。在main.cpp中包含nanovg头文件,但我无法将它们包含在另一个头文件中,就像在我的Application.h中一样:
#ifndef PROJECT_APPLICATION_H
#define PROJECT_APPLICATION_H
// --------------- INCLUDES
#include "Graphics/Window.h"
#include <nanovg.h>
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h> <..... MOVE this to cpp file!
class Application {
Window window;
NVGcontext* vg;
public:
Application();
~Application();
bool start();
private:
bool setup();
bool clean();
void render() const;
};
#endif //PROJECT_APPLICATION_H
然而,移动nanovg_gl包括Application.cpp工作。
// ---------------- INCLUDES
#include "Application.h"
// ----------------
#include <nanovg_gl.h> <---- HERE
Application::Application()
:window("Window", 1920 / 2, 1080 / 2)
{
setup();
}
Application::~Application() {
}
/**
* Handling rendering here...
*/
void Application::render() const {
nvgBeginFrame(vg, 1920 / 2, 1080 / 2, 1920 / 1080);
nvgBeginPath(vg);
nvgRect(vg, 100,100, 120,30);
nvgCircle(vg, 120,120, 5);
nvgPathWinding(vg, NVG_HOLE);
nvgFillColor(vg, nvgRGBA(255,192,0,255));
nvgFill(vg);
nvgEndFrame(vg);
}
...