Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: CMake Static Linking SFML Libraries [undefined reference]  (Read 7564 times)

0 Members and 1 Guest are viewing this topic.

Kas_st

  • Newbie
  • *
  • Posts: 1
    • View Profile
CMake Static Linking SFML Libraries [undefined reference]
« on: September 04, 2020, 11:02:01 am »
For a project im trying to statically link the sfml libraries.
I've built the sfml libraries from source with the same compiler im trying to build the project with.
The samples are all working correctly.
Now I'm trying to link my project using CMake, for reference im using Visual Studio Code as my editor:

HelloWorld.cpp:
#include "SFML/Graphics.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

CMakeLists.txt:
Code: [Select]
cmake_minimum_required(VERSION 3.10)

project(Asteroids VERSION 1.0)

SET(SFML_LIB_DIR ${CMAKE_SOURCE_DIR}/libs/lib/SFML-2.5.1)
SET(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/libs/include/SFML-2.5.1)
SET(SYSTEM_32 C:/Windows/System32/)
#find_package(OpenGL REQUIRED)

include_directories(${SFML_INCL_DIR})

add_executable(HelloWorld HelloWorld.cpp)

#if (OPENGL_FOUND)
#   message(${OPENGL_LIBRARIES})
#   target_link_libraries(HelloWorld ${OPENGL_LIBRARIES})
#endif()

find_library(FREETYPE
    freetype
    ${SFML_LIB_DIR}
    REQUIRED
)
find_library(SFML_WINDOW
    sfml-window-s-d
    ${SFML_LIB_DIR}
    REQUIRED
)
find_library(SFML_SYSTEM
    sfml-system-s-d
    ${SFML_LIB_DIR}
    REQUIRED
)
find_library(SFML_GRAPHICS
    sfml-graphics-s-d
    ${SFML_LIB_DIR}
    REQUIRED
)

target_link_libraries(HelloWorld
    ${SYSTEM_32}/opengl32.dll
    ${SYSTEM_32}/winmm.dll
    ${SYSTEM_32}/gdi32.dll
    ${FREETYPE}
    ${SFML_WINDOW}
    ${SFML_SYSTEM}
    ${SFML_GRAPHICS}
)

get_target_property(OUT HelloWorld LINK_LIBRARIES)
message(STATUS ${OUT})

install (TARGETS HelloWorld DESTINATION bin)


Build error log:
Code: [Select]
[main] Ordner wird erstellt: Asteroids
[build] Build wird gestartet.
[proc] Befehl wird ausgeführt: "C:\Program Files\CMake\bin\cmake.EXE" --build "**PROJECT_DIR**/Asteroids/build" --config Debug --target all -- -j 10
[build] [ 50%] Linking CXX executable HelloWorld.exe
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\HelloWorld.dir/objects.a(HelloWorld.cpp.obj):**PROJECT_DIR**/Asteroids/HelloWorld.cpp:5: undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\HelloWorld.dir/objects.a(HelloWorld.cpp.obj): in function `main':
[build] **PROJECT_DIR**/Asteroids/HelloWorld.cpp:5: undefined reference to `__imp__ZN2sf9VideoModeC1Ejjj'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:5: undefined reference to `__imp__ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:6: undefined reference to `__imp__ZN2sf11CircleShapeC1Efy'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:7: undefined reference to `__imp__ZN2sf5Color5GreenE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:7: undefined reference to `__imp__ZN2sf5Shape12setFillColorERKNS_5ColorE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:9: undefined reference to `__imp__ZNK2sf6Window6isOpenEv'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:12: undefined reference to `__imp__ZN2sf6Window9pollEventERNS_5EventE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:15: undefined reference to `__imp__ZN2sf6Window5closeEv'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:18: undefined reference to `__imp__ZN2sf5ColorC1Ehhhh'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:18: undefined reference to `__imp__ZN2sf12RenderTarget5clearERKNS_5ColorE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:19: undefined reference to `__imp__ZN2sf12RenderStates7DefaultE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:19: undefined reference to `__imp__ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:20: undefined reference to `__imp__ZN2sf6Window7displayEv'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:5: undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/Asteroids/HelloWorld.cpp:5: undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\HelloWorld.dir/objects.a(HelloWorld.cpp.obj): in function `sf::CircleShape::~CircleShape()':
[build] **PROJECT_DIR**/ASTERO~1/libs/include/SFML-2~1.1/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZTVN2sf11CircleShapeE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/ASTERO~1/libs/include/SFML-2~1.1/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZTVN2sf11CircleShapeE'
[build] c:/mingw64_experimental/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: **PROJECT_DIR**/ASTERO~1/libs/include/SFML-2~1.1/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZN2sf5ShapeD2Ev'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\HelloWorld.dir\build.make:111: HelloWorld.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/HelloWorld.dir/all] Error 2
[build] mingw32-make: *** [makefile:149: all] Error 2
[build] Der Build wurde mit dem Exitcode 2 abgeschlossen.

So pretty much only linking errors. I've checked that I link all the dependencies of the sfml libraries that im using (graphics) found on: https://www.sfml-dev.org/tutorials/2.5/start-cb.php


Am I missing something crucial in the linking process?

The get_target_property call in CMakeLists returns the correct paths to the libraries that should be linked and I've verified that all of the system32 dlls exist.

Thanks in advace!


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: CMake Static Linking SFML Libraries [undefined reference]
« Reply #1 on: September 16, 2020, 08:01:38 am »
You CMake code isn't how you find SFML.

Like mentioned in this thread you really just need to have the following three statements for linking SFML statically:

set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
...
target_link_libraries(SFMLTest sfml-graphics sfml-audio)

This requires that you set SFML_DIR to the directory where the SFML config files are located (usually SFML/lib/cmake/SFML).

Note also that SFML will automatically look for its dependencies and link them, so you don't really need to search for freetype in your own CMake script.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything