PDA

View Full Version : Xlib not found, Fedora Core 4



finnepower
07-01-2005, 01:27 AM
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:


# 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:


#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.

Ken Fitlike
07-01-2005, 03:43 AM
Those are linker errors - you need to link with and specify the path to, the relevant libraries; something like:
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. ;)

finnepower
07-01-2005, 05:24 AM
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)?

Ken Fitlike
07-01-2005, 06:11 AM
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
locate libX11.a to get that path for your system and use that instead. ie.
g++ -I /usr/X11R6/include/ -Lyour_path_to_libX11.a -lX11 -o test1.out test1.cpp

finnepower
07-01-2005, 06:34 AM
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:


g++ -L/usr/X11R6/lib64 -lX11 -o test1.out test1.cpp