To build Qt 6.7 static:
-----------------------------------------

To build Qt 6.7 static:

follow these steps:

  • Ensure, during the environment setup, that you download version 6.7, including the source files and the Mingw 11.2 compiler. Place them in a specific path, for example: C:\Qt\Tools\mingw1120_64\bin, and ensure to add it to the Environment/System Variables/Path along with other necessary links for Qt to access the G++ or C++ compilers.
  • After installation, navigate to the QT folder on your device, typically located in Drive C on Windows, or wherever it was installed on your system. Copy the source files into the Qt folder within the C:\Qt\6.7.0 directory to preserve the original version in case of any issues.
  • Create a folder within Qt named "build-static" or similar, parallel to the source files, all within the Qt directory.
  • Ensure that all these tools installed and works in your OS:

  1. Windows 10
  2. Ninja 1.12.0
  3. Node.js 20.12.2
  4. gcc (x86_64-posix-seh-rev3, Built by MinGW-W64 project) 11.2.0
  5. ruby 3.2.3 (2024-01-18 revision 52bb2ac0a6) [x64-mingw-ucrt]
  6. perl v5.38.2
  7. cmake version 3.29.2
  8. python 3.12.3

  1. Access the Src folder within the Qt directory using PowerShell or Bash, and execute the following command:

C:\QT\Src>./configure -static -prefix "c:/Qt/build-static"        

  • If everything is set up correctly, this command will configure the source files for compilation, which may take around 50 seconds.

CL\QT\Src>cmake --build . --parallel        

Specify the number of parallel compilations after "--parallel". This process may take a long time as approximately 12,000 source files will be compiled.

  • Once finished successfully, execute the following command in PowerShell:

CL\QT\Src>cmake --install .        

This command will generate the final version of the necessary files for static linking and place them in the "build-static" folder.

  • Launch Qt Creator, go to Edit/Preferences/Qt Versions.
  • Click "Add" and navigate to the qmake.exe file located in c:\QT\build-static\bin, then select qmake.exe from there.
  • After completing the above steps, select the Kits tab and clone the main tool for default dynamic linking, such as "Desktop Qt %{Qt:Version} MinGW 64-bit".
  • A new item will appear under the Manual category, name it, for example, "static build".
  • If the previous steps were successful, the Qt Creator environment is now ready for static linking.
  • Create a new project, for example, "widgets".
  • During project creation, choose the "static build" option created in the previous steps, and select CMake instead of qmake, which has been recently adopted by Qt Company.
  • Proceed with the compilation process as usual. If the compilation completes successfully, a folder parallel to the source files folder will be created. Navigate to that folder, and you will find an executable file sized over 20 MB, containing all the necessary libraries for running without dynamic runtime files.
  • Test the program on a system without Qt installed. If it runs without any issues, it confirms that the program operates independently of your device's environment.
  • If prompted for some files such as libgcc, libstdc++, when running on a new system, add the following two lines to the CMakelists.txt file within the project under the set group, on separate lines:

set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CSS_STANDARD_LIBRARIES}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")        

Then, recompile and transfer the program to the new environment, and the program will run without any issues.

Please note that for this process, you must have a commercial license for distributing statically linked applications as per Qt's terms of use.


https://forum.qt.io/topic/156200/qt-6-7-0-static-build-mingw

Sergei Nikanorov

Industrial Automation Engineer

2 个月

Thank you for this guide! Some additional comments: 1. I build Qt without installing Ruby and Perl, using default MinGW which install with Qt 2. Also I had standalone MinGW and it couldn't be build until I delete it. 3. If you want to use some shared libraries in addition to static linked application you should add flags for the linker like here: ---- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive -Wl,-Bdynamic -lsnap7") ---- here I use dynamic linked library snap7.dll in addition to static linked standard libraries.

Bernd Homberg

Gründer & CEO bei Innovirtual Software / wir erstellen automatisiert visuelle Software Dokumentation nach dem C4-Modell ??

6 个月

Thank you Ayman Alheraki for this excellent and helpful article! ?? ??

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了