Skip to content

Using Dear ImGui as a C++ module / import ImGui; #8358

@radjkarl

Description

@radjkarl

Version/Branch of Dear ImGui:

v1.91.8 WIP / docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx11.cpp

Compiler, OS:

MSVC 2022 C++latest

Full config/build information:

Dear ImGui 1.91.8 WIP (19174)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=202004
define: _WIN32
define: _WIN64
define: _MSC_VER=1942
define: _MSVC_LANG=202004
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx11
io.ConfigFlags: 0x00000000
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 2 fonts, Flags: 0x00000000, TexSize: 512,512
io.DisplaySize: 1592.00,1046.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

import ImGui;

I wanted to share current solution to use ImGui as a C++20/23 module. I replaced almost all header files in my projects with modules (besides std and opencv). Its clean you know - not sure that justifies the pain of migrating but here I am ...
Attached 2 files (just remove the *.txt) can be used as replacement to #include "imgui.h", imgui_internal.h and imgui_stdlib.h.

ImGui.ixx.txt
ImGui_base.ixx.txt

As this started as an experiment, I manually listed all structs, functions and enums (with their values). To make this future proven, at some point I ll either make a python script or use some clang feature to automate this process.

  1. ImGui_base.ixx
    This file exports all objects of aforementioned headers - excluding their macros.

  2. ImGui.ixx
    Exposes ImGui_base.ixx and redefines (some) macros as constexpr or normal functions.

Lessons learned

  • import <imgui.h> -> fail (for my toolchain importing headers doesn't work anyway)
  • add imgui_....h to precompiled headers -> fail
  • just export all from imgui.h in a module -> fail. All items to be exported need to be listed (including values of unscoped enums)
  • these modules only expose all objects via using ::something; or using ::ImGui::something;, so automating this task later on should be straight forward
  • because macros don't leak from modules, I had to redefine them as functions - fortunately this only affects a few
  • as we don't redefine objects (excl macros), mixing imgui headers together with this module seems to works fine, and the following compiles:
module;

//<< those headers you don't need anymore, but if you forget to exclude them, your module should still compile
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_stdlib.h"
//>>

export module something;

import ImGui;

export
namespace something
{
...
}

This issue is more a showcase than a request to change anything. At a later stage maybe Ill create a PR/Fork if someone is interested.
Just one question for the dear reader: imgui_internal.h incudes some math functions, like ImPow that are declared static, see
static inline float ImPow(float x, float y) { return powf(x, y); }
Is there any good reason for them be static? These ones I also had to redefine in the ImGui.ixx module.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// this is just an example of how I import ImGui as a c++ module

export module something;

import ImGui; // <- this module exposes most items from imgui.h, imgui_internal.h and imgui_stdlib.h

export
namespace something
{

    ImVec2 myFunc()
    {
       return ImGui::something();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions