#undefined reference to `WinMain'

40 messages · Page 1 of 1 (latest)

safe heart
#

I am relatively new to C++ and i use CLion. I have been trying to get the wWinMain entry point to work for hours. i used chatgpt, researched on google, I have done everything I can. No matter what I try, its always this exact error. "undefined reference to WinMain ". I simply pasted the sample code from https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program but it just doesnt work. Many people with the exact same problem on stack overflow said they had to add "-municode" which i did. i did add -municode. I made sure my CMake.txt file was set up well, i restarted my pc, reinstalled my compiler, restarted my pc several times, started all over from scratch, i did everything, really.

I am on windows 11, using CLion, MinGW 64. Apparently its a problem with MinGW? At least thats what the dozens of stackoverflow posts ive been desperately reading say. however, i couldnt find a solution on any of them, neither could chatpgt. at least not one that works. i am not that deep into computers, compilers, linking and everything. simply setting up the library "raylib" and getting it to work was a long battle already. but this one seems impossible to defeat by myself.

Full error code:

C:\WINDOWS\system32\cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.5\bin\mingw\bin\G__~1.EXE -g -mwindows CMakeFiles/CppTests.dir/main.cpp.obj -o CppTests.exe -Wl,--out-implib,libCppTests.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2024.1.5\bin\mingw\bin/ld.exe: C:/Program Files/JetBrains/CLion 2024.1.5/bin/mingw/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text+0x130): undefined reference to WinMain'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Describes how to make a very basic Windows desktop program, which will create and show a blank window.

random fiberBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ebon vapor
#

I don't see -municode there?

safe heart
#

well, how do i add it?

#

stack overflow experts were talking about commandlines, but i use, as i said, CLion

#

so i asked chatgpt

ebon vapor
#

Don't use ChatGPT.

safe heart
#

which told me that i had to add it to the CMake.txt file

#

oop

#

well how do i add it then? this is my CMake.txt file:

cmake_minimum_required(VERSION 3.28)
project(CppTests)

set(CMAKE_CXX_STANDARD 26)

add_executable(CppTests WIN32 main.cpp)

target_compile_options(CppTests PRIVATE -municode)
#

tried it with as well as without the last line

ebon vapor
#

Show the output with the last line.

safe heart
#
"C:\Program Files\JetBrains\CLion 2024.1.5\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\gotta\CLionProjects\CppTests\cmake-build-debug --target all -j 10
[1/2] Building CXX object CMakeFiles/CppTests.dir/main.cpp.obj
[2/2] Linking CXX executable CppTests.exe
FAILED: CppTests.exe 
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.5\bin\mingw\bin\G__~1.EXE -g -mwindows CMakeFiles/CppTests.dir/main.cpp.obj -o CppTests.exe -Wl,--out-implib,libCppTests.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2024.1.5\bin\mingw\bin/ld.exe: C:/Program Files/JetBrains/CLion 2024.1.5/bin/mingw/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text+0x130): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.```
ebon vapor
#

-municode doesn't appear to be actually passed.

safe heart
#

well, how do i pass it then?
i just got into c++, i dont know how all this linking and stuff works. not at all.

#

ive read about using it as a command line argument but i dont use a command line and would rather stick with my IDE, i doubt that using the terminal is the only way to achieve this

ebon vapor
#

OK I just recalled that is a link option, rather.

safe heart
#
cmake_minimum_required(VERSION 3.28)
project(CppTests)

set(CMAKE_CXX_STANDARD 26)

add_executable(CppTests WIN32 main.cpp)

add_link_options(-municode)
#

made sure to clean and rebuild, as well as to reload my CMake file

#

still getting the exact same error

ebon vapor
#

That's not how you specify link options to the target.

safe heart
#

well, how do i?

ebon vapor
#

target_link_options.

safe heart
#

CMake Error at CMakeLists.txt:8 (target_link_options):
target_link_options called with incorrect number of arguments

#

am i not using it correctly?

ebon vapor
#

Indeed.

#

Maybe you should go back and learn some CMake basics properly.

safe heart
#

my bad.

#

well it just worked lmao

#

thank you man

#

im not that deep into coding im sorry, im just a student spending his freetime playing around with some code really.

#

came from java where everything was much easier, without linking and all that stuff

#

discovered C, seemed fun, decided to go with C++, pow 💥

#

didnt expect it to be that complicated lol.

#

well anyways

#

thanks for helping me out

#

!solved

random fiberBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

ebon vapor
#

Would be the same in C.