Compiling a sfml cpp project with nix-shell

Trying to make an sfml project with cpp but having trouble compiling.

Am using this shell.nix:

{ pkgs ? import <nixpkgs> { } }:

with pkgs;

mkShell rec {
  nativeBuildInputs = [
    gcc
  ];
  buildInputs = [
    sfml
  ];
}

Here is my main.cpp from following Opening and managing an SFML window - Simple and Fast Multimedia Library

#include <SFML/Window.hpp>

int main()
{
    sf::Window window(sf::VideoMode({800, 600}), "My window");
    return 0;
}

After running g++ main.cpp I get these errors:

/nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld: /tmp/ccPqpHTk.o: in function `main':
main.cpp:(.text.startup+0x61): undefined reference to `sf::String::String(char const*, std::locale const&)'
/nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld: main.cpp:(.text.startup+0x7c): undefined reference to `sf::VideoMode::VideoMode(sf::Vector2<unsigned int>, unsigned int)'
/nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld: main.cpp:(.text.startup+0xb0): undefined reference to `sf::Window::Window(sf::VideoMode, sf::String const&, unsigned int, sf::State, sf::ContextSettings const&)'
/nix/store/v63bxfiacw082c7ijshf60alvvrpfxsq-binutils-2.44/bin/ld: main.cpp:(.text.startup+0xca): undefined reference to `sf::Window::~Window()'
collect2: error: ld returned 1 exit status

Not sure what I’m doing wrong.

I found this post: Install sfml in nixos which simply instructs to use sfml and not system packages.

I found this; Can't Link SFML Dynamically on NixOS – Only .a Files Available? which seems to be the same problem but with no responses.

Thanks in advance for your help

I also started using sfml and c++ today and I think you need to run the following to only compile it to a object file (main.o) because sfml is not linked by default when you try g++ main.cpp

g++ -c main.cpp

Now Link it to provided sfml libraries to get the final exectuable sfml-app:

g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system

If you execute it now you will get the desired window:

./sfml-app

With your code nothing happens, because you are not actually displaying anything, to try it out use this instead:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode({800, 600}), "My window");

    while (window.isOpen())
    {
        while (const std::optional event = window.pollEvent())
        {
            if (event->is<sf::Event::Closed>())
                window.close();
        }

        window.clear();
        window.display();
    }
}

After compiling, linking and running the executable, we get this window:

oooooooooo that is exactly what I was missing, needing to compile it to an object file and then link. Something I completely missed because I’ve not worked in cpp lol. I appreciate you.

For posterity and future readers, I had figured out an alternative solution that completely slipped my mind to post here. I instead used the sfml cmake template: GitHub - SFML/cmake-sfml-project: Repository template for SFML projects using CMake, and this shell.nix:

{ pkgs ? import <nixpkgs> { } }:

with pkgs;

mkShell rec {
  nativeBuildInputs = [
    cmake
    sfml
  ];

  buildInputs = [
    flac
    freetype
    glew
    libjpeg
    libvorbis
    openal
    udev
    xorg.libXi
    xorg.libX11
    xorg.libXcursor
    xorg.libXrandr
    xorg.libXrender
    xorg.xcbutilimage
  ];
}

Can you share our full repo. I am facing some errors because sfml is compiled statically. like the lib works when the screen is static but not when I there is a motion.

I just tried they template repo you linked its working thanks.