多个声明的问题gcc



我正试图在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):首次在此处定义

游戏.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();`
}

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
此处提供完整控制台输出

我尝试过的

  • 在Game.h中放置线#define NANOVG_GL3_IMPLEMENTATION
  • 放置Nanovg包括在Game.h中与#define ...进行AND运算
  • 尝试更改Game.h和nanovg库与#includes ...一起放置的位置(导致未知类型错误)

提前非常感谢您对此问题的帮助

您应该添加以下行:

#define NANOVG_GL3_IMPLEMENTATION

在一个.cpp文件中,因为它看起来像是然后包含了实现。在其他文件中,仅使用标头。

希望这能有所帮助。

我没有看到任何:

#ifndef GAME_H
#define GAME_H
current content of game.h
#endif

所以你可能多次调用game.h。您必须将其包含在代码中,因为可能会多次调用.h文件。

我也遇到了同样的问题。在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 include移动到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);

}
    ...

最新更新