Skip to content

Compiling your project

Guillaume D edited this page Jun 21, 2024 · 7 revisions

How to use this wrapper in your project?

Installing CSFML

If you're a linux user, you'll be able to find how to compile and install CSFML system wide. Otherwise you need to get CSFML somewhere with its library files and includes. Keep the paths for later if it's not installed system wide.

Adding this as a module

Thanks to Zig's package manager, adding the module to your project is fairly easy:

zig fetch --save https://github.com/Guigui220D/zig-sfml-wrapper/archive/COMMIT_HASH.tar.gz

Replace the commit hash with the actual commit you want to import.

Adding the package

In your build.zig, add these lines to get the module and add it to your executable:

const dep = b.dependency("sfml", .{}).module("sfml");
exe.root_module.addImport("sfml", dep);

Replace exe with the name of the compile you're trying to make. You'll then be able to refer to @import("sfml") in your project. Doing this in two lines allows to get the dependency and add the include and library paths if needed.

Adding the include path

If you have installed CSFML system-wide, you can skip this step. On windows, you can use the C_INCLUDE_PATH environment variable. Otherwise, provide the path to the include folder of CSFML this way:

dep.addIncludePath(b.path("csfml/include/"));

Change it to what the actual path is.

Adding the library path

If you have installed CSFML system-wide, you can skip this step. On windows, you can use the LIBRARY_PATH environment variable. Otherwise, provide the path to the libs folder of CSFML this way:

exe.addLibraryPath(b.path("csfml/lib/msvc/"));

Change the path to what it actually should be. If you need something else than the mmsvc binaries, get that instead. Make sure you have downloaded CSFML with it's compiled libraries or that you have compiled it yourseld.

Linking CSFML

In your build.zig, import const sfml = @import("sfml") to get access to the helper function. You can then do

sfml.link(exe);

This will link all of these:

exe.linkLibC();
exe.linkSystemLibrary("csfml-graphics");
exe.linkSystemLibrary("csfml-system");
exe.linkSystemLibrary("csfml-window");
exe.linkSystemLibrary("csfml-audio");
exe.linkSystemLibrary("csfml-network");

You can also call these manually especially if you want to not link all of them.

Adding the dynamic link libraries

You will need your program to be able to reach the dynamic libs. On windows, those are the DLLs you need, you can for instance put them in the project directory, or add the path to them to the PATH environment variable.

  • csfml-system-2.dll
  • csfml-window-2.dll
  • csfml-graphics-2.dll
  • csfml-audio-2.dll (and openal32.dll)
  • csfml-network-2.dll

If you forget these, the build error could be a little bit cryptic (error 53). On linux, if libcsfml is properly installed, the program will be able to reach them automatically without you having to put them in the directory. Sometimes you need to edit LD_LIBRARY_PATH to make it work, on linux.

Complete build.zig

const std = @import("std");
const sfml = @import("sfml");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const mode = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "my_program",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = mode,
    });

    const dep = b.dependency("sfml", .{}).module("sfml");
    exe.root_module.addImport("sfml", dep);

    // Necessary when the includes and libs are not available system wide, change the paths to what you want if that's the case
    dep.addIncludePath(b.path("csfml/include/"));
    exe.addLibraryPath(b.path("csfml/lib/msvc/"));

    sfml.link(exe);

    const run = b.addRunArtifact(exe);
    const run_step = b.step("run", "Run the game");
    run_step.dependOn(&run.step);
}

How to compile the examples?

This repos has a build.zig file for tests and examples. It has steps called test and run-[example_name] (see Examples for a list of the examples you can test). Each run- step also has an install version to just get the compiled example in zig-out, just remove the run- prefix. There is also the all command to compile all examples.

For instance:

  • zig build green_circle (compile the green circle example as an executable in zig-out)
  • zig build run-sfml_example (run the "sfml example")
  • zig build all (compile all examples)
  • zig build test (run the test suite)

If you have a local installation of CSFML you might need to add the following lines to match your situation:

module.addLibraryPath(b.path("csfml/lib/msvc/"));
module.addIncludePath(b.path("csfml/include/"));