LoginSignup
5
5

More than 1 year has passed since last update.

LinuxへのQt6インストール + CMakeによるビルド

Posted at

LinuxへのQt6インストール + CMakeによるビルド

LinuxにQt6をインストールした際の自分用メモです。
確認した環境は、Ubuntu 20.04 LTS (Windows10 WSL2)になります。

Qtのインストール

https://www.qt.io/download を開いてQtアカウントを作成します。

「Downloads for open source users」->「Downloads the Qt Online installer」->
「View Other Options」-> 「Qt Online Installer for Linux (64-bit)」を選択します。

qt-unified-linux-x64-<version>-online.runがダウンロードされるので、以下のコマンドでインストーラを起動します。

$ chmod u+x ./qt-unified-linux-x64-<version>-online.run
$ ./qt-unified-linux-x64-<version>-online.run

インストーラ上の操作
* 「Installation Folder」タブでは「Custom installation」を選択
* 「Select Components」タブでは「Qt 6.2.0」->「Desktop gcc 64-bit」のみを選択

Qt Creatorの起動確認

次に、Qt Creatorの起動確認を行います。
※ここでは起動確認のみで、実際のビルドはコマンドライン上で行えるようにします。

$ <install path>/Tools/QtCreator/bin/qtcreator

Qt Creatorを起動させようとしますが、エラーが発生してしまいます。

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found

調べたところ、こちらのページに解決策が記載されていました。
https://forum.qt.io/topic/93247/qt-qpa-plugin-could-not-load-the-qt-platform-plugin-xcb-in-even-though-it-was-found

$ QT_DEBUG_PLUSINS=1 <install path>/Tools/QtCreator/bin/qtcreator

を実行すると、ライブラリが見つからないとのデバッグメッセージが出力されます。

Cannot load library <install path>/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so: (libxcb-xinerama.so.0: cannot open shared object file: No such file or directory)

libxcb-xinerama0というライブラリのインストールによりエラーは発生しなくなり、GUIの起動が確認できました。

$ sudo apt update
$ sudo apt search libxcb-xinerama
$ sudo apt install libxcb-xinerama0
$ <install path>/Tools/QtCreator/bin/qtcreator

CMakeを使ったビルド

CMakeを使ってコマンドライン上でビルドできるようにします。
以下のページを参考にしています。
https://doc.qt.io/qt-6/cmake-get-started.html

CMakeのfind_pakcageでQtのパッケージを見つけられるように、.bashrcにPATHを追加しておきます。

.bashrc
export CMAKE_PREFIX_PATH=<install_path>/6.2.0/gcc_64/lib/cmake

CMakeLists.txtと簡単なサンプルプログラムを作成します。

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)

project(hello VERSION 0.0.1 LANGUAGES CXX)

set(CMAKE_VERBOSE_MAKEFILE TRUE)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt6 COMPONENTS Widgets REQUIRED)

set(CMAKE_CXX_FLAGS "\
-Werror -Wall -Wextra -pedantic -Wcast-align \
-Wcast-qual -Wconversion -Wdisabled-optimization \
-Wendif-labels -Wfloat-equal -Winit-self -Winline \
-Wlogical-op -Wmissing-include-dirs -Wnon-virtual-dtor \
-Wold-style-cast -Woverloaded-virtual -Wpacked -Wpointer-arith \
-Wredundant-decls -Wshadow -Wswitch-default -Wswitch-enum \
-Wunsafe-loop-optimizations -Wvariadic-macros \
-Wwrite-strings -Wno-missing-field-initializers"
)

add_executable(hello hello.cc)
target_link_libraries(hello PRIVATE Qt6::Widgets)
target_compile_features(hello PRIVATE cxx_std_17)
target_compile_definitions(hello PRIVATE $<$<NOT:$<CONFIG:Debug>>:NDEBUG>)
hello.cc
#include <QApplication>
#include <QLabel>

int main(int argv, char *args[])
{
    QApplication app(argv, args);
    QLabel label("Hello World!");
    label.show();

    return app.exec();
}

makeしてバイナリを実行すると、無事にGUIのウィンドウが表示されました。

$ mkdir build; cd build
$ cmake ..; make
$ ./hello
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5