Test.h
#pragma once

struct Test
{
        void test();
};

Test.cpp
#include "Test.h"
#include <SFML/Graphics.hpp>

void Test::test()
{
        sf::FloatRect r;
}
 

main.cpp
#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>
int main()
{
        sf::FloatRect r;
        return 0;
}

Hi I'm trying to compile the above code on Visual studio 2017 and I get "error LNK2005: "public: __thiscall sf::Rect<float>::Rect<float>(void)" (??0?$Rect@M@sf@@QAE@XZ) already defined in Test.obj"

The error is gone when I comment out the #include <TGUI/TGUI.hpp>
Is this a problem with TGUI?

*

texus

  • *****
  • 1248
    • View Profile
    • Texus's Blog
Re: sf::Rect<float>::Rect<float>(void) already defined linker error
« Reply #1 on: 28 November 2017, 07:49:51 »
Since Rect is a template class, it shouldn't even be possible to have it defined twice, so the error makes little sense to me.
Since this is a linking error, could you try not linking to tgui? That will of course give a lot of undefined reference linking errors, but the question is whether this error remains or is gone. If the error disappears then maybe the SFML libraries you are using don't match the ones that were used to build TGUI.

Edit: The tgui::FloatRect class might be the reason why it can be defined twice (because it inherits from sf::Rect). I'm guessing you are linking dynamically? You could also try editing the TGUI/include/TGUI/FloatRect.hpp file and remove the TGUI_API on top (so change "class TGUI_API FloatRect" to "class FloatRect"). You don't have to recompile TGUI to test whether this worked, although I can't guarantee that you don't get other linking errors about this class if TGUI isn't recompiled.
« Last Edit: 28 November 2017, 08:25:01 by texus »

Re: sf::Rect<float>::Rect<float>(void) already defined linker error
« Reply #2 on: 28 November 2017, 16:38:29 »
Removing TGUI_API worked. Thank you