2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ubuntu18.04 で X11 を使う (C言語)

Posted at

X11ライブラリのインストール

sudo apt update
(sudo apt upgrade)
sudo apt install libx11-dev
# include <X11/Xlib.h>             //Xlibに必要なインクルード
# include <X11/Xutil.h>

Xlibに必要なライブラリの配置を確認する。
ヘッダファイルと共有ライブラリの配置の確認。

sudo find /usr -type f -name Xlib.h
sudo find /usr -type f -name libX11.so

その他コンパイルオプションをそろえたコマンド

gcc -o xlib_test xlib_test.c -O2 -I/usr/include -L/usr/lib/x86_64-linux-gnu -lX11 -lm

真っ黒なウィンドウを表示するだけのテストプログラム
http://cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/XLIB/xlib2.html
上記のサイトのプログラムをそのまま試しました。

// reference URL
// http://cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/XLIB/xlib2.html

# include <X11/Xlib.h>             //Xlibに必要なインクルード
# include <X11/Xutil.h>
# include <stdio.h>

int main( void )
{
    Display* dis;                       //Display pointer
    Window   win;                       //Window  ID
    XSetWindowAttributes att;           //窓属性の変数
    XEvent ev;                          //イベント取り込み変数

    dis = XOpenDisplay( NULL );         //Xserverとの接続
    win = XCreateSimpleWindow( dis, RootWindow(dis,0), 100, 100,
        256, 256, 3, WhitePixel(dis,0), BlackPixel(dis,0) );  //窓の生成

    att.backing_store = WhenMapped;     //絵を保存する設定をする
    XChangeWindowAttributes( dis, win, CWBackingStore, &att );
    

    XMapWindow( dis, win );             //窓の表示
    XFlush( dis );                      //リクエストの強制送信
        
    XSelectInput( dis, win, ExposureMask );
    do{                                 //窓が開くの待つループ
        XNextEvent( dis, &ev);
    }while( ev.type != Expose ); // Exposeイベントが届くまでここを繰り返す

    // ここまで来たら真っ黒な窓が登場しているはず。

    getchar();  // リターンキーが押されるまで待つ。

    XDestroyWindow( dis, win );         //窓の消去
    XCloseDisplay( dis );               //Xserverと断線

    return 0;
}

Makefileを作成することでコンパイルを楽にする。

CC	= gcc
CFLAGS	= -O2 -I/usr/include
LIBS	= -L/usr/lib/x86_64-linux-gnu -lX11 -lm
TARGET	= xlib_test
SRC	= xlib_test.c

$(TARGET): $(SRC)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

clean:
	rm $(TARGET)

$@はターゲットファイル。
$^は素材指定しているファイル。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?