Skip to content

Conversation

tpoechtrager
Copy link
Contributor

  • Added MinGW support
  • Added cross compilation support, see below
  • Removed unused -D__APPLE__, __APPLE__ is always defined there anyway
  • Removed -static from pkg-config flags and added the missing gl dependency instead
  • Added /opt/local includes for MacPorts on Mac OS X
  • Fall back to -lglfw -lgl in case pkg-config fails
  • Made the Linux target "Unix Generic" (should now compile on real Unices too)
  • Added -O2 and -g - with -O2 there are two new GCC warnings, see below

Cross Compilation Support:

I am using MinGW on (Arch-)Linux, while MinGW should now work on Windows too.

Example:

Cross Compiling for MinGW-w64 x86_64:

$ make HOSTPREFIX=x86_64-w64-mingw32
x86_64-w64-mingw32-g++  -I../.. -Wall -Wformat -c -o main.o main.cpp
x86_64-w64-mingw32-g++  -I../.. -Wall -Wformat -c -o imgui_impl_glfw.o imgui_impl_glfw.cpp
x86_64-w64-mingw32-g++  -I../.. -Wall -Wformat -c -o ../../imgui.o ../../imgui.cpp
x86_64-w64-mingw32-g++  -I../.. -Wall -Wformat -c -o ../../imgui_demo.o ../../imgui_demo.cpp
x86_64-w64-mingw32-g++  -I../.. -Wall -Wformat -c -o ../../imgui_draw.o ../../imgui_draw.cpp
[unused function warnings removed...]
x86_64-w64-mingw32-g++ -o imgui_example main.o imgui_impl_glfw.o ../../imgui.o ../../imgui_demo.o ../../imgui_draw.o  -I../.. -Wall -Wformat  -lglfw3 -lopengl32 -luser32 -limm32
Build complete for MinGW

Cross Compiling for MinGW-w64 i686 using clang:

$ make HOSTPREFIX=i686-w64-mingw32 CXX=i686-w64-mingw32-clang++
i686-w64-mingw32-clang++  -I../.. -Wall -Wformat -c -o main.o main.cpp
i686-w64-mingw32-clang++  -I../.. -Wall -Wformat -c -o imgui_impl_glfw.o imgui_impl_glfw.cpp
i686-w64-mingw32-clang++  -I../.. -Wall -Wformat -c -o ../../imgui.o ../../imgui.cpp
i686-w64-mingw32-clang++  -I../.. -Wall -Wformat -c -o ../../imgui_demo.o ../../imgui_demo.cpp
i686-w64-mingw32-clang++  -I../.. -Wall -Wformat -c -o ../../imgui_draw.o ../../imgui_draw.cpp
i686-w64-mingw32-clang++ -o imgui_example main.o imgui_impl_glfw.o ../../imgui.o ../../imgui_demo.o ../../imgui_draw.o  -I../.. -Wall -Wformat  -lglfw3 -lopengl32 -luser32 -limm32
Build complete for MinGW

New GCC (5.2.0) Warnings:

../../imgui.cpp: In function ‘ImGuiWindow* ImGui::GetParentWindow()’:
../../imgui.cpp:8917:1: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Wstrict-overflow]
 }
 ^

../../imgui_demo.cpp: In member function ‘void ExampleAppConsole::Run(const char*, bool*)’:
../../imgui_demo.cpp:1719:13: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Wstrict-overflow]
     void    Run(const char* title, bool* opened)
             ^

The line numbers are wrong, the first warning is caused by the assert in imgui.h @ line 778:

inline value_type& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; }

For the second one, I don't know.

@ocornut
Copy link
Owner

ocornut commented Aug 9, 2015

It might be the best to change the ImVector size type to size_t to get rid of the warning(s). Signed integer overflows can be really dangerous, if conditions might be optimized away, especially by GCC.

I have removed all size_t maybe two months ago, along removing the possibility to replace ImVector<> by user's implementation (or std::vector). It made the code much shorter, actually faster, less casts everywhere for cope with all platforms, smaller storage space, also allowing us to make the ImVector class better with extra helpers, it was a definitive win.

I don't know what you mean by "signed overflow can be really dangerous". Those operators requires a valid index anyway. Does casting i to (unsigned int)i fixes the warning?

