floooh / cimgui-sokol-starterkit

A minimal self-contained Dear ImGui starter project for Windows, Linux and macOS.
MIT License
221 stars 22 forks source link

Sokol on M1 mac with C++20 #15

Open g-tejas opened 8 months ago

g-tejas commented 8 months ago

How should I go about using sokol with imgui on M1 mac with C++20? I tried naively removing the cimgui wrapper, and changing the CMAkefile, but on mac, the sokol.c ends up getting compiled with objective-c which is not compatible with C++20, so not sure what to do here.

floooh commented 8 months ago

There are several options. The easiest one is probably to just rename all source files to .mm (for Objective-C++) and then remove this line in the CMakeLists.txt file:

https://github.com/floooh/cimgui-sokol-starterkit/blob/e63a36d31f06de4c71ffed3ff505c8b5b93e51d8/CMakeLists.txt#L32

...that way cmake picks the right language mode based on the file extension.

The next option is to split the sokol implementation into its own source file compiled as ObjC how I did here in the sokol samples (note the .m file extension):

https://github.com/floooh/sokol-samples/blob/master/libs/sokol/sokol.m

...and keep the rest of the project in C++ files (in that case I would use the Dear ImGui C++ API directly though, not the cimgui C bindings).

g-tejas commented 8 months ago

Just got it to work, but there's a slight problem though so

#=== LIBRARY: sokol
# add headers to the the file list because they are useful to have in IDEs
set(SOKOL_HEADERS
    sokol/sokol_gfx.h
    sokol/sokol_app.h
    sokol/sokol_glue.h)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
    add_library(sokol STATIC sokol/sokol.m ${SOKOL_HEADERS})
    # target_compile_options(sokol PRIVATE -x objective-c)
    target_link_libraries(sokol PUBLIC
        "-framework QuartzCore"
        "-framework Cocoa"
        "-framework MetalKit"
        "-framework Metal")
else()
    add_library(sokol STATIC sokol/sokol.m ${SOKOL_HEADERS})
    if (CMAKE_SYSTEM_NAME STREQUAL Linux)
        target_link_libraries(sokol INTERFACE X11 Xi Xcursor GL dl m)
        target_link_libraries(sokol PUBLIC Threads::Threads)
    endif()
endif()
target_link_libraries(sokol PUBLIC imgui)
target_include_directories(sokol INTERFACE sokol)

I changed sokol.c to sokol.m, removed #include sokol_imgui.h and #include imgui.h, only included them in the demo.cpp so my sokol.m looks like this

// sokol implementation library on non-Apple platforms
#define SOKOL_IMPL
#if defined(_WIN32)
#define SOKOL_D3D11
#elif defined(__EMSCRIPTEN__)
#define SOKOL_GLES3
#elif defined(__APPLE__)
// NOTE: on macOS, sokol.c is compiled explicitely as ObjC
#define SOKOL_METAL
#else
#define SOKOL_GLCORE33
#endif
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"

But for some reason, the #define SOKOL_METAL that should be defined in sokol.m, does not actualy get defined, and i get build errors when i run cmake --build ., I had to add it to demo.cpp like this for it to work

// demo.cpp
#define SOKOL_METAL
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "imgui.h"
#define SOKOL_IMGUI_IMPL
#include "sokol_imgui.h"

I adjusted the cmake file to remove cimgui and changed the extensions where needed as well