How to install imgui-sfml from conan
Tags: cpp, conan
Published: 2021-11-14 14:47:45

[Conan](https://conan.io/) is a powerful C++ package manager which simplify a lot the dependency managment in your projects.

Here I will talk about the installation of imgui-sfml from the perspective of someone who has never used it.

## Requirements

To follow this tutorial you will need to have on your machine :
- Python3 (we will use pip to install conan)
- CMake 

## Conan Installation

First we will need to install conan.
In your project directory run
```sh
$ python -m venv venv
$ source venv/bin/activate
$ pip install conan
$ conan
$ conan remote add bincrafters https://bincrafters.jfrog.io/artifactory/api/conan/conan
```
Here is the expected output : 
![Output of the command `conan`](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hhqepgo3a4g4pjr9vaf.PNG)

> To deactivate the virtualenv just run 
>```sh
$ deactivate
```


## Let's build the project

> In this section we will use CMake but if you want to use any other generator the documentation is [here](https://docs.conan.io/en/latest/reference/generators.html).

As there is no package for imgui-sfml in conan-center we will need to download it from [bincrafters](https://bincrafters.jfrog.io/).

For that we need to create a `conanfile.txt` at the root of the project.
./conanfile.txt
```
[requires]
imgui-sfml/2.3@bincrafters/stable

[generator]
cmake
```

Next add those two line in your `CMakeLists.txt`.
./CMakeLists.txt
```diff
cmake_minimum_required(VERSION 3.16)
project(MyProject)
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()

...
```

Then create the build folder and install the dependencies.
```sh
$ mkdir build
$ conan install . --install-folder ./build # You will have some error but that's normal
```

The point of the last command was to generate the file `~/.conan/profile/default`.

## Change the libcxx version
SFML which is a dependancy of imgui-sfml needs the libstdc++11 to work properly. However conan still use libstdc++ by default for backwards compatibility so you have to edit it by yourself.

Run the command:
```sh
conan profile update settings.compiler.libcxx=libstdc++11 default
```

or

Open the file `~/.conan/profile/default` and apply the following change

```diff
-compiler.libcxx=libstdc++
+compiler.libcxx=libstdc++11
```

## Go back to the project

Here is a code sample (from [here](https://eliasdaler.github.io/using-imgui-with-sfml-pt1/)) to test imgui-sfml.

```cpp
#include "imgui.h"
#include "imgui-SFML.h"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "");
    window.setVerticalSyncEnabled(true);
    ImGui::SFML::Init(window);

    sf::Color bgColor;

    float color[3] = { 0.f, 0.f, 0.f };

    // let's use char array as buffer, see next part
    // for instructions on using std::string with ImGui
    char windowTitle[255] = "ImGui + SFML = <3";

    window.setTitle(windowTitle);
    window.resetGLStates(); // call it if you only draw ImGui. Otherwise not needed.
    sf::Clock deltaClock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            ImGui::SFML::ProcessEvent(event);

            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        ImGui::SFML::Update(window, deltaClock.restart());

        ImGui::Begin("Sample window"); // begin window

                                       // Background color edit
        if (ImGui::ColorEdit3("Background color", color)) {
            // this code gets called if color value changes, so
            // the background color is upgraded automatically!
            bgColor.r = static_cast<sf::Uint8>(color[0] * 255.f);
            bgColor.g = static_cast<sf::Uint8>(color[1] * 255.f);
            bgColor.b = static_cast<sf::Uint8>(color[2] * 255.f);
        }

        // Window title text edit
        ImGui::InputText("Window title", windowTitle, 255);

        if (ImGui::Button("Update window title")) {
            // this code gets if user clicks on the button
            // yes, you could have written if(ImGui::InputText(...))
            // but I do this to show how buttons work :)
            window.setTitle(windowTitle);
        }
        ImGui::End(); // end window

        window.clear(bgColor); // fill background with color
        ImGui::SFML::Render(window);
        window.display();
    }

    ImGui::SFML::Shutdown();
}
```

Then in your project
```sh
$ conan install . --install-folder ./build # Now you should not have any error
$ cmake . -Bbuild
$ cmake --build build
$ ./build/bin/app
```
Once that's finished enjoy your application 😉.