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

Author Topic: Unable to link statically compiled SFML with clang MSVC  (Read 98 times)

0 Members and 1 Guest are viewing this topic.

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Unable to link statically compiled SFML with clang MSVC
« on: November 08, 2024, 09:13:50 am »
I have statically compiled SFML 2.6.1 from source using LLVM 19 on Windows
When I try to compile my code, lld-link omits an error saying "mismatch detected for 'RuntimeLibrary'

I tried looking up fixes for this problem, but they all involve changing settings within Visual Studio and I do not want to use Visual Studio

Code Used:
#include "SFML/Graphics.hpp"

int main() {
    sf :: RenderWindow window(sf :: VideoMode(400, 400), "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;
};
 

Compile Command:
Code: [Select]
clang++ test.cpp -o test.exe -fuse-ld=lld-link -D SFML_STATIC -I "SFML-2.6.1\include" -L "SFML-2.6.1\build\lib" -l sfml-graphics-s -l sfml-window-s -l sfml-system-s -l opengl32 -l winmm -l gdi32

Error:
Code: [Select]
lld-link: error: /failifmismatch: mismatch detected for 'RuntimeLibrary':
>>> C:\Users\\AppData\Local\Temp\test-db37c8.o has value MT_StaticRelease
>>> sfml-graphics-s.lib(CircleShape.cpp.obj) has value MD_DynamicRelease
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
« Last Edit: November 09, 2024, 12:21:57 am by ebots »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #1 on: November 08, 2024, 10:11:48 am »
You need to set SFML_USE_STATIC_STD_LIBS to ON when building SFML with CMake, so that it too uses the static runtimes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #2 on: November 08, 2024, 10:12:51 am »
But then wouldn't that make it produce dlls instead of static libraries?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #3 on: November 08, 2024, 10:18:49 am »
Using static runtime with shared libraries is not a flow that SFML supports and neither one that you should ever be using, because you end up duplicating the runtime for each shared library (i.e. every DLL would have its own copy of the runtime), which can will cause issues.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #4 on: November 08, 2024, 10:25:31 am »
So does that mean that building with SFML_USE_STATIC_STD_LIBS will produce static libraries?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #5 on: November 08, 2024, 10:26:42 am »
Not on its own, for that you need to set BUILD_SHARED_LIBS to OFF.

See also the CMake tutorial: https://www.sfml-dev.org/tutorials/2.6/compile-with-cmake.php
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #6 on: November 08, 2024, 10:29:40 am »
I tried compiling SFML with BUILD_SHARED_LIBS off and SFML_USE_STATIC_STD_LIBS on, but I am still getting the same linker error when trying to compile my code

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #7 on: November 08, 2024, 10:46:13 am »
Make sure you're using the newly produced library files.

What's the exact new error?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #8 on: November 08, 2024, 10:51:24 am »
Code: [Select]
lld-link: error: /failifmismatch: mismatch detected for 'RuntimeLibrary':
>>> C:\Users\\AppData\Local\Temp\test-af06e4.o has value MT_StaticRelease
>>> sfml-graphics-s.lib(CircleShape.cpp.obj) has value MD_DynamicRelease
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #9 on: November 08, 2024, 10:56:39 am »
Did you actually rebuild SFML or just rerun CMake?

Code: [Select]
>>> sfml-graphics-s.lib(CircleShape.cpp.obj) has value MD_DynamicRelease
This is clearly not updated to use the static runtime, so either SFML wasn't rebuilt or you're still pointing to the old files.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #10 on: November 08, 2024, 11:01:18 am »
I did recompile SFML using the CMake GUI when I got that error, but regardless I tried recompiling SFML again with SFML_USE_STATIC_STD_LIBS on and BUILD_SHARED_LIBS off, and I am still getting the same linker error saying that the SFML static library has value MD_DynamicRelease. Also I checked and I am linking against the updated libraries

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #11 on: November 08, 2024, 11:30:17 am »
I did some more testing, and the problem seems to arise with the fact that LLVM downloaded directly from the website, uses MSVC, causing problems with linking and the runtime. Though I am not so sure why SFML is being marked as MD_DynamicRelease when it is static libraries. I tried using clang from MinGW, which uses GCC's standard library and that seems to work fine. I am not sure if this is an issue with MSVC or SFML, but it most likely seems to be an MSVC issue

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: Unable to link statically compiled SFML with clang
« Reply #12 on: November 08, 2024, 11:51:07 am »
I did recompile SFML using the CMake GUI
But the CMake GUI itself doesn't build anything.
It just generates make/project files. You still need to invoke your toolchain to actually generate the libraries.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebots

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Unable to link statically compiled SFML with clang
« Reply #13 on: November 08, 2024, 11:51:50 am »
I forgot to mention I also ran the makefile after the CMake GUI