Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding -municode doesn't fix undefined references to `WinMain' #106

Closed
aminya opened this issue May 23, 2022 · 8 comments
Closed

Adding -municode doesn't fix undefined references to `WinMain' #106

aminya opened this issue May 23, 2022 · 8 comments

Comments

@aminya
Copy link

aminya commented May 23, 2022

I have written a macro in project_options to try to fix the WinMain issue in MinGW. But adding -municode as a compile option doesn't fix the issue. Adding it as a link option causes linking to the fail with undefined reference to wWinMain. Other compilers don't need such flags, and I am not sure why we should do this for MinGW.

macro(mingw_unicode)
  is_mingw(_is_mingw)
  if(${_is_mingw})
    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag("-municode" _cxx_supports_municode)
    if(${_cxx_supports_municode})
      message(STATUS "Enabling Unicode for MinGW in the current project to fix undefined references to WinMain")
      add_compile_definitions("UNICODE" "_UNICODE")
      add_compile_options("-municode")
    endif()
  endif()
endmacro()

Blocks aminya/project_options#129

https://github.com/aminya/cpp_vcpkg_project/runs/6542106054?check_suite_focus=true#step:6:203

[6/18] Linking CXX executable my_exe\test\Debug\my_exe_helpers_tests.exe
FAILED: my_exe/test/Debug/my_exe_helpers_tests.exe 
cmd.exe /C "cd . && C:\Users\runneradmin\gcc\mingw64\bin\g++.exe -g  my_exe/test/CMakeFiles/my_exe_helpers_tests.dir/Debug/tests.cpp.obj -o my_exe\test\Debug\my_exe_helpers_tests.exe -Wl,--out-implib,my_exe\test\Debug\libmy_exe_helpers_tests.dll.a -Wl,--major-image-version,0,--minor-image-version,0  vcpkg_installed/x64-mingw-static/debug/lib/libfmtd.a  --coverage  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
c:/users/runneradmin/gcc/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/users/runneradmin/gcc/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text+0x46): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[7/18] Linking CXX executable my_lib\test\Debug\my_lib_tests.exe
FAILED: my_lib/test/Debug/my_lib_tests.exe my_lib/test/my_lib_tests_tests-84286ba.cmake D:/a/cpp_vcpkg_project/cpp_vcpkg_project/build/my_lib/test/my_lib_tests_tests-84286ba.cmake 
cmd.exe /C "cd . && C:\Users\runneradmin\gcc\mingw64\bin\g++.exe -g  my_lib/test/CMakeFiles/my_lib_tests.dir/Debug/tests.cpp.obj -o my_lib\test\Debug\my_lib_tests.exe -Wl,--out-implib,my_lib\test\Debug\libmy_lib_tests.dll.a -Wl,--major-image-version,0,--minor-image-version,0  my_lib/Debug/libmy_lib.a  --coverage  vcpkg_installed/x64-mingw-static/debug/lib/libfmtd.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D D:\a\cpp_vcpkg_project\cpp_vcpkg_project\build\my_lib\test && C:\Users\runneradmin\cmake\cmake-3.23.1-windows-x86_64\bin\cmake.exe -D TEST_TARGET=my_lib_tests -D TEST_EXECUTABLE=D:/a/cpp_vcpkg_project/cpp_vcpkg_project/build/my_lib/test/Debug/my_lib_tests.exe -D TEST_EXECUTOR= -D TEST_WORKING_DIR=D:/a/cpp_vcpkg_project/cpp_vcpkg_project/build/my_lib/test -D TEST_SPEC= -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D TEST_LIST=my_lib_tests_TESTS -D TEST_REPORTER=xml -D TEST_OUTPUT_DIR= -D TEST_OUTPUT_PREFIX= -D TEST_OUTPUT_SUFFIX= -D CTEST_FILE=D:/a/cpp_vcpkg_project/cpp_vcpkg_project/build/my_lib/test/my_lib_tests_tests-84286ba.cmake -P D:/a/cpp_vcpkg_project/cpp_vcpkg_project/build/vcpkg_installed/x64-mingw-static/share/catch2/CatchAddTests.cmake""
c:/users/runneradmin/gcc/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/users/runneradmin/gcc/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text+0x46): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[8/18] Building CXX object my_header_lib/test/constexpr/CMakeFiles/my_header_lib_relaxed_constexpr_tests.dir/Debug/constexpr_tests.cpp.obj
Checking D:\a\cpp_vcpkg_project\cpp_vcpkg_project\my_header_lib\test\constexpr\constexpr_tests.cpp ...
Checking D:\a\cpp_vcpkg_project\cpp_vcpkg_project\my_header_lib\test\constexpr\constexpr_tests.cpp: CATCH_CONFIG_MAIN=1;CATCH_CONFIG_RUNTIME_STATIC_REQUIRE=1;FMT_LOCALE=1;UNICODE=1;_UNICODE=1;CMAKE_INTDIR="Debug"...
ninja: build stopped: subcommand failed.
task: Failed to run task "coverage": task: Failed to run task "build_template": exit status 1
@brechtsanders
Copy link
Owner

I'm not sure why you log this as a specific winlibs issue, it seems you have general MinGW-w64 issues with our project. These kind of questions are better asked on forums like https://stackoverflow.com/

A quick Internet search for your issue leads me to https://techstalking.com/solved-wwinmain-unicode-and-mingw/
Have you tried the suggestions there?

Also, why to you use WinMain()? Is that intentional, or did you mean to use main(). In the latter case you should use -mconsole (instead of -mwindows) when linking, so you get a console application.

@aminya
Copy link
Author

aminya commented May 25, 2022

Where is the repository that hosts the MinGW source code? This is not a StackOverflow question, it is a bug that is only present in MinGW.

Also, why to you use WinMain()? Is that intentional, or did you mean to use main(). In the latter case you should use -mconsole (instead of -mwindows) when linking, so you get a console application.

I don't know either. MinGW for some reason uses WinMain instead of the standard main function. This doesn't happen on other compilers. I can't detect the intention of the user in project_options, and so I can't work around this MinGW bug by adding -mconsole

@brechtsanders
Copy link
Owner

brechtsanders commented May 25, 2022

http://mingw-w64.sourceforge.net/

MinGW-w64 gcc uses main() for console applications, which is the default unless you specify you want a GUI application (-mwindows). So you probably have a flag like this somewhere...

@aminya
Copy link
Author

aminya commented May 25, 2022

Thanks. I'll open an issue there. In the above log I have included all the flags passed to g++

@aminya
Copy link
Author

aminya commented May 26, 2022

Reported here:
https://sourceforge.net/p/mingw-w64/bugs/942/

@brechtsanders
Copy link
Owner

brechtsanders commented May 27, 2022

I don't really understand why you try to fix the undefined reference to WinMain by doing a Unicode build.

Can you try to search the entire folder recursively after running cmake for mwindows and if you find it replace it with mconsole ?

Otherwise a dirty workaround could be to actually define WinMain like this:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  return main(_argc, _argv);
}

@aminya
Copy link
Author

aminya commented May 29, 2022

It seems like an issue with Catch2 + MinGW. The main function defined by Catch2 isn't working with MinGW
catchorg/Catch2#2437

@aminya
Copy link
Author

aminya commented Jun 2, 2022

This is a Catch2 issue. Found the reason:
catchorg/Catch2#2441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants