Glusoft

Static build of SFML

Benefits of Static Linking

Downsides of Static Builds

IssueDescription
Larger file sizeThe executable contains all SFML code, making it significantly larger.
License RequirementsSFML is zlib-licensed, but you must ensure other statically linked libs (e.g. OpenAL, FreeType) comply too.
Longer Compile TimeStatic builds often take longer to build and link.
Manual LinkingYou must explicitly link all static dependencies, which can get tricky.

🛠️ When to Use a Static Build?

SituationRecommended?
Game jam or solo game projectYes
Distributing a CLI toolYes
Cross-platform GUI appUsually
Large modular enginePrefer dynamic
Hot-reloading librariesDynamic is better

The goal in this tutorial is to create a static build of SFML.

For visual studio

The first thing to do is pick the version compiled with the right version on the official website

If you have a 64 bits computer you can still pick the 32 bits version to support the older computers.
But nowaday a lot of softwares drop the 32 bit support. In my opinion you can pick the 64 bit version with no problem.

After Creating a project with visual studio add a main cpp file.

Unzip the file you downloaded, After that in the main you can add:

#include <SFML/Graphics.hpp>

int main() {
	sf::RenderWindow window(sf::VideoMode(400, 200), "SFML Installation");
	
	while (window.isOpen()) {
		sf::Event ev;
		while (window.pollEvent(ev)) {
			if (ev.type == sf::Event::Closed)
				window.close();
		}
	}

	return 0;
}

The you need to add the dll in the folder of the executable.

What if I don’t want to have to pack all the dlls for each modules ?
For that you will need to static link the SFML library, meaning the library will be pack in the executable.
On my computer with the DLLs the executable is very small : 10Kb.

Static linking

The first thing to do is change the linking of the libraries and take the static version of the SFML and the dependencies :

Linking static libraries and dependencies

SFML need to know the linking mode, so we need to define the macro SFML_STATIC :

Define macro preprocessor SFML STATIC

If the SFML library has been compiled with argument MT, meaning the runtime library is statically linked you need to change the code generation :

MT option to have the runtime library statically linked

You can download the example of static build of sfml : SFMLTutorials.exe
In this case even the static version is pretty small 109KB :).