Can't find X11 includes?

B

bashcommando

Guest
I was learning to program with X11 but when I compiled the program it gave me an error:
Code:
main.c:3:22: fatal error: X11/Xlib.h: No such file or directory
#include <X11/Xlib.h>
                      ^
compilation terminated.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>

Display *dis;
Window win;

int main()
{
    dis = XOpenDisplay(NULL);
    win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, \0, BlackPixel (dis, 0), BlackPixel(dis, 0));
    XMapWindow(dis, win);
    XFlush(dis);
    sleep(5);
    return(0);
}
Help?
 


That is odd. From the error it obviously can't locate the libs. Are they located under /usr/include? There should be an X11 folder with Xlib.h.

The only thing I can think of is its a problem with the search path via gcc (or whatever you're using to compile) although it should be searching in the correct places if the X11 library is installed correctly.

You can try using -I flag and manually specify the dir just to make sure

Info on GCC search path: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
 
try looking through your directories:

Code:
sudo find / -iname xlib.h

It may be that it's in a subtree named Xorg.
 
Now I have another Problem with the same code:
Code:
/tmp/ccLTk8Ip.o: In function `main':
main.c:(.text+0x12): undefined reference to `XOpenDisplay'
main.c:(.text+0x82): undefined reference to `XCreateSimpleWindow'
main.c:(.text+0x9e): undefined reference to `XMapWindow'
main.c:(.text+0xab): undefined reference to `XFlush'
collect2: error: ld returned 1 exit status
 
To what? The command?
Code:
gcc -o test main.c
to
Code:
gcc -o test -lX11 main.c
Yes.

By the way, whenever you want to build using some interface like X you must have development packages. IE the libraries that you are trying to link. Same thing when using QT, GTK, Java, etc.
 
Code:
/tmp/ccUqAXHL.o: In function `main':
main.c:(.text+0x12): undefined reference to `XOpenDisplay'
main.c:(.text+0x82): undefined reference to `XCreateSimpleWindow'
main.c:(.text+0x9e): undefined reference to `XMapWindow'
main.c:(.text+0xab): undefined reference to `XFlush'
collect2: error: ld returned 1 exit status
I used the command
Code:
gcc -o test -lX11 main.c
 
You have another problem with the line
win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, \0, BlackPixel (dis, 0), BlackPixel(dis, 0));


Change it to


win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), BlackPixel(dis, 0));


The errors you're getting now has to be due to not having the libx11-dev package installed.

I don't know what distro you're using, but that's what it's called in the apt package manager
.

I compiled it with gcc ccode.c -lX11 -o ccode and it was fine.
 

Members online


Latest posts

Top