Xlib not found, Fedora Core 4

This is a discussion on Xlib not found, Fedora Core 4 within the Linux Programming forums, part of the Platform Specific Boards category; The g++ compiler doesn't seem to find Xlib.h on my machine. I know the file is in /usr/X11R6/include/X11. In /usr/include ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    51

    Xlib not found, Fedora Core 4

    The g++ compiler doesn't seem to find Xlib.h on my machine. I know the file is in /usr/X11R6/include/X11. In /usr/include there is a link to the X11 folder. The compiler says this if I run:

    Code:
    # g++ -o test1.out test1.cpp
    /tmp/ccYcKmMS.o(.text+0x11): In function `main':
    test1.cpp: undefined reference to `XOpenDisplay'
    /tmp/ccYcKmMS.o(.text+0xe0):test1.cpp: undefined reference to `XCreateSimpleWindow'
    /tmp/ccYcKmMS.o(.text+0xf1):test1.cpp: undefined reference to `XMapWindow'
    /tmp/ccYcKmMS.o(.text+0x108):test1.cpp: undefined reference to `XCreateGC'
    /tmp/ccYcKmMS.o(.text+0x11f):test1.cpp: undefined reference to `XSetForeground'
    /tmp/ccYcKmMS.o(.text+0x12f):test1.cpp: undefined reference to `XNextEvent'
    /tmp/ccYcKmMS.o(.text+0x163):test1.cpp: undefined reference to `XDrawLine'
    /tmp/ccYcKmMS.o(.text+0x16c):test1.cpp: undefined reference to `XFlush'
    collect2: ld returned 1 exit status
    I get the same errors if I run: g++ -I /usr/X11R6/include/ -o test1.out test1.cpp
    Here are the contents of test1.cpp:

    Code:
    #include <X11/Xlib.h>
    #include <assert.h>
    #include <unistd.h>
    
    #define NIL (0)
    
    int main()
    {
    Display *dpy = XOpenDisplay(NIL);
    assert(dpy);
    
    int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
    int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
    
    Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 200,
    				100, 0, blackColor, blackColor);
    
    XMapWindow(dpy, w);
    
    GC gc = XCreateGC(dpy, w, 0, NIL);
    XSetForeground(dpy, gc, whiteColor);
    
    	while(1)
    	{
    	XEvent e;
    	XNextEvent(dpy, &e);
    
    		if(e.type == MapNotify)
    		break;
    	}
    
    XDrawLine(dpy, w, gc, 10, 60, 180, 20);
    
    XFlush(dpy);
    sleep(10);
    return 0;
    }
    There shouldn't be anything wrong with the source code since I copied it from a tutorial page.
    Last edited by finnepower; 07-01-2005 at 02:29 AM.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Those are linker errors - you need to link with and specify the path to, the relevant libraries; something like:
    Code:
    g++ -I /usr/X11R6/include/ -L/usr/X11R6/lib -lX11 -o test1.out test1.cpp
    There shouldn't be anything wrong with the source code since I copied it from a tutorial page.
    New maxim: third party code rarely builds at the first attempt.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    51
    I still get the same errors. Except when i add the -lX11 option I get the error:
    /usr/bin/ld: cannot find -lX11

    Am I right about that it should work with only the -L/usr/X11R6/lib option?

    What do I need to replace the -lX11 option with to avoid the error mentioned above (and possibly get it to work)?
    Last edited by finnepower; 07-01-2005 at 05:31 AM.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The -L switch provides the linker (ld) with additional library search path(s) - it may be that Fedora has the X-libraries in some other directory(I'm using Slackware); do a
    Code:
    locate libX11.a
    to get that path for your system and use that instead. ie.
    Code:
    g++ -I /usr/X11R6/include/ -Lyour_path_to_libX11.a -lX11 -o test1.out test1.cpp
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    51
    The file was in /usr/X11R6/lib64
    It works now, thanks for the help!

    If anyone else want's to know, the correct command was:

    Code:
    g++ -L/usr/X11R6/lib64 -lX11 -o test1.out test1.cpp
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  2. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. question regarding code found on this board
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21