[LINUX] Using X11 with ubuntu18.04 (C)

X11 library installation

sudo apt update
(sudo apt upgrade)
sudo apt install libx11-dev
#include <X11/Xlib.h>             //Includes required for Xlib
#include <X11/Xutil.h>

Check the placement of the required libraries for Xlib. Check the placement of header files and shared libraries.

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

Commands with other compile options

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

A test program that only displays a black window http://cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/XLIB/xlib2.html I tried the program on the above site as it is.

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

#include <X11/Xlib.h>             //Includes required for Xlib
#include <X11/Xutil.h>
#include <stdio.h>

int main( void )
{
    Display* dis;                       //Display pointer
    Window   win;                       //Window  ID
    XSetWindowAttributes att;           //Window attribute variables
    XEvent ev;                          //Event capture variable

    dis = XOpenDisplay( NULL );         //Connection with Xserver
    win = XCreateSimpleWindow( dis, RootWindow(dis,0), 100, 100,
        256, 256, 3, WhitePixel(dis,0), BlackPixel(dis,0) );  //Window generation

    att.backing_store = WhenMapped;     //Set to save the picture
    XChangeWindowAttributes( dis, win, CWBackingStore, &att );
    

    XMapWindow( dis, win );             //Window display
    XFlush( dis );                      //Forced sending of request
        
    XSelectInput( dis, win, ExposureMask );
    do{                                 //Loop waiting for the window to open
        XNextEvent( dis, &ev);
    }while( ev.type != Expose ); //Repeat here until the Expose event arrives

    //If you come this far, you should see a black window.

    getchar();  //Wait until the return key is pressed.

    XDestroyWindow( dis, win );         //Erase the window
    XCloseDisplay( dis );               //Disconnection from Xserver

    return 0;
}

Make compilation easier by creating a 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)

$ @ Is the target file. $ ^ is the file that specifies the material.

Recommended Posts

Using X11 with ubuntu18.04 (C)
X86 assembler on Linux (linkage with C)
Container-like # 1 made with C
Debugging C / C ++ with gdb
Container-like # 2 made with C
Type-aware using c ++ templates
Call your own C library with Go using cgo
Troublesome story when using Python3 with VScode on ubuntu
x86 compiler self-made with python
When using optparse with iPython
Try using PythonTex with Texpad.
Using Graphviz with Jupyter Notebook
ABC163 C problem with python3
[S3] CRUD with S3 using Python [Python]
using golang slack editing C2
Messaging with AMQP using kombu
Try Google Mock with C
Using Quaternion with Python ~ numpy-quaternion ~
Try using matplotlib with PyCharm
Games using IMU with SenseHat
[Python] Using OpenCV with Python (Basic)
Put Python 3.x on Ubuntu
Build python3 environment with ubuntu 16.04
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory
Using a printer with Debian 10
Format C source with pycparser
ABC188 C problem with python3
Try using folium with anaconda
Using OpenCV with Python @Mac
ABC187 C problem with python
Send using Python with Gmail
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
Solve ABC163 A ~ C with Python
[Ubuntu] [Python] Object tracking using dlib
Call C from Python with DragonFFI
Install Python 2.7.9 and Python 3.4.x with pip.
[Python] Using OpenCV with Python (Image Filtering)
Create Awaitable with Python / C API
Japaneseize Matplotlib with Alpine using Docker
Launch multiple instances with Postgresql 11.x
Using Rstan from Python with PypeR
ROS Lecture 108 Using Database (mongo) with ROS
[Python] Using OpenCV with Python (Image transformation)
Meteorology x Ruby ~ Ruby scraping using Mechanize ~
Introducing Python using pyenv on Ubuntu 20.04
C code review tool using pycparser
[Python] Using OpenCV with Python (Edge Detection)
Preparing python using vscode on ubuntu
Draw Japanese with matplotlib on Ubuntu
Writing C language with Sympy (metaprogramming)
Mount S3 on Ubuntu with goofys
Try using cheap LiDAR (Camsense X1)
Solve ABC168 A ~ C with Python
Using Sessions and Reflections with SQLAlchemy
Using Lambda with AWS Amplify with Go
Using Japanese with Rodeo's IPython @ Windows
Using cgo with the go command
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python