How to solve the undefined reference to `WinMain' error for links made on command line?
-
Hi everyone,
I'm doing a test. I have created an empty qmake project with Qt creator. I added a file main.cpp. A small code in main.cpp just displays a button. I want to use the created main.o file to build the obtained application in Qt Creator. For this, I make links between the main.o file and the libraries indicated in the Makefile.Debug file. I do it in command line and I use mingw_64 Version 12.2.0 (not the one in the Qt folder) to make the links.
The command I use is the following:g++ main.o -LC:\Qt\6.4.0\mingw_64\lib -lQt6Widgets -lQt6Gui -lQt6Core -lQt6EntryPoint -o executabl_test
I get the following error:
undefined reference to `WinMain'
the complete output is the following:
C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.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
I also noticed that with or without the Qt6EntryPoint library I get the same error.
I would like to know please what is the source of the problem and how to solve it. Some forums talk about this problem, but no solution is adapted to my case.I don't know much about command line compilation and compilation in general. I'm just doing a test out of curiosity.
I am on Qt6
Thanks in advance.
-
Hi everyone,
I'm doing a test. I have created an empty qmake project with Qt creator. I added a file main.cpp. A small code in main.cpp just displays a button. I want to use the created main.o file to build the obtained application in Qt Creator. For this, I make links between the main.o file and the libraries indicated in the Makefile.Debug file. I do it in command line and I use mingw_64 Version 12.2.0 (not the one in the Qt folder) to make the links.
The command I use is the following:g++ main.o -LC:\Qt\6.4.0\mingw_64\lib -lQt6Widgets -lQt6Gui -lQt6Core -lQt6EntryPoint -o executabl_test
I get the following error:
undefined reference to `WinMain'
the complete output is the following:
C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.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
I also noticed that with or without the Qt6EntryPoint library I get the same error.
I would like to know please what is the source of the problem and how to solve it. Some forums talk about this problem, but no solution is adapted to my case.I don't know much about command line compilation and compilation in general. I'm just doing a test out of curiosity.
I am on Qt6
Thanks in advance.
@Kev_ said in How to solve the undefined reference to `WinMain' error for links made on command line?:
get the following error:
undefined reference to `WinMain'Well, what does this tell you? Perhaps your main.cpp doesn't declare WinMain, but perhaps a different entry point that is more POSIX common?
Oh, and for what it's worth. Don't try to build Qt apps from command line manually (outside of makefiles). Therein lies madness.
-
@Kev_ said in How to solve the undefined reference to `WinMain' error for links made on command line?:
get the following error:
undefined reference to `WinMain'Well, what does this tell you? Perhaps your main.cpp doesn't declare WinMain, but perhaps a different entry point that is more POSIX common?
Oh, and for what it's worth. Don't try to build Qt apps from command line manually (outside of makefiles). Therein lies madness.
I would guess some defines like QT_WIDGETS_LIB and QT_CORE_LIB and others. Also the subsystem must be correctly passed to the link, don't know how this is done for g++ though.
Use qmake or cmake instead. -
Hi everyone,
I'm doing a test. I have created an empty qmake project with Qt creator. I added a file main.cpp. A small code in main.cpp just displays a button. I want to use the created main.o file to build the obtained application in Qt Creator. For this, I make links between the main.o file and the libraries indicated in the Makefile.Debug file. I do it in command line and I use mingw_64 Version 12.2.0 (not the one in the Qt folder) to make the links.
The command I use is the following:g++ main.o -LC:\Qt\6.4.0\mingw_64\lib -lQt6Widgets -lQt6Gui -lQt6Core -lQt6EntryPoint -o executabl_test
I get the following error:
undefined reference to `WinMain'
the complete output is the following:
C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/x86_64-12.2.0-release-posix-seh-rt_v10-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.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
I also noticed that with or without the Qt6EntryPoint library I get the same error.
I would like to know please what is the source of the problem and how to solve it. Some forums talk about this problem, but no solution is adapted to my case.I don't know much about command line compilation and compilation in general. I'm just doing a test out of curiosity.
I am on Qt6
Thanks in advance.
@Kev_ You are missing
-lmingw32
in the link command. The order of the libraries is important due to how they interact, so make sure this is listed before-lQt6EntryPoint
.Second thing is that you're trying to build a widgets app. By default your app will be linked using console subsystem, meaning your app will open a terminal window and then the UI window. To avoid that you should specify that you want a windows subsystem. This is done by specifying
-Wl,-subsystem,windows
in the command line.So the entire thing should look something like this:
g++ main.o -Wl,-subsystem,windows -LC:\Qt\6.4.0\mingw_64\lib -lmingw32 -lQt6Widgets -lQt6Gui -lQt6Core -lQt6EntryPoint -o executabl_test
-
@Kev_ You are missing
-lmingw32
in the link command. The order of the libraries is important due to how they interact, so make sure this is listed before-lQt6EntryPoint
.Second thing is that you're trying to build a widgets app. By default your app will be linked using console subsystem, meaning your app will open a terminal window and then the UI window. To avoid that you should specify that you want a windows subsystem. This is done by specifying
-Wl,-subsystem,windows
in the command line.So the entire thing should look something like this:
g++ main.o -Wl,-subsystem,windows -LC:\Qt\6.4.0\mingw_64\lib -lmingw32 -lQt6Widgets -lQt6Gui -lQt6Core -lQt6EntryPoint -o executabl_test
@Chris-Kawa
Hello,
thank you very much for your help. I have no more linker problem. The executable is produced. Now I just have to solve the following problem:Application failed to start because no Qt platform plugin could be initialized
I'm still trying to solve this, but if you have an idea, I would be grateful if you could share it with me.
Thanks again for your precious help.
-
-
@Chris-Kawa
Hello,
thank you very much for your help. I have no more linker problem. The executable is produced. Now I just have to solve the following problem:Application failed to start because no Qt platform plugin could be initialized
I'm still trying to solve this, but if you have an idea, I would be grateful if you could share it with me.
Thanks again for your precious help.
@Kev_ As the message says you're missing the platform plugin. It's located in your Qt installation dir in the
plugins\platforms
dir. You'll also need a style plugin fromplugins\styles
.Qt comes with a tool windeployqt that copies all needed dependencies to your executable directory. Run it with your executable as a parameter and it will do all the work for you.