@tpoechtrager
Copy link
Contributor Author

I have removed all size_t maybe two months ago, along removing the possibility to replace ImVector<> by user's implementation (or std::vector). It made the code much shorter, actually faster, less casts everywhere for cope with all platforms, smaller storage space, also allowing us to make the ImVector class better with extra helpers, it was a definitive win.

How about unsigned int instead of size_t?

I don't know what you mean by "signed overflow can be really dangerous". Those operators requires a valid index anyway.

Signed integer overflow invokes undefined behavior and is a great source for troubles.

Quoting the LLVM blog:

Signed integer overflow: If arithmetic on an 'int' type (for example) overflows, the result is undefined. One example is that "INT_MAX+1" is not guaranteed to be INT_MIN. This behavior enables certain classes of optimizations that are important for some code. For example, knowing that INT_MAX+1 is undefined allows optimizing "X+1 > X" to "true". Knowing the multiplication "cannot" overflow (because doing so would be undefined) allows optimizing "X*2/2" to "X".

Does casting i to (unsigned int)i fixes the warning?

Yes, there are no warnings with:

diff --git a/imgui.h b/imgui.h
index 7e3bf64..bb2d063 100644
--- a/imgui.h
+++ b/imgui.h
@@ -775,8 +775,8 @@ public:
    inline int                  size() const                    { return Size; }
    inline int                  capacity() const                { return Capacity; }

-    inline value_type&          operator[](int i)               { IM_ASSERT(i < Size); return Data[i]; }
-    inline const value_type&    operator[](int i) const         { IM_ASSERT(i < Size); return Data[i]; }
+    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < (unsigned int)Size); return Data[i]; }
+    inline const value_type&    operator[](int i) const         { IM_ASSERT((unsigned int)i < (unsigned int)Size); return Data[i]; }

    inline void                 clear()                         { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } }
    inline iterator             begin()                         { return Data; }

@ocornut
Copy link
Owner

ocornut commented Aug 9, 2015

How about unsigned int instead of size_t?

That still leads to lots of signed/unsigned casting in the code and in user's code, with higher warning settings that sort of casting never ends to spread. I initially had the code strictly use unsigned value for sizes and wondered why e.g. stb_ libraries tends to mostly use 'int' but I eventually drifted toward that, it made things really easier and neater.

I'm really puzzled by the fix, I wonder if it stem from an inline extension.

@tpoechtrager
Copy link
Contributor Author

That still leads to lots of signed/unsigned casting in the code and in user's code, with higher warning settings that sort of casting never ends to spread. I initially had the code strictly use unsigned value for sizes and wondered why e.g. stb_ libraries tends to mostly use 'int' but I eventually drifted toward that, it made things really easier and neater.

The C++ standard libraries are all using unsigned size types (AFAIK).

I'm really puzzled by the fix, I wonder if it stem from an inline extension.

Most likely, would at least explain the wrong line numbers.

As a workaround, you could check for __OPTIMIZE__ and make the assertions a no-op in "release" mode.

Not great either, but it would silence the warnings.

@ocornut
Copy link
Owner

ocornut commented Aug 9, 2015

The C++ standard libraries are all using unsigned size types (AFAIK).

Yes they do. It makes it really tedious to maintain 32+64 bits code with high level of warnings.
(I was referring to the stb library not std)

I'm still puzzled as to why casting both operands to (unsigned int) makes a difference at all. What does IM_ASSERT expand to for you?

@tpoechtrager
Copy link
Contributor Author

Yes they do. It makes it really tedious to maintain 32+64 bits code with high level of warnings.
(I was referring to the stb library not std)

Yes, I know, was just an example 😃.

However, unsigned int shouldn't be much of a problem as long as the user code is mostly using unsigned int too.

I'm still puzzled as to why casting both operands to (unsigned int) makes a difference at all. What does IM_ASSERT expand to for you?

Need to cast both operands to avoid signed/unsigned comparison.

Otherwise you would get tons of warnings:

In file included from ../../imgui.h:24:0,
                from ../../imgui_demo.cpp:12:
