Skip to content

Instantly share code, notes, and snippets.

@zpervan
Last active December 8, 2022 02:22
Show Gist options
  • Save zpervan/00faf923e770bce12a71b255c988cac7 to your computer and use it in GitHub Desktop.
Save zpervan/00faf923e770bce12a71b255c988cac7 to your computer and use it in GitHub Desktop.
Integrate Bazel with your SFML/Imgui projects on Ubuntu 20.04

Integrate Bazel into your Imgui/SFML projects on Ubuntu 20.04

Why use Bazel in the first place?

Some of you may ask why to use Bazel to build your project, aren't there already in-place solutions with CMake examples? And, to be honest, I thought the same, but in the end two major factors prevailed - curiosity and challenge! Also, as Bazel is slowly prying around the corner to be a popular build system, the syntax is more intuitive, readability is high and it's well documented on their official sites (but misses more examples IMHO). If you're interested what the cons and pros for Bazel against CMake are you can look up this page and check it in detail. 

Ok, I get it, now show me the how-to guide

First of all, you need Bazel installed on your system. Bazel's documentation is generous in these manners and you can look up the installation page for your OS and or, in our case, it's Ubuntu Linux (sorry Windows users :-( ).

After you installed Bazel and verified that it's working, download the following libraries from github, imgui and imgui-SFML.

To run imgui you will need to install a C++ compiler, some OpenGL libraries and the SFML library. In your terminal, run the following commands:

sudo apt update
sudo apt install -y build-essential libglew-dev libglfw3-dev libglfw3 freeglut3-dev libsfml-dev

Cool, I got everything as you said. What's next?

Let's make a new folder and name it ImguiSFMLBazel (so creative!). Afterwards, create a new folder inside your project under the name ThirdParty in which we shall copy our imgui library.  Also, integrate the imgui-SFML code as it is explained here.

Bazel needs a WORKSPACE file to treat a folder as a workspace. So, let's create one! Do the following:

  • At the top level of out project, create a file with the name WORKSPACE (no extensions)
  • We need to name our project and tell Bazel where imgui is located. To do this, copy the following into the WORKSPACE file:
workspace(name = "ImguiSFMLBazel")

# Imgui repository located inside your ThirdParty library folder
local_repository(
  name = "imguilib",
  path = "ThirdParty/imgui/",
)

The next step is to create a WORKSPACE and BUILD.bazel file inside the imgui lib in order to use the library. Do the following:

  • Create a WORKSPACE file and copy the following:
workspace(name = "imguilib")
  • Create a BUILD.bazel and copy the following:
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "imgui",
    srcs = glob([
        "*.cpp",
        "backends/*.cpp",
    ]),
    hdrs = glob([
        "*.h",
        "backends/*.h",
    ]),
    linkopts = [
        "-lglfw",
        "-lGLEW",
        "-lGLU",
        "-lGL",
        "-lglut",
        "-lsfml-graphics",
        "-lsfml-window",
        "-lsfml-system",
    ],
    visibility = ["//visibility:public"],
)

Next, create a main.cpp file inside your project at the top level. This file should contain the example code given in this example. Copy the content and save it.

Now, Bazel needs to know what to build and, therefore, we need to create another BUILD file for our main.cpp file. Create a new file with the name BUILD.bazel and copy the following code into it:

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    deps = [
        "@imguilib//:imgui",
    ],
)

After this done, position your terminal inside your projects root directory and run bazel run //:main. It should build project successfully and run the application as shown in the tutorial.

And for the end...

You can find a template repositoy on my github page which has implemented the previous steps if you want check something in detail. Github repository: ImguiSFMLBazel

If there is an exist an interest to work with imgui-SFMl on the Windows platform, I'll consider make an update with steps for enabling imgui-SFML on Windows.

Thanks and happy coding! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment