インストール: fcitx-qt5 | LinuxのQtとQtCreatorで日本語入力を実現

UbuntuでQt5とQtCreatorでアプリケーションを開発している。UbuntuのパッケージマネージャーのAPTでインストールできるものはバージョンが古いので,公式サイトのインストーラーからダウンロードしたものを使っている。

最新のバージョンのQtが使えて満足していたのだが,インストーラーからインストールした場合,QtCreatorで日本語が入力できず,このQtCreatorで作成したアプリケーションでも日本語を入力できないという問題があった。UbuntuでのIMEにはfcitxとmozcの組み合わせを使っており,日本語に入力を切り替えようとしても切り替えられなかった。

今後の開発に大きな影響が出るためこの問題の対応方法を調べ,無事に解決できたのでその手順を記す。

この問題の対処方法は以下のページを参考にした。

参照元

どうやら,インストーラーからインストールした場合,plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.soが欠如しているため,IMEの切り替えが機能しないらしい。

そこで,自分でlibfcitxplatforminputcontextplugin.soを含んでいるfcitx-qt5をソースコードからビルドして,インストールすることで対応する。

インストール手順

インストール情報
項目説明
配布元Fcitx – Fcitx
リポジトリー
手順
依存情報CMakeLists.txt · master · fcitx / fcitx-qt5 · GitLab
依存先 (必須)ECM 1.4.0+, Qt5 5.1.0+, Qt5Gui 5.1.0+, XKBCommon 0.5.0+
依存先 (任意)ENABLE_LIBRARY: Fcitx 4.2.8+, LibIntl
依存元Qt5, QtCreator
インストール手順
sh -eux <<-"EOT"
PKG=fcitx-qt5 VER=1.2.3 TAG=$VER
LOCAL=~/.local J=$(grep -cs '^processor' /proc/cpuinfo || echo 2) mkdir -p "$LOCAL/src" cd "$LOCAL/src" if command -v git >/dev/null; then [ -e $PKG ] || git clone --depth 1 https://gitlab.com/fcitx/$PKG cd $PKG git fetch --depth 1 origin tag $TAG git checkout -f $TAG git clean -dfX else [ -e $PKG-$VER ] || wget https://download.fcitx-im.org/fcitx-qt5/$PKG-$VER.tar.xz tar -xf $PKG-$VER.* cd $PKG-$VER make -kj $J distclean clean || : fi ENABLE_LIBRARY=$(pkg-config --exists fcitx icu-uc && echo ON || echo OFF) cmake -D CMAKE_INSTALL_PREFIX="$LOCAL/stow/$PKG-$VER" -D ENABLE_LIBRARY=$ENABLE_LIBRARY . make -j $J make -j $J install # cp platforminputcontext/libfcitxplatforminputcontextplugin.so \ # $LOCAL/opt/stow/Qt/Tools/QtCreator/lib/Qt/plugins/platforminputcontexts/ cd "$LOCAL/stow" echo $PKG-[0-9]* | xargs -n 1 stow --ignore=dir -D stow --ignore=dir $PKG-$VER EOT
インストール例
日付バージョンOS依存関係
2018-05-061.2.2Ubuntu 16.04
2019-06-151.2.3Ubuntu 18.04GNU Make 4.2.1, GCC 7.4.0, CMake 3.10.2, ECM 5.56.0, XKBCommon 0.8.4, Qt 5.11.1

Fcitxのプロジェクトのリポジトリーは,GithubからGitlabに移行したようだ。

fcitx-qt5にはctestやmake check, make testが存在せず,ビルド結果を保証できないので,扱いには注意する。

デフォルトでは,ENABLE_LIBRARY=ONとなっており,ビルドにFcitxとLibIntlが必要となる。しかし,QtやQtCreatorでのFcitxによるIMEの有効化にはなくても問題ないので,pkg-configコマンドで判定し,これらのライブラリーが存在しない場合はcmakeのビルドオプションを無効にしている。

QtとQtCreatorのFcitxによるIMEの有効化

ビルドが成功するとplatforminputcontext/libfcitxplatforminputcontextplugin.soが生成される。このファイルがQtでのFcitxの有効化に必要なライブラリーだ。

このファイルはmake installを実行しても$LOCAL/stow/fcitx-qt5-$VERディレクトリーにはインストールされないようだ。その代わりに,以下のメッセージが表示されQt5 (gcc_64/plugins/platforminputcontexts/) には自動的にインストールされる。

-- Installing: /home/senooken/.local/opt/stow/Qt/5.11.1/gcc_64/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so
-- Set runtime path of "/home/senooken/.local/opt/stow/Qt/5.11.1/gcc_64/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so" to ""

QtCreatorにはインストールされないので,手動でインストールする。以下のようにQtのインストール先のTools/QtCreator/lib/Qt/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.soを配置すればよい。

cp platforminputcontext/libfcitxplatforminputcontextplugin.so \
$LOCAL/opt/stow/Qt/Tools/QtCreator/lib/Qt/plugins/platforminputcontexts/

これでQtやQtCreatorでFcitxによるIME切り替えができるようになる。

QtやQtCreatorのバージョンを上げた場合などは,再ビルド・インストールが必要になるので注意する。

ビルド時の環境変数

ビルドにはQt5が必須であり,PATHとLD_LIBRARY_PATHの環境変数も必須となる。fcitx-qt5のビルド前に,以下のようなコマンドで,Qt5のインストール先 (例: $HOME/.local/opt/stow/Qt/5.11.1/) に環境変数を設定しておく。~/.profile~/.bashrcにも設定しておくとよいだろう。

## Qt
QT_HOME="$HOME/.local/opt/stow/Qt/5.11.1/gcc_64"
if [ -d "$QT_HOME" ]; then
  export QT_HOME
export PATH="$QT_HOME/bin:$PATH" export CPATH="$QT_HOME/include:$CPATH" export LD_LIBRARY_PATH="$QT_HOME/lib:$LD_LIBRARY_PATH" export LIBRARY_PATH="$LD_LIBRARY_PATH" export PKG_CONFIG_PATH="$QT_HOME/lib/pkgconfig:$PKG_CONFIG_PATH" fi

ネット上の情報では,cmake実行時に-D CMAKE_PREFIX_PATHを指定するように書いてあるものがあったが,これは本質ではない。

結局のところ,仮に-D CMAKE_PREFIX_PATHを指定していても,ビルド時にQt5のmocやライブラリーを参照するので,PATH環境変数とLD_LIBRARY_PATH環境変数が設定されていないとビルドエラーとなる。

具体的には,PATH環境変数にQt5が設定されていないと,make実行時に以下のエラーが出る。

[ 70%] Automatic MOC for target fcitxplatforminputcontextplugin

AutoMoc subprocess error
------------------------
moc failed for
  "/home/senooken/.local/src/fcitx-qt5/platforminputcontext/main.h"

Command ------- /usr/lib/qt5/bin/moc -I/home/senooken/.local/src/fcitx-qt5/platforminputcontext/fcitxplatforminputcontextplugin_autogen/include -I/usr/include/x86_64-linux-gnu/qt5/QtGui/5.9.5 -I/usr/include/x86_64-linux-gnu/qt5/QtGui/5.9.5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtCore/5.9.5 -I/usr/include/x86_64-linux-gnu/qt5/QtCore/5.9.5/QtCore -I/home/senooken/.local/src/fcitx-qt5/platforminputcontext -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtDBus -I/home/senooken/.local/stow/libxkbcommon-0.8.4/include -I/usr/include -DQT_CORE_LIB -DQT_DBUS_LIB -DQT_GUI_LIB -DQT_NO_DEBUG --include /home/senooken/.local/src/fcitx-qt5/platforminputcontext/fcitxplatforminputcontextplugin_autogen/moc_predefs.h -o /home/senooken/.local/src/fcitx-qt5/platforminputcontext/fcitxplatforminputcontextplugin_autogen/EWIEGA46WW/moc_main.cpp /home/senooken/.local/src/fcitx-qt5/platforminputcontext/main.h Output ------ /home/senooken/.local/src/fcitx-qt5/platforminputcontext/main.h:32: Parse error at "IID" make[2]: *** [platforminputcontext/CMakeFiles/fcitxplatforminputcontextplugin_autogen.dir/build.make:58: platforminputcontext/CMakeFiles/fcitxplatforminputcontextplugin_autogen] Error 1 make[1]: *** [CMakeFiles/Makefile2:964: platforminputcontext/CMakeFiles/fcitxplatforminputcontextplugin_autogen.dir/all] Error 2 make: *** [Makefile:130: all] Error 2

