user5534793
user5534793

Reputation:

Issues with Multiple Declarations gcc

I am attempting to use Nanovg in my OpenGL project and am getting repeated multiple definition errors such as

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

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

  };

Game.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
}

Here are some other things that may be useful CMakeLists Available Here
Full Console output Available Here

What I have tried

Many thanks in advance for the help with this issue

Upvotes: 2

Views: 294

Answers (3)

onurhb
onurhb

Reputation: 1181

I had the same problem. Including nanovg headers in main.cpp worked, but I could not include them inside another header, like this in my 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

However, moving nanovg_gl include to Application.cpp worked.

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


}
    ...

Upvotes: 0

Eamonn Kenny
Eamonn Kenny

Reputation: 2072

I don't see any:

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

so its possible that you are calling game.h multiple times. You must include this in your code because of the possibility of multiple calls to the .h file.

Upvotes: 0

marcinj
marcinj

Reputation: 50046

You should add this line:

#define NANOVG_GL3_IMPLEMENTATION

in only one .cpp file, as it looks like it then contains the implementation then. In other files use only header.

hope this helps.

Upvotes: 2

Related Questions