Rule of Thumb – Linking Order

Those newer to programming in C++ often fail to understand that compilers like GCC or Clang require a specific link order for the libraries in use or they don’t know what order to pick. Working on SFML and helping out in its community, I’ve had the pleasure to help people fix their linker errors many times and every now and then I’d explain a short rule of thumb one can keep in mind when specifying libraries to be linked. As such I wanted to share it here with you too.

If library X depends on library Y, then X must come before Y.

Library Order

It’s quite simple and can help immensely in figuring out the required order. For an example let’s take SFML:

SFML has five modules: system, audio, graphics, window and network. All modules depend on the system module, which means the system module needs to be at the end of list. Additionally the graphics module depends on the window module – how else would you draw something when there’s no window? As such, you’ll end up with a list similar to this: audio, network, graphics, window and system. It’s however not important where you place the audio or network module as long as it’s before the system module.

I hope this will help someone! As for SFML, if you try to link statically, don’t forget to link SFML’s dependencies too!

Leave a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.