2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

rustのguiライブラリeguiのテンプレートをdocker上で動かしてみる

Last updated at Posted at 2024-09-23

rustのguiアプリを作るのに、まずは一番簡単なeguiを使ってみようと思って、環境構築してみた。

eguiとは

Rust の egui は、シンプルかつ即時モード(immediate mode)で動作する GUI ライブラリです。
軽量で使いやすく、クロスプラットフォーム対応しており、ゲームやツール開発に便利です。
コード内でウィジェットを直接作成・操作でき、リアルタイムなインタラクションが可能です。
(ChatGPT による説明)

docker 上で egui のテンプレートを動かす

以下の egui の テンプレート実行してみる

emilk/eframe_template at master

環境

windows 11 wsl ubuntu 24.04

docker CLI

ディレクトリ構成

docker_rust
├── compose.yml
├── rust
│   └── Dockerfile
└── workspace
compose.yml
services:
  mylinux:
    build:
      context: .
      dockerfile: ./rust/Dockerfile
    container_name: myrust_gui
    hostname: myrust 
    working_dir: /workspace
    tty: true
    stdin_open: true
    volumes:
      - /tmp/.X11-unix:/tmp/.X11-unix
      - /mnt/wslg:/mnt/wslg
      - ./workspace:/workspace
    environment:
      - DISPLAY=$DISPLAY
      - PULSE_SERVER=$PULSE_SERVER
      - XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR
      - XDG_SESSION_TYPE=x11
      - WAYLAND_DISPLAY=
Dockerfile
FROM rust:latest

ENV CARGO_TARGET_DIR=/tmp/target \
  DEBIAN_FRONTEND=noninteractive \
  LC_CTYPE=ja_JP.utf8 \
  LANG=ja_JP.utf8

RUN apt-get update \
  && apt-get upgrade -y \
  && apt-get install -y -q \
  locales \
  git \
  xserver-xorg \
  x11-apps \
  libxkbcommon-x11-0 \
  libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev
  && echo "ja_JP UTF-8" > /etc/locale.gen \
  && locale-gen 

WORKDIR /workspace

実行方法

以下のコマンドを実行する

wsl 上で

docker compose build
docker compose up -d
dockeer container exec -it myrust_gui bash

docker コンテナ内で

git clone https://github.com/emilk/eframe_template.git
cd eframe_template
cargo run --release

※以下のエラーが出ることがあるが、ただの通信エラーなのか
cargo run --release
コマンドをもう一度実行すればうまくいった

error: component download failed for rustc-x86_64-unknown-linux-gnu: error decoding response body

以下の画面が表示されれば完了

eguiのテンプレートの画面

補足

emilk/eframe_template at master
に書かれている通りに実行した。

ただし、以下のエラーが発生して、waylandのコンポジターがないと出たので、waylandではなく、x11で動作させた。

Error: WinitEventLoop(Os(OsError { line: 81, file: "/home/anyumu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.9/src/platform_impl/linux/wayland/event_loop/mod.rs", error: WaylandError(Connection(NoCompositor)) }))

x11上で実行させるために、
xserver-xorg をインストール1し、
以下の環境変数を設定した

export XDG_SESSION_TYPE=x11
export WAYLAND_DISPLAY=""

おまけ

クロスコンパイルでWindows向けの.exeファイルを作成する手順

Linux環境からWindows向けの実行ファイル(.exe)をクロスコンパイルする手順を説明します。以下の手順に従って、設定を行います。

1. MinGWリンカーのインストール

まず、MinGWのリンカーをインストールします。これにより、Windows向けのバイナリを作成できるようになります。

sudo apt install mingw-w64

2. .cargo/config ファイルの設定

プロジェクトのルートディレクトリに .cargo/config ファイルを作成し、以下の設定を追加します。この設定により、クロスコンパイル時に使用するリンカーが指定されます。

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"

3. Windowsターゲットの追加

RustがWindows向けにコンパイルできるように、ターゲットを追加します。

rustup target add x86_64-pc-windows-gnu

追加されたターゲットを確認するには、以下のコマンドを実行します。

rustup target list

4. ビルドの実行

準備が整ったら、以下のコマンドでWindows向けのバイナリをビルドします。

cargo build --release --target x86_64-pc-windows-gnu

これで、プロジェクトの target/x86_64-pc-windows-gnu/release/ ディレクトリ内にWindows用の実行ファイル(.exe)が生成されます。


参考

  1. x11-appsもxserverの動作確認用にインストール。xeyesコマンドを打てば、目玉が表示される

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?