Can't find X11 includes?

Discussion in 'Desktop / X' started by bashcommando, Jul 22, 2014.

  1. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    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?

  2. WharfRat

    WharfRat Member

    Messages:
    35
    Likes Received:
    11
    Trophy Points:
    8
    Best Answer
    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.
  3. MikeyD

    MikeyD Active Member

    Messages:
    232
    Likes Received:
    113
    Trophy Points:
    43
    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
  4. GrumpyOldMan

    GrumpyOldMan Active Member

    Messages:
    157
    Likes Received:
    50
    Trophy Points:
    28
    try looking through your directories:

    Code:
    sudo find / -iname xlib.h
    It may be that it's in a subtree named Xorg.
  5. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,388
    Likes Received:
    454
    Trophy Points:
    83
    You do have the X11 development packages installed?
    JasKinasis and bashcommando like this.
  6. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    Attempting Install... If it works, then I'll rate this the best answer.
  7. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    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
  8. WharfRat

    WharfRat Member

    Messages:
    35
    Likes Received:
    11
    Trophy Points:
    8
    Add -lX11
    JasKinasis likes this.
  9. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    To what? The command?
    Code:
    gcc -o test main.c
    to
    Code:
    gcc -o test -lX11 main.c
    ryanvade likes this.
  10. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,388
    Likes Received:
    454
    Trophy Points:
    83
    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.
  11. bashcommando

    bashcommando Active Member

    Messages:
    217
    Likes Received:
    43
    Trophy Points:
    28
    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
  12. WharfRat

    WharfRat Member

    Messages:
    35
    Likes Received:
    11
    Trophy Points:
    8
    Best Answer
    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.

Share This Page