どうやら,Qt5のmocコマンドを使っているためのようだ。

同様に,LD_LIBRARY_PATH環境変数が設定されていないと,make実行時に以下のエラーが出る。

[ 29%] Generating ui_mainwindow.h
/home/senooken/.local/opt/Qt/5.11.1/gcc_64/bin/uic: relocation error: /home/senooken/.local/opt/Qt/5.11.1/gcc_64/bin/uic: symbol _ZdlPvm version Qt_5 not defined in file libQt5Core.so.5 with link time reference
make[2]: *** [guiwrapper/CMakeFiles/fcitx-qt5-gui-wrapper.dir/build.make:62: guiwrapper/ui_mainwindow.h] Error 127
make[2]: *** Deleting file 'guiwrapper/ui_mainwindow.h'
make[1]: *** [CMakeFiles/Makefile2:594: guiwrapper/CMakeFiles/fcitx-qt5-gui-wrapper.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

そのため,PATH環境変数とLD_LIBRARY_PATH環境変数はビルド時に設定が必要だ。

インストール: fcitx-qt5 | LinuxのQtとQtCreatorで日本語入力を実現” に対して5件のコメントがあります。

  1. psan より:

    Please help

    cmake is giving me below error

    fcitx-qt5/platforminputcontext/qtkey.cpp:25:28: fatal error: X11/XF86keysym.h: No such file or directory

    How to resolve. any help is appreciated.

    1. senooken より:

      Hi psan. Thanks for comment.

      I think you will need to X11 related library.

      I will try to investigate this error in this weekend. Could you give me some information?

      For example, OS (uname -a), compiler version (gcc --version).

      1. psan より:

        Hi
        Thank you for quick reply.
        OS is UBUNTU 16.4 LTS (uname -a : 4.4.0.31 generic #50-Ubuntu x86_64)

        I resolved above error by installing below libs
        sudo apt-get install libxkbcommon-dev
        sudo apt-get install xorg

        Now I am able to build fcitx-qt5 and copied it to required folders.

        If I open Qtcreator in normal mode I am able to input Japanese characters.
        But If I open QTCreator in sudo mode I am still not able to input Japanese characters.
        sudo モードでQTCreatorを開くと日本語入力出来ない事。sudoモード以外わ出来ます。

        I am in big trouble 🙂

        1. senooken より:

          I am glad to success compiling fcitx-qt5!

          This article describe how to install fcitx-qt5 in local user. If you want to install global user (admin, super user, sudo), please change or remove -DCMAKE_INSTALL_PREFIX.

          # cmake .. -DCMAKE_INSTALL_PREFIX=$LOCAL/stow/$PKG-$VER -DCMAKE_PREFIX_PATH=$LOCAL/opt/Qt/Qt5.10.0/5.10.0/gcc_64
          cmake  -DCMAKE_PREFIX_PATH=$LOCAL/opt/Qt/Qt5.10.0/5.10.0/gcc_64 ..

          Also if you want to install fcitx-qt5 in local user and run as sudo, you need to inherit user environmental variable (LD_LIBRARY_PATH) by following command.

          sudo -E LD_LIBRARY_PATH="$LD_LIBRARY_PATH" qtcreator

          Or you need to customize /etc/sudoers (refer to man sudoers for details).

psan へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です