Cross-compile Qt – GUI Windows Application in Ubuntu 18.04 MinGW environment

I want to create a GUI application for Windows and Linux environments, so this time I will compile and develop a QT framework that supports cross-platform in the Ubuntu environment, and try the cross-compilation method that runs in the Windows environment.

environment

MinGW Install the Windows compiler mingw-w64 in a Linux environment.

sudo apt-get install binutils-mingw-w64-x86-64
sudo apt-get install mingw-w64-common
sudo apt-get install mingw-w64-x86-64-dev
sudo apt-get install mingw-w64-tools
sudo apt-get install gcc-mingw-w64-base
sudo apt-get install gcc-mingw-w64-x86-64
sudo apt-get install g++-mingw-w64-x86-64

Use Win64 + pthread version (posix)

update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix

Qt Source Get the top repository

PKG=qt5 VER=5.13.2 TAG=v$VER
QTDIR=~/Qt
mkdir -p "$QTDIR/$VER/mingw_64"
mkdir -p "$QTDIR/$VER/src"
cd "$QTDIR/$VER/src"

git clone --depth 1 git://code.qt.io/qt/qt5.git $PKG
cd $PKG
git fetch --depth 1 origin tag $TAG
git checkout -f $TAG

Get submodule Qt5 is made up of a large number of submodules. Since it takes time to download the entire history of submodules, This time we will download only the qtbase submodules needed for development with Qt Widgets.

for module in qtbase; do
  git submodule update --init --depth 1 --progress $module
done

git clean -dfX
git submodule foreach --recursive git clean -dfX

Configuring Qt Cross-compile Qt for Windows environment.

./configure \
-xplatform win32-g++ \
-device-option CROSS_COMPILE=/usr/bin/x86_64-w64-mingw32- \
--prefix="$QTDIR/$VER/mingw_64" \
-opensource \
-confirm-license \
-developer-build \
-no-compile-examples \
-nomake examples \
-opengl desktop \
-L/usr/x86_64-w64-mingw32/lib \
QMAKE_LIBS+="-lws2_32"

Make Qt Since the compilation time is long, compile in parallel with the number of CPU slaves. If the compilation ends abnormally due to lack of PC memory, reduce the number of parallel compilations and compile. "When compiling in a 16-slade environment: memory 8G, takes about 10 minutes"

J=$(grep -cs '^processor' /proc/cpuinfo || echo 2)
echo $J
make -j $J

Installation Qt

J=$(grep -cs '^processor' /proc/cpuinfo || echo 2)
echo $J
make -j $J install

Qt Creator

MinGW compiler settings

Add the MinGW compiler in Qt Creator. Add MinGW C and C ++ compilers from [Tools] [Options] [Kits] [Compiler] [Add] [MinGW].

image.png

Qt version settings

Add "QTDIR/VER/mingw_64/bin/qmake" built and installed from [Tools] [Options] [Kits] [Qt version] [Add].

image.png

Development Kit settings

Add from [Tools] [Options] [Kits] [Kits] [Add]. Compiler: Select the added MinGW compiler Qt version: Select the added MinGW Qt version Make the added MinGW Qt kit the default

image.png

Creating a Qt Application

New application

Create from [File] [New Project].

image.png

Enter the name and location of the application

image.png

Select the added MinGW Qt kit and enter the build location

image.png

Use the default setting for class information

image.png

Complete the settings

image.png

image.png

image.png

Edit GUI screen

Add button to GUI Double-click Forms/mainwindow.ui to open the Forms edit screen Drag and drop [Buttons] [Push Button] onto the Form

image.png

Build Qt Application

Build Qt Application from [Build] [Rebuild Project].

image.png

image.png

Deploy Qt Application to Windows environment

Copy the built qt_gui_app.exe, MinGW DLL, and Qt DLL to Windows.

Original: /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/libgcc_s_seh-1.dll /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/libstdc++-6.dll /usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll Copy destination: Same folder as exe

Copy source: QTDIR/VER/mingw_64/bin Qt5Core.dll、Qt5Gui.dll、Qt5Widgets.dll Copy destination: Same folder as exe

Copy source: QTDIR/VER/mingw_64/plugins/platforms qwindows.dll Copy to: platforms subfolder of exe folder

image.png

Run Qt Application in Windows environment

Execute * .exe by double-clicking

image.png

Recommended Posts

Cross-compile Qt – GUI Windows Application in Ubuntu 18.04 MinGW environment
Beginners use ubuntu in windows to prepare rails environment
[Windows] WSL2 + Ubuntu + Node.js environment construction
WSL2 + Ubuntu 20.04 Install Vivado in LTS environment
Use docker in proxy environment on ubuntu 20.04.1
Run static analysis Infer in Windows environment
Java application development environment created in VM environment
Self-hosting with Docker of AuteMuteUs in Windows environment
Create ubuntu20.04 (or windows10) + anaconda + pytorch or tensorflow environment
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker