Undefined reference to WinMain when trying to use wWinMain (C++ MinGW) [duplicate]
Asked Answered
F

5

33

Currently, I am trying to make a Windows application using C++. For compiling my program I use MinGW (GCC). But as soon as I use int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) instead of int main() the compiler shows me the following message:

C:/mingw-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
 collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

The example code I tried to compile I got from this Windows application tutorial:

Example Code

I have already tried reinstalling MinGW but it did not help (also I'm using Windows 10).

Floriaflorian answered 10/10, 2019 at 13:34 Comment(2)
learn.microsoft.com/en-us/windows/win32/learnwin32/…Cardigan
To anyone willing to close other question as duplicate of this: this question is about using wWinMain specifically, questions that do not mention wWinMain are not duplicates of this one.Ithunn
L
24

Newer Mingw versions support -municode linker option switching to alternate startup code allowing to use wWinMain instead of WinMain (or wmain instead of main). Add it to your command line, linker options in IDE or makefile.

g++ other_options_and_arguments -municode

Older versions do not support this option.

One thing to note is that Visual C++ supports a “wWinMain” entry point where the “lpCmdLine” parameter is a “LPWSTR”. You would typically use the “_tWinMain” preprocessor definition for your entry point and declare “LPTSTR lpCmdLine” so that you can easily support both ANSI and Unicode builds. However, the MinGW CRT startup library does not support wWinMain, so you’ll have to stick with the standard “WinMain” and use “GetCommandLine()” if you need to access command line arguments.

via Building Win32 GUI Applications with MinGW

In this specific case, you can use WinMain instead. This program doesn't use pCmdLine value, so it should compile when you change wWinMain to WinMain and PWSTR pCmdLine to PSTR pCmdLine.

If you later would need unicode command line use LPWSTR cmd_line = GetCommandLineW(); instead of WinMain argument.

Languid answered 11/10, 2019 at 7:38 Comment(0)
L
16

The solution is to add -municode to the compilation parameters, as mentioned by @ssbssa in a comment. (In the meantime, the accepted answer has been updated, and confirms this).

Example:

g++ helloworld3.cpp -o helloworld3 -Wl,-subsystem,windows -municode

See https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/

While it is not necessary to define _UNICODE or UNICODE to compile the above code, -municode is needed for linking because it uses wmain() instead of the traditional main().

Lamella answered 20/6, 2020 at 17:2 Comment(1)
"-municode" is actually a parameter to the linker, "ld.exe" ?Assemble
G
4

undefined reference to `WinMain'

It tries to find WinMain and failed. So you need use WinMain instead of wWinMain.

Another possible issue is

error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) ^~~~~~~ In file included from c:\mingw\include\windows.h:44:0, from test.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);

So you need use LPSTR instead of PWSTR.

Then the entry point will like this:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)

Above is ANSI version entry point.

Gefell answered 11/10, 2019 at 2:35 Comment(1)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) you forgot hPrevInstanceAndantino
D
3

I use mingw-w64 for Windows 10 (64 bit).

If you use this win32 example:

Change

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow);

to

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)

then compile it with

gcc -O2 -Iinclude -std=c99 -D UNICODE -D _win32_IE=0x0500 -D WINVER=0x0500 hw.c -s  -Wl,--subsystem,windows -municode  -lcomctl32 -D WIN_32_LEAN_AND_MEAN -c 

and link it with:

gcc hw.o
Deafening answered 22/1, 2021 at 12:48 Comment(0)
A
-1

I was using vscode and answer to my problem was :

  1. open setting
  2. search save
  3. enable save file before run

terminal will work properly

Ava answered 3/4, 2022 at 16:53 Comment(4)
What is the relation between this answer and that question?Calumet
I was getting error while running CPP file on vscode .Ava
The question is about an error in compiling a c++ code. There is nothing to do with the autosave feature of some editors.Calumet
No, he is right to an extent, the aforementioned error often appears when the file has not been saved so you are compiling an empty file. The autosave feature means this is will not ocurr.Scofield

© 2022 - 2024 — McMap. All rights reserved.