0

I have the g++ compiler installed with MSYS2. In my CMake file, I have added the WIN32 flag to the add_executable command. It works for MSVC and Clang, but not for g++. It gives me a linking error saying undefined reference to "WinMain". Is there some sort of flag that I need to pass to the compiler by using target_compile_options?

EDIT: I think the problem is my entry point is wWinMain instead of plain WinMain.

EDIT2: My CMake files for reference:

Main CMake file

cmake_minimum_required(VERSION 3.22)

project(AlgorithmVisualizer)

add_subdirectory("AlgorithmVisualizer")

Project CMake file

cmake_minimum_required(VERSION 3.22)

add_executable(AlgorithmVisualizer WIN32
               "src/AV/Application.cpp"
               "src/AV/Application.hpp"
               "src/AV/Main.cpp"
               "src/AV/Window.cpp"
               "src/AV/Window.hpp"
               "src/AV/ErrorHandling.cpp" 
               "src/AV/ErrorHandling.hpp" "src/AV/Macros.hpp" "src/pch.hpp")

target_include_directories(AlgorithmVisualizer PRIVATE "deps/include")

target_compile_definitions(AlgorithmVisualizer PRIVATE $<$<CONFIG:Debug>:AV_DEBUG>)

target_precompile_headers(AlgorithmVisualizer PRIVATE "src/pch.hpp")

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory("deps/glfw")
add_subdirectory("deps/utfcpp")
add_subdirectory("deps/glad")

target_link_libraries(AlgorithmVisualizer glfw utf8::cpp glad Comctl32.lib)

target_compile_definitions(AlgorithmVisualizer PRIVATE UNICODE _UNICODE)

find_package(OpenGL REQUIRED)
target_link_libraries(AlgorithmVisualizer OpenGL::GL)

target_compile_features(AlgorithmVisualizer PUBLIC cxx_std_20)
if(MSVC)
    target_compile_options(AlgorithmVisualizer PUBLIC /Zc:__cplusplus)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(AlgorithmVisualizer PUBLIC -municode)
endif()
SunnyMonster
  • 500
  • 5
  • 14
  • Do you have a console app ? In case of GUI you'll have to use corss-platform entry function in your code. I.e. `#ifdef _WIN32 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) #else int main(int argc, const char** argv) #endif { .... return 0; }` Windows version of GNU LD linker looking for WinMain function as entry point and giving you an error since no such function in object files. – Victor Gubin Jul 10 '22 at 09:45
  • @VictorGubin I think my problem is that I have the `wWinMain` function as my entry point instead of `WinMain`. I tried defining `UNICODE` and `_UNICODE` in my CMake file but it did not work. – SunnyMonster Jul 10 '22 at 09:56
  • @VictorGubin I'm now using the normal `WinMain` function, and the linker throws no errors. But now, nothing happens when I run it. I put a breakpoint in the entry point and it does not get hit. Maybe now it is not calling the main function at all? – SunnyMonster Jul 10 '22 at 10:18
  • do you have a minimal example ? Do you have any windows run loops in this app ? `But now, nothing happens when I run it` - run it from command line i.e. run -> cmd locate and type exe name. – Victor Gubin Jul 10 '22 at 11:37
  • P.S. Look into https://stackoverflow.com/questions/3571250/wwinmain-unicode-and-mingw – Victor Gubin Jul 10 '22 at 11:38
  • @VictorGubin I know that my program works with Clang and MSVC (e.g. it actually shows a window and a background that changes colour). I've now tried adding the -municode flag with `target_compile_options` and it doesn't do anything. I tried running the g++ command myself with the -municode flag and it works. So I think it's probably to do with my CMake file. I've updated my question to include my CMake files. – SunnyMonster Jul 10 '22 at 22:14
  • 1
    For `g++` compiling a Win32 application, add `-Wl,-subsystem,windows` to pass the subsystem windows option to the linker. (you don't need anything special for a console application) – David C. Rankin Jul 11 '22 at 03:06

0 Answers0