Skip to content

IMGUI_DEFINE_MATH_OPERATORS build error. #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
reformstudios opened this issue May 28, 2023 · 4 comments
Closed

IMGUI_DEFINE_MATH_OPERATORS build error. #226

reformstudios opened this issue May 28, 2023 · 4 comments

Comments

@reformstudios
Copy link

Hi Michal,
Thanks for sharing this library!
This is the first time I'm using imgui and a novice c++ programmer, so assume I'm doing everything wrong.

I'm taking a look at adding a node editor to my imgui app.
I've added this repo via my cmakelists.txt, but when I try to build, I'm getting an error (this is before I even add it to my project).

 In file included from /Users/pmac/src/cpp/imgui_app_template/build/_deps/imguinodeeditor-src/imgui_node_editor.cpp:12:
[build] In file included from /Users/pmac/src/cpp/imgui_app_template/build/_deps/imguinodeeditor-src/imgui_node_editor_internal.h:24:
[build] /Users/pmac/src/cpp/imgui_app_template/build/_deps/imgui-src/imgui_internal.h:101:2: error: Please '#define IMGUI_DEFINE_MATH_OPERATORS' _BEFORE_ including imgui.h!
[build] #error Please '#define IMGUI_DEFINE_MATH_OPERATORS' _BEFORE_ including imgui.h!

Should I only add the "imgui_node_editor.h" to the add_executable section, or all the base .h and .cpp files?
Removing the cpp files silences this error, but I get other issues in my script (perhaps unrelated).

Here's my cmakelists.txt.

cmake_minimum_required(VERSION 3.17)
project(imgui_app_template)

#-----------------------------------------------------------------------------------------------------------------------
# Download dependencies for GitHub
include(FetchContent)

FetchContent_Declare(
        glew
        GIT_REPOSITORY https://github.com/Perlmint/glew-cmake.git
        GIT_TAG glew-cmake-2.2.0
)

FetchContent_Declare(
        glfw
        GIT_REPOSITORY https://github.com/glfw/glfw
        GIT_TAG 3.3.8
)

FetchContent_Declare(
        imgui
        GIT_REPOSITORY https://github.com/ocornut/imgui
        GIT_TAG docking
)

FetchContent_Declare(
        imguinodeeditor
        GIT_REPOSITORY https://github.com/thedmd/imgui-node-editor
        GIT_TAG master
)

FetchContent_MakeAvailable(glew glfw imgui imguinodeeditor)

# FetchContent_Declare() normally supports EXCLUDE_FROM_ALL but apparently there is a bug
# See https://gitlab.kitware.com/cmake/cmake/-/issues/20167
# As a workaround, we exclude the directories manually
set_property(DIRECTORY ${glew_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES)
set_property(DIRECTORY ${glfw_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES)

#-----------------------------------------------------------------------------------------------------------------------
# Configure C++ version

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

#-----------------------------------------------------------------------------------------------------------------------
# Create executable
set(EXECUTABLE_NAME ${PROJECT_NAME})

add_executable(${EXECUTABLE_NAME}
        main.cpp

        ${imgui_SOURCE_DIR}/imgui.cpp
        ${imgui_SOURCE_DIR}/imgui.h
        ${imgui_SOURCE_DIR}/imgui_demo.cpp
        ${imgui_SOURCE_DIR}/imgui_draw.cpp
        ${imgui_SOURCE_DIR}/imgui_internal.h
        ${imgui_SOURCE_DIR}/imgui_tables.cpp
        ${imgui_SOURCE_DIR}/imgui_widgets.cpp

        ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.h

        ${imguinodeeditor_SOURCE_DIR}/imgui_node_editor.h
        ${imguinodeeditor_SOURCE_DIR}/imgui_node_editor.cpp

        ${imguinodeeditor_SOURCE_DIR}/crude_json.h
        ${imguinodeeditor_SOURCE_DIR}/imgui_bezier_math.h
        ${imguinodeeditor_SOURCE_DIR}/imgui_canvas.h
        ${imguinodeeditor_SOURCE_DIR}/imgui_extra_math.h
        ${imguinodeeditor_SOURCE_DIR}/imgui_node_editor_internal.h

        ${imguinodeeditor_SOURCE_DIR}/crude_json.cpp
        ${imguinodeeditor_SOURCE_DIR}/imgui_canvas.cpp
        ${imguinodeeditor_SOURCE_DIR}/imgui_node_editor_api.cpp

        )

# Add options for warning
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(${EXECUTABLE_NAME}
            PRIVATE
            -Wall -Wextra -pedantic
            )
endif ()

# Add a define to use the static version of Glew
target_compile_definitions(${EXECUTABLE_NAME}
        PRIVATE
        GLEW_STATIC
        )

# Set the include path
target_include_directories(${EXECUTABLE_NAME}
        PRIVATE
        ${imguinodeeditor_SOURCE_DIR}/
        ${imgui_SOURCE_DIR}/

        )

# Link against the appropriate libraries
target_link_libraries(${EXECUTABLE_NAME}
        PRIVATE
        glfw
        libglew_static
        $<$<CXX_COMPILER_ID:GNU>:opengl32.a>
        $<$<CXX_COMPILER_ID:MSVC>:opengl32.lib>
        )

@reformstudios
Copy link
Author

From digging around, I came across this issue : ocornut/imgui#6245
It looks like the changes made are sitting in the develop branch, hence why I'm having this issue using the master branch.
Can someone confirm that my interpretation here is correct?
I tried using the develop branch but it's raising a different error on build :
"""
In file included from /Users/patrickmacdonald/src/cpp/imgui_template_2/external/imgui-node-editor/imgui_node_editor.cpp:12:
[build] In file included from /Users/patrickmacdonald/src/cpp/imgui_template_2/external/imgui-node-editor/imgui_node_editor_internal.h:27:
[build] In file included from /Users/patrickmacdonald/src/cpp/imgui_template_2/external/imgui-node-editor/imgui_extra_math.h:69:
[build] /Users/patrickmacdonald/src/cpp/imgui_template_2/external/imgui-node-editor/imgui_extra_math.inl:37:22: error: redefinition of 'operator-'
[build] inline static ImVec2 operator-(const ImVec2& lhs)
[build] ^
[build] /Users/patrickmacdonald/src/cpp/imgui_template_2/External/ImGui/imgui.h:2481:23: note: previous definition is here
[build] static inline ImVec2 operator-(const ImVec2& lhs) { return ImVec2(-lhs.x, -lhs.y); }
[build] ^
[build] 1 error generated.

"""

@RedNicStone
Copy link

Same issue here. As you mentioned there seems to be a fix on develop as of PR #208.
This fix also does not work for me due to a redefinition of ‘ImVec2 operator-(const ImVec2&)’. Removing that definition seems to fix the issue.
I've bundled a fix for this as well as fixes for some other syntactically bogus statements into a branch on my fork. Waiting on merge to develop as per pull request #233.

@LittleCoinCoin
Copy link

Hello,

To add a bit more context, the issue comes from this commit on the side of ImGui: ocornut/imgui@fd94318

@thedmd
Copy link
Owner

thedmd commented Aug 31, 2023

Resolved on develop. Please let me know if it works for you.

@thedmd thedmd closed this as completed Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants