Search code examples
c++macosg++sfml

Manually link and compile C++ program with SFML (macOS)


I am trying to create small C++ program with SFML and I don't want to use the Xcode template. I am using VSCode.

I downloaded SFML for Mac, and copied the headers and dylibs to my project directory like so:

  • include/ <- copied contents from the include directory (SFML dir with all headers)

  • lib/ <- copied contents from the lib folder (all the dylib files)

main.cpp looks like this:

#include "include/SFML/Graphics.hpp"
int main()
{
    return EXIT_SUCCESS;
}

The command I use to compile (succesfully): g++ main.cpp -o main -I include -L lib -l sfml-graphics -l sfml-window -l sfml-system

And when running the app I get following error:

dyld: Library not loaded: @rpath/libsfml-graphics.2.5.dylib
  Referenced from: /Users/mikal/code/cpp/sfml-box2d/./main
  Reason: image not found
zsh: abort      ./main

Solution

  • I needed to tell the linker where to look for the dynamic libraries at runtime by adding these linker options:

    -Wl,-rpath ./lib

    More information here: https://gcc.gnu.org/legacy-ml/gcc-help/2005-12/msg00017.html

    So the full command to compile for example the hello SFML code is: g++ main.cpp -o main -I include -L lib -l sfml-system -l sfml-window -l sfml-graphics -l sfml-audio -l sfml-network -Wl,-rpath ./lib