Compiling C++ project using SFML with cl.exe


The usefulness of this approach is debatable, but let me try to offer an answer on why you might want to know how to do that. I think it’s the mark of a good engineer to know what’s happening under the hood. Simple as that. New or inexperienced developers will benefit from understanding the connection between the user interface in Visual Studio and the actual command line tools used underneath.


Imagine that you’re on Windows and want to use the MSVC compiler toolset from the command line to build a project with SFML. In a nutshell you’re going to replicate the documentation given for SFML and Visual Studio, but from the terminal.

Assuming you have installed all the necessarry tooling and you have SFML in the C:\libs\SFML-2.5.1 directory you can just do the following.

Dynamic Linking

# For DEBUG builds -d is part of the lib names. See SFML docs on that.
cl Main.cpp Game.cpp Window.cpp "sfml-graphics-d.lib" "sfml-window-d.lib" "sfml-system-d.lib" "sfml-audio-d.lib" "sfml-network-d.lib" /I "C:\libs\SFML-2.5.1\include" /MDd /link /LIBPATH:"C:\libs\SFML-2.5.1\lib" /DEBUG

Let’s quickly break that down.

  • After cl put your .cpp files, then add all .lib files because the linker will look for those.
  • With /I you include the path to the SFML headers.
  • Next is /MDd which is dealing with the runtime and adding additional debug information.
  • We also need /link which is used to pass options to the linker and in this case we’re passing /LIBPATH which contains the path to the SFML libraries.
  • Finally we pass /DEBUG to the linker because we’re doing a debug build in this case.

This will create a Main.exe in your current folder. You’ll need to add the appropriate SFML .dll files to that folder in order to execute your program successfully.

Static Linking

For static linking the process is almost the same with some slight differences.

# Note -s-d as part of the lib names. See SFML docs on that.
cl Main.cpp Game.cpp Window.cpp "sfml-graphics-s-d.lib" "sfml-window-s-d.lib" "sfml-system-s-d.lib" "sfml-audio-s-d.lib" "sfml-network-s-d.lib" opengl32.lib freetype.lib winmm.lib gdi32.lib user32.lib Advapi32.lib /I "C:\libs\SFML-2.5.1\include" /D SFML_STATIC /MDd /link /LIBPATH:"C:\libs\SFML-2.5.1\lib" /DEBUG
  • This time we’re including the static version of the SFML libraries.
  • We have to pass the extra dependencies needed by SFML in order for the build to be successful and not blow up with a lot of link errors.
  • We use the /D flag to define the SFML_STATIC macro.

If all is well you’ll be able to just run your Main.exe straight away.

Compiling a project with SFML in Vim

Compiling a project with SFML in Vim

Final Words

Learn to use CMake :)

comments powered by Disqus