gpt4 book ai didi

c++ - 多重声明 gcc 的问题

转载 作者:行者123 更新时间:2023-11-30 05:29:34 26 4
gpt4 key购买 nike

我试图在我的 OpenGL 项目中使用 Nanovg,但出现了重复的多个定义错误,例如

CMakeFiles\Game.dir/objects.a(Game.cpp.obj):Game.cpp:(.text+0x2e91): multiple definition of `nvgCreateGL3'
CMakeFiles\Game.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2e91): first defined here

游戏.h

  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);

};

游戏.cpp

//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();`
}

渲染.cpp

//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 中
  • 将 Nanovg 包含和 #define ... 放入 Game.h
  • 尝试用 #includes ... 更改 Game.h 和 nanovg 库的位置(导致未知类型错误)

非常感谢您在此问题上的帮助

最佳答案

你应该添加这一行:

#define NANOVG_GL3_IMPLEMENTATION

仅在一个 .cpp 文件中,因为它看起来包含了当时的实现。在其他文件中仅使用标题。

希望这对您有所帮助。

关于c++ - 多重声明 gcc 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com