SFML community forums

Help => General => Topic started by: neogulgul on June 06, 2022, 12:49:44 pm

Title: How do I statically link SFML on Linux?
Post by: neogulgul 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 (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!
Title: Re: How do I statically link SFML on Linux?
Post by: eXpl0it3r 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.
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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:
please help :'(
Title: Re: How do I statically link SFML on Linux?
Post by: eXpl0it3r 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
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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.
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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?
Title: Re: How do I statically link SFML on Linux?
Post by: eXpl0it3r 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.
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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?
Title: Re: How do I statically link SFML on Linux?
Post by: eXpl0it3r 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
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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.
Title: Re: How do I statically link SFML on Linux?
Post by: neogulgul 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