Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to solve the undefined reference to `WinMain' error for links made on command line?

How to solve the undefined reference to `WinMain' error for links made on command line?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 10.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kev_
    wrote on 27 Feb 2023, 15:28 last edited by
    #1

    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.

    K C 2 Replies Last reply 27 Feb 2023, 15:38
    0
    • K Kev_
      27 Feb 2023, 15:28

      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.

      K Offline
      K Offline
      Kent-Dorfman
      wrote on 27 Feb 2023, 15:38 last edited by Kent-Dorfman
      #2

      @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.

      C 1 Reply Last reply 27 Feb 2023, 16:28
      0
      • K Kent-Dorfman
        27 Feb 2023, 15:38

        @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.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Feb 2023, 16:28 last edited by
        #3

        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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • K Kev_
          27 Feb 2023, 15:28

          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.

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 27 Feb 2023, 23:39 last edited by
          #4

          @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
          
          K 1 Reply Last reply 2 Mar 2023, 00:50
          2
          • C Chris Kawa
            27 Feb 2023, 23:39

            @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
            
            K Offline
            K Offline
            Kev_
            wrote on 2 Mar 2023, 00:50 last edited by
            #5

            @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.

            C 1 Reply Last reply 2 Mar 2023, 03:09
            0
            • K Kev_ has marked this topic as solved on 2 Mar 2023, 00:52
            • K Kev_
              2 Mar 2023, 00:50

              @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.

              C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 2 Mar 2023, 03:09 last edited by
              #6

              @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 from plugins\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.

              1 Reply Last reply
              0

              5/6

              2 Mar 2023, 00:50

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved