Skip to content

Commit

Permalink
Review Win32 WinMain policy
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 26, 2023
1 parent 4b651c4 commit d5097cd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ list(APPEND CMAKE_MODULE_PATH ${HELLOIMGUI_CMAKE_PATH})
# HelloImGui Build options
###############################################################################

option(HELLOIMGUI_WIN32_EXECUTABLE "Under windows, hello_imgui_add_app will create apps without console, with main() as the entry point" ON)
#------------------------------------------------------------------------------
# Options / Windows: provide WinMain automatically
#------------------------------------------------------------------------------
if (WIN32)
option(HELLOIMGUI_WIN32_NO_CONSOLE "Under windows, build apps without Dos Console" ON)
option(HELLOIMGUI_WIN32_AUTO_WINMAIN "Under windows, automatically provide a WinMain (provide `int main(int, char**)`, it will be called by WinMain())" ON)
endif()

#------------------------------------------------------------------------------
# Options / Backend selection
Expand Down
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ __Table of contents__
* [handle events](#handle-events)
* [Full usage instructions and API](#full-usage-instructions-and-api)
* [Online interactive example applications](#online-interactive-example-applications)
* [Main signature: use `int main(int, char**)`](#main-signature-use-`int-mainint-char**`)
* [Build instructions](#build-instructions)
* [Supported platforms and backends](#supported-platforms-and-backends)
* [Clone the repository](#clone-the-repository)
* [Easy build on desktop platforms using Glfw](#easy-build-on-desktop-platforms-using-glfw)
* [Custom build: select your preferred backend](#custom-build-select-your-preferred-backend)
* [Warning, if using the SDL backend:](#warning-if-using-the-sdl-backend)
* [Build instructions for iOS](#build-instructions-for-ios)
* [Customizing the iOS build](#customizing-the-ios-build)
* [Build instructions for emscripten](#build-instructions-for-emscripten)
Expand All @@ -225,6 +225,14 @@ __Table of contents__

--------------------

# Main signature: use `int main(int, char**)`

Under windows, Hello ImGui will automatically provide a `WinMain()` function that will call main,
and expects its signature to be `int main(int, char**)`. You may get a linker error if your main function signature is for example `int main()`.

You can disable this via cmake by passing `-DHELLOIMGUI_WIN32_AUTO_WINMAIN=OFF` as a command line cmake option.


# Build instructions

## Supported platforms and backends
Expand Down Expand Up @@ -262,19 +270,6 @@ cmake .. -DHELLOIMGUI_USE_GLFW_OPENGL3=ON # To use your own version of GLFW
cmake .. -DHELLOIMGUI_USE_SDL_OPENGL3=ON # To use your own version of SDL (it should be findable via find_package(SDL2))
```

### Warning, if using the SDL backend:


Warning for SDL apps under iOS and Android:

SDL uses a dirty hack in order to _replace your main() function by its own main() function_,
which will then call your own main !

Please make sure that the signature of your main() function is *exactly*
`int main(int argc, char **argv)`
and that your main() function returns an int.



## Build instructions for iOS

Expand Down
12 changes: 8 additions & 4 deletions README.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ __Table of contents__

--------------------

# Main signature: use `int main(int, char**)`

Under windows, Hello ImGui will automatically provide a `WinMain()` function that will call main,
and expects its signature to be `int main(int, char**)`. You may get a linker error if your main function signature is for example `int main()`.

You can disable this via cmake by passing `-DHELLOIMGUI_WIN32_AUTO_WINMAIN=OFF` as a command line cmake option.


# Build instructions

## Supported platforms and backends
Expand Down Expand Up @@ -200,10 +208,6 @@ cmake .. -DHELLOIMGUI_USE_GLFW_OPENGL3=ON # To use your own version of GLFW
cmake .. -DHELLOIMGUI_USE_SDL_OPENGL3=ON # To use your own version of SDL (it should be findable via find_package(SDL2))
```

### Warning, if using the SDL backend:

@import "src/hello_imgui/hello_imgui.h" {md_id=SDLMain}


## Build instructions for iOS

Expand Down
11 changes: 11 additions & 0 deletions hello_imgui_cmake/HelloImGui_WinMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <windows.h>


int main(int, char **);


INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
{
int exit_result = main(0, NULL);
return exit_result;
}
18 changes: 11 additions & 7 deletions hello_imgui_cmake/hello_imgui_add_app.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ function(hello_imgui_add_app)
add_executable(${app_name} ${app_sources})
endif()

if (WIN32 AND HELLOIMGUI_WIN32_EXECUTABLE)
# Make this an app without console, and force it to use main() as the entry point, not WinMain()
# TODO: Use WinMain instead, this hack risks undefined behaviour: https://stackoverflow.com/a/72033725/7753444
if (MINGW)
target_link_options(${app_name} PRIVATE -Wl,--subsystem,windows,--entry,mainCRTStartup)
else() # If MSVC
target_link_options(${app_name} PRIVATE /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup)
if (WIN32)
if (HELLOIMGUI_WIN32_NO_CONSOLE)
# Make this an app without console, and use HelloImGui_WinMain.cpp
if (MINGW)
target_link_options(${app_name} PRIVATE -Wl,--subsystem,windows)
else() # If MSVC
target_link_options(${app_name} PRIVATE /SUBSYSTEM:WINDOWS)
endif()
endif()
if (HELLOIMGUI_WIN32_AUTO_WINMAIN)
target_sources(${app_name} PRIVATE ${HELLOIMGUI_CMAKE_PATH}/HelloImGui_WinMain.cpp)
endif()
endif()

Expand Down
13 changes: 0 additions & 13 deletions src/hello_imgui/hello_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,7 @@ namespace HelloImGui
float FrameRate(float durationForMean = 0.5f);
}

/**
@@md#SDLMain
Warning for SDL apps under iOS and Android:
SDL uses a dirty hack in order to _replace your main() function by its own main() function_,
which will then call your own main !

Please make sure that the signature of your main() function is *exactly*
`int main(int argc, char **argv)`
and that your main() function returns an int.
@@md
*/
#if defined(HELLOIMGUI_USE_SDL_OPENGL3)
#include <SDL_main.h>
#endif

0 comments on commit d5097cd

Please sign in to comment.