../../imgui.h: In instantiation of ‘const value_type& ImVector<T>::operator[](int) const [with T = float; ImVector<T>::value_type = float]’:
../../imgui.h:1239:134:   required from here
../../imgui.h:779:93: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    inline const value_type&    operator[](int i) const         { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                                            ^
../../imgui.h:779:67: note: in expansion of macro ‘IM_ASSERT’
    inline const value_type&    operator[](int i) const         { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                  ^
../../imgui.h: In instantiation of ‘ImVector<T>::value_type& ImVector<T>::operator[](int) [with T = ImFont*; ImVector<T>::value_type = ImFont*]’:
../../imgui_demo.cpp:214:46:   required from here
../../imgui.h:778:93: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                                            ^
../../imgui.h:778:67: note: in expansion of macro ‘IM_ASSERT’
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                  ^
../../imgui.h: In instantiation of ‘ImVector<T>::value_type& ImVector<T>::operator[](int) [with T = float; ImVector<T>::value_type = float]’:
../../imgui_demo.cpp:675:37:   required from here
../../imgui.h:778:93: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                                            ^
../../imgui.h:778:67: note: in expansion of macro ‘IM_ASSERT’
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                  ^
../../imgui.h: In instantiation of ‘ImVector<T>::value_type& ImVector<T>::operator[](int) [with T = ImVec2; ImVector<T>::value_type = ImVec2]’:
../../imgui_demo.cpp:1665:58:   required from here
../../imgui.h:778:93: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                                            ^
../../imgui.h:778:67: note: in expansion of macro ‘IM_ASSERT’
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                  ^
../../imgui.h: In instantiation of ‘ImVector<T>::value_type& ImVector<T>::operator[](int) [with T = char*; ImVector<T>::value_type = char*]’:
../../imgui_demo.cpp:1695:27:   required from here
../../imgui.h:778:93: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
                                                                                            ^
../../imgui.h:778:67: note: in expansion of macro ‘IM_ASSERT’
    inline value_type&          operator[](int i)               { IM_ASSERT((unsigned int)i < Size); return Data[i]; }
[... and so on]

@ocornut
Copy link
Owner

ocornut commented Aug 9, 2015

But both i and Size are 'int' I don't understand why they would need a cast at all. int < int is valid. That static-overflow warning wouldn't even apply here. That's why I was wondering if your IM_ASSERT macros or the underlying assert macro is expanding to something that create the behavior?

@tpoechtrager
Copy link
Contributor Author

But both i and Size are 'int' I don't understand why they would need a cast at all. int < int is valid. That static-overflow warning wouldn't even apply here.

Yes, i < Size is valid - but after inlining you may have something like:

IM_ASSERT(INT_MAX + 1 < Size);

which may be optimized to always "false".

Anyway, it could be a bogus warning too, it wouldn't be the first time for GCC to show wrong warnings.

That's why I was wondering if your IM_ASSERT macros or the underlying assert macro is expanding to something that create the behavior?

Preprocessed [] operator:

    inline value_type& operator[](int i) { 
# 778 "../../imgui.h" 3 4
                                                                ((
# 778 "../../imgui.h"
                                                                (unsigned int)i < (unsigned int)Size
# 778 "../../imgui.h" 3 4
                                                                ) ? static_cast<void> (0) : __assert_fail (
# 778 "../../imgui.h"
                                                                "(unsigned int)i < (unsigned int)Size"
# 778 "../../imgui.h" 3 4
                                                                , "../../imgui.h", 778, __PRETTY_FUNCTION__))
# 778 "../../imgui.h"
                                                                                                              ; return Data[i]; }

@ocornut
Copy link
Owner

ocornut commented Aug 9, 2015

Yes, i < Size is valid - but after inlining you may have something like:
IM_ASSERT(INT_MAX + 1 < Size);
which may be optimized to always "false".
Anyway, it could be a bogus warning too, it wouldn't be the first time for GCC to show wrong warnings.

If it follows inlining this way I don't quite understand why we don't have warnings everywhere in the code every ten lines. It sounds like a bogus warning to me but I'm really curious.

The IM_ASSERT expansion looks ok.

@tpoechtrager
Copy link
Contributor Author

If it follows inlining this way I don't quite understand why we don't have warnings everywhere in the code every ten lines.

It definitely looks like a backend warning, if it would be a frontend warning, then the correct line numbers would be shown.

Might be a gcc 5 regression, gcc 4.9 doesn't show up any warnings.

@ocornut
Copy link
Owner

ocornut commented Feb 12, 2024

I've been keeping this open as I felt there's always going to be some useful idea in there, but considering how time has passed it's not realistic :) beside if anything we are not likely be pushing makefiles much.

@ocornut ocornut closed this Feb 12, 2024
idbrii added a commit to idbrii/cpp-imgui that referenced this pull request Mar 24, 2025
Includes my merged PRs and everything in my dev branch. Haven't tested
with it yet.

Changelog:
Test case for clip rect
HACK: more recent Windows SDK and VS2017; disable graph
Set size to amount of space required
Merge pull request ocornut#349 from maksw2/master
Merge pull request ocornut#347 from mgerhardy/341
Merge pull request ocornut#348 from mgerhardy/fixed-warning
Merge pull request ocornut#346 from mgerhardy/280
Merge pull request ocornut#345 from mgerhardy/322
Merge pull request ocornut#344 from rherilier/fix-gcc-warnings
Merge pull request ocornut#336 from rherilier/add-isusingviewmanipulate
Merge pull request ocornut#335 from RedSkittleFox/alternative_window
Merge pull request ocornut#334 from ocornut/fix-beginchildframe
dear imgui update and small fixes
Merge pull request ocornut#316 from Batres3/2DSupport
Merge pull request ocornut#326 from Sayama3/use-push-pop-id
Merge pull request ocornut#328 from georgeto/master
Merge pull request ocornut#330 from maritim/master
Merge pull request ocornut#331 from GiovanyH/patch-1
Merge pull request ocornut#318 from dougbinks/imgui_math_operators
Merge pull request ocornut#312 from kimidaisuki22/master
div 0 fixed
Merge pull request ocornut#301 from ZingBallyhoo/using-any
Merge pull request ocornut#300 from Clog41200/Configurable-limits
Merge pull request ocornut#298 from xDUDSSx/fix/rotation_circles
Merge pull request ocornut#297 from xDUDSSx/fix/vertical-aspect-scaling
Merge pull request ocornut#289 from ComputationalBiomechanicsLab/fix_isusing-ignores-setid
Merge pull request ocornut#291 from ocornut/fix_math_operators_include
Merge pull request ocornut#282 from MohitSethi99/master
Merge pull request ocornut#276 from pthom/virtual_destructors
Merge pull request ocornut#271 from idbrii/clip-parent
Merge pull request ocornut#270 from idbrii/btn-behaviour
Merge pull request ocornut#265 from mgerhardy/pr/fix-minor-formatting
Merge pull request ocornut#264 from mgerhardy/pr/div0
Merge pull request ocornut#269 from peter1745/hatched-line-thickness-enhancement
Merge pull request ocornut#259 from miyanyan/master
Merge branch 'master' of https://github.com/CedricGuillemet/ImGuizmo
update dear imgui
Merge pull request ocornut#256 from Aidiakapi/patch-1
Merge pull request ocornut#252 from aaronkirkham/master
Merge pull request ocornut#249 from rokups/rk/mouse-capture
Merge pull request ocornut#246 from mgerhardy/pr/viewmanipulate
removed commented code
fix click view cube
Merge pull request ocornut#231 from mgerhardy/master
Merge pull request ocornut#230 from mgerhardy/master
Merge pull request ocornut#228 from longod/master
Merge pull request ocornut#227 from madeso/master
AddBezierCubic
Merge pull request ocornut#226 from sherief/master
revert culling test commit
Merge pull request ocornut#203 from rokups/rk/misc-fixes
Merge pull request ocornut#212 from zhaijialong/fix-behind-camera-cull
Merge pull request ocornut#209 from VictorFouquet/fix_normalize
scale is always local
Merge pull request ocornut#202 from pezy/master
imguizmo namespace
Merge pull request ocornut#194 from JonathanHiggs/vcpkg-example
1.84 WIP
Merge pull request ocornut#197 from idbrii/seq-btn-color
Merge pull request ocornut#196 from idbrii/seq-big-handles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants