1

I'm linking SFML staticly, with CMake, and I'm getting undefined reference errors to all SFML classes or methods. CmakeLists.txt (SFML is in the same directory, and is x64):

cmake_minimum_required(VERSION 3.22)
project(YeOldFlapper)

add_definitions(-DSFML_STATIC)

set(SFML_INCLUDE_DIR "SFML/include")
set(SFML_LIB_DIR "SFML/lib")
set(CMAKE_CXX_STANDARD 14)

include_directories(${SFML_INCLUDE_DIR})
link_directories(${SFML_LIB_DIR})


add_executable(YeOldFlapper main.cpp)

target_link_libraries(YeOldFlapper sfml-graphics-s sfml-window-s sfml-system-s)

main.cpp

#include <SFML/Graphics.hpp>

namespace SFML = sf;

int main() {
    SFML::RenderWindow window(SFML::VideoMode(500, 500), "SFML WORKS!!!");

    while(window.isOpen()){
        SFML::Event event;
        while(window.pollEvent(event)){
            switch (event.type) {
                case SFML::Event::Closed:
                    window.close();
                    break;
            }
        }

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


    return 0;
}

I well linked, but i'g getting undefined references.

Kyrbyn YT
  • 11
  • 3
  • 1
    Starting from SFML 2.2, when static linking, you will have to link all of SFML's dependencies to your project as well. This means that if you are linking sfml-window-s.lib or sfml-window-s-d.lib for example, you will also have to link opengl32.lib, winmm.lib and gdi32.lib. – Cosemuckel Apr 18 '23 at 15:08
  • Well I changed the CmakeLists.txt target_link_libraries to ```target_link_libraries(YeOldFlapper opengl32 winmm gdi32 sfml-graphics-s sfml-window-s sfml-system-s)``` but is is still throwing the same errors – Kyrbyn YT Apr 18 '23 at 15:11
  • Can you post the errors? Based on [the docs](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) and [this answer](https://stackoverflow.com/a/39600062/7034621) I would assume you're supposed to pass `-lsfml-graphics-s` instead of `sfml-graphics-s`. – orhtej2 Apr 18 '23 at 15:15
  • Basically this for all sfml calls: ```C:\Program Files\JetBrains\CLion 2022.1\bin\mingw\bin/ld.exe: C:/Users/Mokykla/CLionProjects/SFML/Projects/YeOldFlapper/main.cpp:7: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'``` – Kyrbyn YT Apr 18 '23 at 15:18
  • Are you linking binaries compiled for MinGW? Is the extension `.a` and not `.lib`? MinGW binaries should have a `.a` extension for the static libraries. msvc should have `.lib` – drescherjm Apr 18 '23 at 15:34
  • Ok so I switched the VisualStudio sfml for mingw, and it compiles BUT now something called libgcc_s_seh-1.dll wasn't found – Kyrbyn YT Apr 18 '23 at 15:43
  • That dll should be in your MinGW bin folder. If it's not your SFML libraries are not compatible with your current compiler. My advice is to avoid this problem entirely and use msys2 to install MinGW and SFML using pacman from the mingw64 terminal. The VSCode documentation tells you to use msys2 to install MinGW anyways: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm Apr 18 '23 at 16:19

1 Answers1

0

The OP discovered that the link issue seen was due to him mixing up which libraries he thought he was linking with. He thought he was linking with MING32 SFML as but found he'd attempted to link with VC SFML.

Docs for SFML and CMake can be found here: https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php

Caribou
  • 2,070
  • 13
  • 29
Kyrbyn YT
  • 11
  • 3