I am using a static library. But when I want to make a SHARED library, I can't build the projectas I get "multiple definition error"s.
Library CMake:
add_library(arcade-lib SHARED
src/button.cpp
src/menu.cpp
src/random.cpp)
target_include_directories(arcade-lib PUBLIC include)
target_link_libraries(arcade-lib PRIVATE sfml-window sfml-graphics)
Executable CMake:
add_executable(snake-game WIN32 snake-game.cpp
src/field.cpp
src/food.cpp
src/level.cpp
src/menu.cpp
src/snake.cpp)
target_link_libraries(snake-game PRIVATE arcade-lib sfml-graphics sfml-window sfml-system sfml-main)
target_include_directories(snake-game PRIVATE include)
I understand that the problem is that the SFML library is included 2 times. But if I remove the SFML library in one of the projects, then the header files stop seeing SFML since that Executable library uses SFML. How then to be?
Brief error message:
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g -static -static-libgcc -mwindows snake-game/CMakeFiles/snake-game.dir/snake-game.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/field.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/food.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/level.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/menu.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/snake.cpp.obj snake-game/CMakeFiles/snake-game.dir/src/witch_level.cpp.obj -o snake-game\bin\snake-game.exe -Wl,--out-implib,snake-game\lib\libsnake-game.dll.a -Wl,--major-image-version,0,--minor-image-version,0 arcade-games-lib/libarcade-lib.dll.a C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-main-d.a -lOpenGL32 -lwinmm -lgdi32 C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libfreetype.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Color.cpp.obj): in function `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Color.cpp:60: multiple definition of `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'; arcade-games-lib/libarcade-lib.dll.a(d000781.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Transformable.cpp.obj): in function `sf::Transformable::~Transformable()':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Transformable.cpp:49: multiple definition of `sf::Transformable::~Transformable()'; arcade-games-lib/libarcade-lib.dll.a(d000482.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Transformable.cpp.obj): in function `sf::Transformable::setPosition(float, float)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Transformable.cpp:56: multiple definition of `sf::Transformable::setPosition(float, float)'; arcade-games-lib/libarcade-lib.dll.a(d000466.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Transformable.cpp.obj): in function `sf::Transformable::setPosition(sf::Vector2<float> const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Transformable.cpp:66: multiple definition of `sf::Transformable::setPosition(sf::Vector2<float> const&)'; arcade-games-lib/libarcade-lib.dll.a(d000465.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Transformable.cpp.obj): in function `sf::Transformable::setOrigin(sf::Vector2<float> const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Transformable.cpp:112: multiple definition of `sf::Transformable::setOrigin(sf::Vector2<float> const&)'; arcade-games-lib/libarcade-lib.dll.a(d000475.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Transformable.cpp.obj): in function `sf::Transformable::getPosition() const':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Transformable.cpp:119: multiple definition of `sf::Transformable::getPosition() const'; arcade-games-lib/libarcade-lib.dll.a(d001252.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Shape.cpp.obj): in function `sf::Shape::~Shape()':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Shape.cpp:57: multiple definition of `sf::Shape::~Shape()'; arcade-games-lib/libarcade-lib.dll.a(d000823.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Shape.cpp.obj): in function `sf::Shape::setFillColor(sf::Color const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Shape.cpp:101: multiple definition of `sf::Shape::setFillColor(sf::Color const&)'; arcade-games-lib/libarcade-lib.dll.a(d000810.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Shape.cpp.obj): in function `sf::Shape::setOutlineColor(sf::Color const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Shape.cpp:116: multiple definition of `sf::Shape::setOutlineColor(sf::Color const&)'; arcade-games-lib/libarcade-lib.dll.a(d000813.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Shape.cpp.obj): in function `sf::Shape::setOutlineThickness(float)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Shape.cpp:131: multiple definition of `sf::Shape::setOutlineThickness(float)'; arcade-games-lib/libarcade-lib.dll.a(d000816.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Shape.cpp.obj): in function `sf::Shape::getGlobalBounds() const':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Shape.cpp:153: multiple definition of `sf::Shape::getGlobalBounds() const'; arcade-games-lib/libarcade-lib.dll.a(d001330.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(RectangleShape.cpp.obj): in function `sf::Vector2<float>::Vector2()':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/RectangleShape.cpp:35: multiple definition of `sf::RectangleShape::RectangleShape(sf::Vector2<float> const&)'; arcade-games-lib/libarcade-lib.dll.a(d000485.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(RectangleShape.cpp.obj): in function `sf::RectangleShape::setSize(sf::Vector2<float> const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/RectangleShape.cpp:43: multiple definition of `sf::RectangleShape::setSize(sf::Vector2<float> const&)'; arcade-games-lib/libarcade-lib.dll.a(d000484.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(RectangleShape.cpp.obj): in function `sf::RectangleShape::getSize() const':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/RectangleShape.cpp:51: multiple definition of `sf::RectangleShape::getSize() const'; arcade-games-lib/libarcade-lib.dll.a(d001259.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Text.cpp.obj): in function `sf::Text::Text(sf::String const&, sf::Font const&, unsigned int)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Text.cpp:99: multiple definition of `sf::Text::Text(sf::String const&, sf::Font const&, unsigned int)'; arcade-games-lib/libarcade-lib.dll.a(d000561.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Text.cpp.obj): in function `sf::Text::setFillColor(sf::Color const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Text.cpp:194: multiple definition of `sf::Text::setFillColor(sf::Color const&)'; arcade-games-lib/libarcade-lib.dll.a(d000550.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Font.cpp.obj): in function `sf::Font::Font()':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Font.cpp:87: multiple definition of `sf::Font::Font()'; arcade-games-lib/libarcade-lib.dll.a(d000533.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Font.cpp.obj): in function `sf::Font::~Font()':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Font.cpp:127: multiple definition of `sf::Font::~Font()'; arcade-games-lib/libarcade-lib.dll.a(d000536.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(Font.cpp.obj): in function `sf::Font::loadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/Font.cpp:142: multiple definition of `sf::Font::loadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'; arcade-games-lib/libarcade-lib.dll.a(d000516.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(RenderTarget.cpp.obj): in function `sf::RenderTarget::clear(sf::Color const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/RenderTarget.cpp:173: multiple definition of `sf::RenderTarget::clear(sf::Color const&)'; arcade-games-lib/libarcade-lib.dll.a(d000430.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-graphics-s-d.a(RenderTarget.cpp.obj): in function `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Graphics/RenderTarget.cpp:267: multiple definition of `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'; arcade-games-lib/libarcade-lib.dll.a(d000429.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(Window.cpp.obj): in function `sf::Window::create(sf::VideoMode, sf::String const&, unsigned int)':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/Window.cpp:73: multiple definition of `sf::Window::create(sf::VideoMode, sf::String const&, unsigned int)'; arcade-games-lib/libarcade-lib.dll.a(d000932.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(Window.cpp.obj): in function `sf::Window::close()':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/Window.cpp:155: multiple definition of `sf::Window::close()'; arcade-games-lib/libarcade-lib.dll.a(d000931.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(Window.cpp.obj): in function `sf::Window::setFramerateLimit(unsigned int)':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/Window.cpp:184: multiple definition of `sf::Window::setFramerateLimit(unsigned int)'; arcade-games-lib/libarcade-lib.dll.a(d000929.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(Window.cpp.obj): in function `sf::Window::display()':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/Window.cpp:216: multiple definition of `sf::Window::display()'; arcade-games-lib/libarcade-lib.dll.a(d000936.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(WindowBase.cpp.obj): in function `sf::WindowBase::isOpen() const':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/WindowBase.cpp:157: multiple definition of `sf::WindowBase::isOpen() const'; arcade-games-lib/libarcade-lib.dll.a(d001230.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(WindowBase.cpp.obj): in function `sf::WindowBase::pollEvent(sf::Event&)':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/WindowBase.cpp:164: multiple definition of `sf::WindowBase::pollEvent(sf::Event&)'; arcade-games-lib/libarcade-lib.dll.a(d000370.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(WindowBase.cpp.obj): in function `sf::WindowBase::hasFocus() const':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/WindowBase.cpp:303: multiple definition of `sf::WindowBase::hasFocus() const'; arcade-games-lib/libarcade-lib.dll.a(d001232.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(VideoMode.cpp.obj): in function `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/VideoMode.cpp:47: multiple definition of `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'; arcade-games-lib/libarcade-lib.dll.a(d001040.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-window-s-d.a(VideoMode.cpp.obj): in function `sf::VideoMode::getDesktopMode()':
D:/Programming/C++/Contrib/SFML/src/SFML/Window/VideoMode.cpp:58: multiple definition of `sf::VideoMode::getDesktopMode()'; arcade-games-lib/libarcade-lib.dll.a(d001038.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a(Clock.cpp.obj): in function `sf::Clock::Clock()':
D:/Programming/C++/Contrib/SFML/src/SFML/System/Clock.cpp:40: multiple definition of `sf::Clock::Clock()'; arcade-games-lib/libarcade-lib.dll.a(d000770.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a(Clock.cpp.obj): in function `sf::Clock::restart()':
D:/Programming/C++/Contrib/SFML/src/SFML/System/Clock.cpp:55: multiple definition of `sf::Clock::restart()'; arcade-games-lib/libarcade-lib.dll.a(d000769.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a(String.cpp.obj): in function `sf::String::String(char const*, std::locale const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/System/String.cpp:68: multiple definition of `sf::String::String(char const*, std::locale const&)'; arcade-games-lib/libarcade-lib.dll.a(d000892.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a(String.cpp.obj): in function `sf::String::String(sf::String const&)':
D:/Programming/C++/Contrib/SFML/src/SFML/System/String.cpp:129: multiple definition of `sf::String::String(sf::String const&)'; arcade-games-lib/libarcade-lib.dll.a(d000898.o):(.text+0x0): first defined here
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: C:/projects/snake-game/arcade-games/sfml/MinGW/lib/libsfml-system-s-d.a(Time.cpp.obj): in function `sf::Time::asSeconds() const':
D:/Programming/C++/Contrib/SFML/src/SFML/System/Time.cpp:46: multiple definition of `sf::Time::asSeconds() const'; arcade-games-lib/libarcade-lib.dll.a(d001298.o):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Make the various SFML targets INTERFACE dependencies so they are not linked into your shared library:
target_link_libraries(arcade-lib INTERFACE sfml-window sfml-graphics)