1

I'm trying to compile C with gcc in command prompt but I'm getting this undefined reference to `wWinMain' error.

I was getting undefined reference to `WinMain' first but I fixed that by adding the argument:

-municode

Now `wWinMain' is undefined. How to fix this?

C:\Development\WA\Library\Backend\C\CB\CB>gcc CB.c -lssl -lcrypto -municode
C:/Strawberry/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 
C:/Strawberry/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_w.o):crt0_w.c:(.text+0x21): undefined reference to `wWinMain'
collect2.exe: error: ld returned 1 exit status
Xayris
  • 47
  • 7
  • And of course you can easily fix that one by removing `-municode` (sorry, couldn't resist). So what is the entry point in that C? If it doesn't have `WinMain`, what does it have, `main`? Or is it a library with no entry point? –  Mar 10 '21 at 15:09
  • @dratenik Yes it is a library. As I wrote, if I remove -municode I get undefined reference to WinMain. If I add it I get undefined reference to `wWinMain'. – Xayris Mar 10 '21 at 15:12
  • What do you expect to get? With no arguments, you are asking to make an `.exe` file. If you want a `.o` object file, then you need to add `-c`, if you want a `.dll`, I have no idea off the top of my head, but I'm sure you can find the right options to use somewhere. –  Mar 10 '21 at 15:17

2 Answers2

0

gcc CB.c -lssl -lcrypto will compile and link an application, which on Windows defaults to a windowed application for which the entry point is WinMain (which your library obviously does not have nor need).

Adding the -municode just instructs the system to use the windows unicode API and then the entry point becomes wWinMain, and you still have the same problem.

To build a DLL, add -shared: gcc CB.c -shared -lssl -lcrypto

koder
  • 2,038
  • 7
  • 10
  • Ok I get my error. I need a DLL and I tried adding -shared. It works now but it's still generating an .exe – Xayris Mar 10 '21 at 15:37
  • @Xayris You might need to add an `-o` option. Please read the documentation, and perhaps some tutorial for further enlighment. – the busybee Mar 10 '21 at 15:55
  • @Xayris, .exe is the default extension. Use `-o cb.dll` to specify the output name. – koder Mar 10 '21 at 15:56
  • @koder Thanks a lot, that worked. I'll read the doc for further details. – Xayris Mar 10 '21 at 15:57
0

You didn't provide any of your code. And the problem I encountered is similar to this, so I'll describe the experience I had.

I was try out Dear ImGui library, and when I added the -municode option, the linker complained the same thing as yours. I did some searching, and found an answer to a similar question and another comment discussing a related problem, they both suggesting change "main" to "wmain". I did that, and the build was able to succeed. I'm not knowledgeable enough to provide any deeper information about the source of the problem though.

user1835157
  • 124
  • 4
  • 6