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

Author Topic: How do I statically link SFML on Linux?  (Read 3240 times)

0 Members and 1 Guest are viewing this topic.

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
How do I statically link SFML on Linux?
« on: June 06, 2022, 12:49:44 pm »
Hey,

I have created a small game in C++ with SFML on Linux and would like to share it with my friend who uses Windows. However when he tries to run the .exe it can't find some .dll files. When I compiled I didn't know the difference between doing it dynamically or statically. After having read up on it I thought I would use static linking instead to have SFML integrated into the executable to avoid this problem. The problem is I don't know how :(

To compile my game for Linux I use:
g++ ./src/*.cpp -o main -lsfml-graphics -lsfml-window -lsfml-system
and for Windows:
x86_64-w64-mingw32-g++ ./src/*.cpp -o main.exe -lsfml-graphics -lsfml-window -lsfml-system -mwindows

On the FAQ https://www.sfml-dev.org/faq.php#build-link-static it says to link against the static libraries of SFML and add SFML_STATIC to the preprocessor option. What I though this meant was to add -s to the end of the libraries and use -Xpreprocessor SFML_STATIC.

So I tried:
x86_64-w64-mingw32-g++ ./src/*.cpp -o main.exe -Xpreprocessor SFML_STATIC -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -mwindows
That didn't work.

I am new to C++ and to be honest I have no idea what I am doing. I saw some similar threads to this one but still couldn't figure out how to do this. Maybe I am just dumb and missing something obvious.

Any help would be greatly appreciated!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How do I statically link SFML on Linux?
« Reply #1 on: June 07, 2022, 09:08:25 am »
Personally I can't recommend cross-compilation, because it usually ends up being more effort to get it right, than booting up some Windows VM and building it there or implementing some GitHub Actions or other CI that creates the build for you natively.

GCC has the flag -D for defines, so instead of -Xpreprocessor SFML_STATIC it should be -D SFML_STATIC
But not sure if the cross-compiler will properly handle static linking.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #2 on: June 07, 2022, 02:57:05 pm »
Okay so I decided to boot Windows 10 from a VM. I decided to use Code::Blocks and just follow along the getting started tutorial to see if I could get anything to compile but couldn't get it to work even though I think I did everything right.
I'll write down what I did to see if you can find what I did wrong:
  • I downloaded codeblocks-20.03mingw-setup.exe from https://www.codeblocks.org/downloads/binaries/
  • Checked which version of GCC came with the install and saw libgcc_s_seh-1.dll so I downloaded SFML GCC 7.3.0 MinGW (SEH) - 64-bit
  • Extracted SFML to C:
  • Created an empty project in Code::Blocks
  • In the project's "Build options", "project name", "Search directories" I added "C:\SFML-2.5.1\include" to "Compiler" and "C:\SFML-2.5.1\lib" to "Linker"
  • When linking the libraries in "Release", "Linker settings", "Link libraries" this is what I added in order:
    • sfml-graphics-s
    • sfml-window-s
    • sfml-system-s
    • opengl32
    • winmm
    • freetype
    • gdi32
  • I added SFML_STATIC to "project name", "Compiler settings", "#defines"
  • Created a main.cpp and copied the sample code to that file.
  • Lastly, I tried to build the sample green circle but then I get 19 build message errors about "undefined reference to 'sf::...' "
please help :'(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How do I statically link SFML on Linux?
« Reply #3 on: June 07, 2022, 03:17:01 pm »
You probably setup the Build Options only for release, but you need to set it up for Debug as well.

Additionally, for debug builds you need to use the libraries with the -s-d suffix
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #4 on: June 07, 2022, 03:38:01 pm »
That worked! Thank you so much dude! I didn't set it up for debug since I didn't see it written in the getting started tutorial but maybe I am just blind.

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #5 on: June 07, 2022, 04:52:28 pm »
Damn, I was wrong. It still doesn't entirely work. I can build the .exe but when my friend tries to run it he's still missing libgcc_s_seh-1.dll and libstdc++-6.dll. How do I also integrate those into the executable?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How do I statically link SFML on Linux?
« Reply #6 on: June 07, 2022, 05:20:42 pm »
You could copy those DLLs from the compiler's bin/ directory.

The alternative is a bit more tricky, because you'll have to build SFML yourself and set SFML_USE_STATIC_STD_LIBS while doing so.
Then when you build your application, you can add -static to your linker command.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #7 on: June 07, 2022, 10:08:56 pm »
I copied "libgcc_s_seh-1.dll", "libstdc++-6.dll" and "libwinpthread-1.dll" to the folder with the executable and now the only error is that occurs is:
The procedure entry point _ZSt28__throw_bad_array_new_lengthv could not be located in the dynamic link library.

So it still doesn't work, would it be easier to do this any other way?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: How do I statically link SFML on Linux?
« Reply #8 on: June 07, 2022, 11:18:58 pm »
Oh wait, I only skimmed the steps. As the download page states, the compilers have to match exactly. You can't just pick a random pre-compiled version of SFML with whatever compiler you have.

But you're in luck, because we do have pre-built versions for the compiler shipped with Code::Blocks but for the next version: https://artifacts.sfml-dev.org/by-branch/2.6.x/windows-gcc-810-mingw-64.zip
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #9 on: June 08, 2022, 12:16:53 am »
I set up the compiler and linker search directories for the new version you linked but we still get the same error after rebuilding:
The procedure entry point _ZSt28__throw_bad_array_new_lengthv could not be located in the dynamic link library.

neogulgul

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How do I statically link SFML on Linux?
« Reply #10 on: June 08, 2022, 09:38:02 pm »
Okay now it finally works, I just needed to enable the -static flag in "Compiler Flags". Thanks for all the help ;D

 

anything