trying to build a static Qt for Windows
-
@cristian-adam OK, I didn't think that was essential, but I'll try that and report back. Thanks...
-
@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.
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-adam I'm not sure I'm looking in the correct location, but:
-
@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):
- 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)
- from a command prompt, create and enter a directory for your static build (eg: c:\qt_static)
- 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
- 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.
- 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:
- 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.
- 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.
- 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.
- make sure you have all the necessary tools added to your PATH:
-
-
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 toON
.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. -
@cristian-adam thanks for the suggestion. Can you tell me where in Creator I enter these?
Thanks...
-
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
-
-
@cristian-adam thanks for the clarification. I started over (deleted everything from my static directory), added the configuration variable you cited:
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...
-
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. -
Thanks.
-