Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. trying to build a static Qt for Windows
QtWS25 Last Chance

trying to build a static Qt for Windows

Scheduled Pinned Locked Moved Solved Installation and Deployment
18 Posts 3 Posters 4.1k 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.
  • cristian-adamC cristian-adam

    @mzimmers

    The officially supported CMake generator for building Qt is Ninja. You are
    using: 'MinGW Makefiles' instead. Thus, you might encounter issues. Use
    at your own risk

    Make sure you have Ninja in path.

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #8

    @cristian-adam OK, I added Ninja to my system and my path, and that did seem to make a difference. The configuration completed with only a couple warnings, and the build and installs looks successful.

    Is there a way to determine that this is truly a static build? I ask because when I use it, my executable still looks for some .dll files.image_480.png

    I'm not sure whether this is because my Qt build isn't static, or because I'm building my application incorrectly.

    Any suggestions for isolating the problem here?

    cristian-adamC 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @cristian-adam OK, I added Ninja to my system and my path, and that did seem to make a difference. The configuration completed with only a couple warnings, and the build and installs looks successful.

      Is there a way to determine that this is truly a static build? I ask because when I use it, my executable still looks for some .dll files.image_480.png

      I'm not sure whether this is because my Qt build isn't static, or because I'm building my application incorrectly.

      Any suggestions for isolating the problem here?

      cristian-adamC Offline
      cristian-adamC Offline
      cristian-adam
      wrote on last edited by
      #9

      @mzimmers does your qt6 build have a Qt6Location.dll?

      If so, it means that QtLocation didn't get the memo that it's doing a static build ...

      mzimmersM 1 Reply Last reply
      1
      • cristian-adamC cristian-adam

        @mzimmers does your qt6 build have a Qt6Location.dll?

        If so, it means that QtLocation didn't get the memo that it's doing a static build ...

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #10

        @cristian-adam I'm not sure I'm looking in the correct location, but:
        Screenshot 2024-05-01 105212.png

        cristian-adamC 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @cristian-adam I'm not sure I'm looking in the correct location, but:
          Screenshot 2024-05-01 105212.png

          cristian-adamC Offline
          cristian-adamC Offline
          cristian-adam
          wrote on last edited by
          #11

          @mzimmers Then try building a Qt Location example with a Kit that has your static Qt. And see how that works out.

          mzimmersM 1 Reply Last reply
          1
          • cristian-adamC cristian-adam

            @mzimmers Then try building a Qt Location example with a Kit that has your static Qt. And see how that works out.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by mzimmers
            #12

            @cristian-adam if I understand you, that's what I just did (my application does use QGeoLocation).

            Or, are you suggesting I try a different application that also uses Qt Location?

            EDIT:

            Per @cristian-adam 's suggestion, I used one of the supplied Qt applications, and it built. On a hunch, I completely deleted the build directory for my application (doing a clean from Creator was not sufficient), and rebuilt. My executable is now ~20x the size of the prior version. Once I'm done uploading, I'll test it and report back.

            EDIT 2:

            It seems to work! Here are the steps I took (in summary form):

            1. make sure you have all the necessary tools added to your PATH:
              • your toolchain (eg: C:\qt\tools\mingw1120_64\bin)
              • Ninja (eg: C:\Qt\Tools\Ninja)
            2. from a command prompt, create and enter a directory for your static build (eg: c:\qt_static)
            3. run a configure command with these options (you might add more options, such as some "-skip"s to your configuration):
                c:\Qt\6.5.3\Src\configure.bat -prefix c:\qt_static -static -static-runtime -release -Wno-dev 
            
            1. if anything goes wrong with your configuration, or if you want to change something, delete the entire contents of your directory created in step 2. As an alternative, you can try @SGaist's suggestion of the "-redo" option, but read up on it before using; its behavior may surprise you.
            2. run:
                cmake --build . --parallel
                cmake --install .
            

            If all went well, you have now successfully built and installed a static Qt version. To use this static version, perform the following steps:

            1. from Creator -> Preferences -> Kits -> Qt Versions, manually add the version you just created. Reference the qmake6.exe file in the bin directory of your new Qt version. Apply this change.
            2. now go to the Kits tab, and create a new kit, named whatever you like. In the line "Qt version:" select the version you entered in the previous step. Double-check that the compilers and debugger are the correct versions for your kit. Apply and hit OK.
            3. choose your newly-created kit for use with your application. Do this from the Projects->Manage Kits... button. Once you've enabled your new kit, you can select it from the icon in the leftmost column of Creator (I don't know what that icon is called).

            That should do it. When building static executables, be prepared for a huge jump in file size (mine is on the order of 20X). Also, I'd recommend you not try to build a static debug image (they get insanely large); stick to release.

            If anyone has any corrections to this list, please let me know. Thanks to everyone for the help on this.

            1 Reply Last reply
            2
            • mzimmersM mzimmers has marked this topic as solved on
            • cristian-adamC Offline
              cristian-adamC Offline
              cristian-adam
              wrote on last edited by
              #13

              If the size of the final application is an issue, you can enable for Qt's configure the -ltcg (link time code generation) option!

              Then for your application set the CMAKE_INTERPROCEDURAL_OPTIMIZATION CMake variable to ON.

              This way the linker can have a look at the whole application and its Qt dependencies and create the most optimal version of the executable.

              See https://www.qt.io/blog/2019/01/02/qt-applications-lto for more information.

              Note that not all applications work with LTO and it might be that the MinGW linker can fail or take a long time.

              mzimmersM 1 Reply Last reply
              1
              • cristian-adamC cristian-adam

                If the size of the final application is an issue, you can enable for Qt's configure the -ltcg (link time code generation) option!

                Then for your application set the CMAKE_INTERPROCEDURAL_OPTIMIZATION CMake variable to ON.

                This way the linker can have a look at the whole application and its Qt dependencies and create the most optimal version of the executable.

                See https://www.qt.io/blog/2019/01/02/qt-applications-lto for more information.

                Note that not all applications work with LTO and it might be that the MinGW linker can fail or take a long time.

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #14

                @cristian-adam thanks for the suggestion. Can you tell me where in Creator I enter these?

                Thanks...

                1 Reply Last reply
                0
                • cristian-adamC Offline
                  cristian-adamC Offline
                  cristian-adam
                  wrote on last edited by
                  #15

                  First is for Qt's configure.bat call:

                  $ c:\Qt\6.5.3\Src\configure.bat -prefix c:\qt_static -ltcg -static -static-runtime -release -Wno-dev
                  

                  Second is for your application's CMake parameters as seen at https://doc.qt.io/qtcreator/creator-build-settings-cmake.html#current-configuration

                  alt text

                  mzimmersM 1 Reply Last reply
                  2
                  • cristian-adamC cristian-adam referenced this topic on
                  • cristian-adamC cristian-adam

                    First is for Qt's configure.bat call:

                    $ c:\Qt\6.5.3\Src\configure.bat -prefix c:\qt_static -ltcg -static -static-runtime -release -Wno-dev
                    

                    Second is for your application's CMake parameters as seen at https://doc.qt.io/qtcreator/creator-build-settings-cmake.html#current-configuration

                    alt text

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by mzimmers
                    #16

                    @cristian-adam thanks for the clarification. I started over (deleted everything from my static directory), added the configuration variable you cited:

                    Screenshot 2024-05-02 075444.png
                    and ran this command:

                    c:\Qt\6.5.3\Src\configure.bat -prefix c:\qt_static -ltcg -static -static-runtime -release -skip qt3d -skip qt5compat -skip qtactiveqt -skip qtcharts -skip qtdatavis3d -skip qtdoc -skip qtgrpc -skip qtinsighttracker -skip qtmultimedia -skip qtquick3d -skip qtquick3dphysics -skip qtquicktimeline -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebview -Wno-dev 
                    

                    The configure returned successfully (though it did have some small errors), but the build command failed here:

                    [579/7750] Linking CXX executable qtbase\bin\cmake_automoc_parser.exe
                    FAILED: qtbase/bin/cmake_automoc_parser.exe
                    C:\WINDOWS\system32\cmd.exe /C "cd . && C:\Qt\Tools\mingw1120_64\bin\c++.exe -DNDEBUG -O2 -flto=auto -fno-fat-lto-objects -static -Wl,--gc-sections qtbase/src/tools/cmake_automoc_parser/CMakeFiles/cmake_automoc_parser.dir/cmake_automoc_parser_resource.rc.obj qtbase/src/tools/cmake_automoc_parser/CMakeFiles/cmake_automoc_parser.dir/main.cpp.obj qtbase/src/tools/cmake_automoc_parser/CMakeFiles/cmake_automoc_parser.dir/cmake_automoc_parser_longpath.rc.obj -o qtbase\bin\cmake_automoc_parser.exe -Wl,--out-implib,qtbase\src\tools\cmake_automoc_parser\libcmake_automoc_parser.dll.a -Wl,--major-image-version,0,--minor-image-version,0  qtbase/src/tools/bootstrap/libBootstrap.a  -ladvapi32  -lnetapi32  -lole32  -lshell32  -luser32  -luuid  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
                    C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: qtbase/src/tools/cmake_automoc_parser/CMakeFiles/cmake_automoc_parser.dir/main.cpp.obj: plugin needed to handle lto object
                    C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.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
                    [608/7750] Building CXX object qtbase/src/3rdparty/harfbuzz-ng/CMakeFiles/BundledHarfbuzz.dir/src/hb-subset.cc.obj
                    ninja: build stopped: subcommand failed.
                    

                    Any idea what might be going on here?

                    Also, the configuration summary shows that I'm including language support for all available languages. I'd like to get rid of most of these. Do you know the option for this?

                    Thanks...

                    1 Reply Last reply
                    0
                    • cristian-adamC Offline
                      cristian-adamC Offline
                      cristian-adam
                      wrote on last edited by
                      #17

                      This looks like a bug in Qt. I'm not sure if -ltcg is tested regularly. Please do create a bug report at https://bugreports.qt.io/.

                      You can then only test CMAKE_INTERPROCEDURAL_OPTIMIZATION with your application.

                      mzimmersM 1 Reply Last reply
                      0
                      • cristian-adamC cristian-adam

                        This looks like a bug in Qt. I'm not sure if -ltcg is tested regularly. Please do create a bug report at https://bugreports.qt.io/.

                        You can then only test CMAKE_INTERPROCEDURAL_OPTIMIZATION with your application.

                        mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #18

                        @cristian-adam submitted.

                        Thanks.

                        1 Reply Last reply
                        1
                        • hskoglundH hskoglund referenced this topic on

                        • Login

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