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