microui icon indicating copy to clipboard operation
microui copied to clipboard

Add C++ compatibility guard

Open chirsz-ever opened this issue 6 years ago • 6 comments

Just add micro checking code to microui.h. Since than these codes can be introduced into C++ projects more easily.

chirsz-ever avatar Feb 23 '19 16:02 chirsz-ever

eh

microui can be compiled as C++ without errors, which then allows the use of type checking to the microui interface

despair86 avatar Jul 12 '19 06:07 despair86

Indeed so.

chirsz-ever avatar Mar 02 '20 15:03 chirsz-ever

I build a C++ progrom with microui then I found I must carefully make the #include instructions surrounded by extern "C" block, or the compiler would report errors, because the symbols of C functions, types and global variables is different from C++'s. It is worthwhile to add the "#ifdef __cplusplus ... #endif" guard to microui.h.

chirsz-ever avatar Mar 11 '20 06:03 chirsz-ever

@chirsz-ever That's because you compiled microui as C, not C++. Just force it to compile as C++.

Visual Studio : change extension to .cpp, or specify /TP on microui.c, or right click the file and set force compile as C++ CMake : SET_SOURCE_FILES_PROPERTIES( microui.c PROPERTIES LANGUAGE CXX ) gcc : use g++ instead of gcc, or specify -x c++ microui.c clang : use clang++, or specify -x c++ microui.c

g012 avatar Mar 11 '20 12:03 g012

I know, but add the guard is (almost) always easier. If someone want use microui as a library (static or dynamic), the C symbols would be linked more easily (the user could pay no attention to the compiler version), and the library would be able to be exported to other languages.

chirsz-ever avatar Mar 11 '20 12:03 chirsz-ever

I'm not sure what you mean by "exported to other languages", but if you're talking about mapping to say Lua or whatever, it's always easier in C than C++.

As for building microui as a library, it totally defies its purpose. It's a simple droppable file with minimalistic/core features only. It makes really no sense to build it as a library. I, for one, even merged the header and the implementation in one file, stb style.

g012 avatar Mar 11 '20 12:03 g012

OK, I think it jusk like the lua.h...

If someone want use extern "C", just do as the lua.hpp:

// microui.hpp
extern "C" {
#include "microui.h"
}

chirsz-ever avatar May 22 '24 02:05 chirsz-ever