Problem compiling an Xlib program.

Hello, I am attempting to compile a simple C++ program that produces an Xlib window. Here is the program which is from a tutorial:

Code:
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h>   // I include this to test return values the lazy way
#include <unistd.h>   // So we got the profile for 10 seconds

#define NIL (0)       // A name for the void pointer

main()
{
      // Open the display

      Display *dpy = XOpenDisplay(NIL);
      assert(dpy);

      // Get some colors

      int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
      int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

      // Create the window

      Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
				     200, 100, 0, blackColor, blackColor);

      // We want to get MapNotify events

      XSelectInput(dpy, w, StructureNotifyMask);

      // "Map" the window (that is, make it appear on the screen)

      XMapWindow(dpy, w);

      // Create a "Graphics Context"

      GC gc = XCreateGC(dpy, w, 0, NIL);

      // Tell the GC we draw using the white color

      XSetForeground(dpy, gc, whiteColor);

      // Wait for the MapNotify event

      for(;;) {
	    XEvent e;
	    XNextEvent(dpy, &e);
	    if (e.type == MapNotify)
		  break;
      }

      // Draw the line
      
      XDrawLine(dpy, w, gc, 10, 60, 180, 20);

      // Send the "DrawLine" request to the server

      XFlush(dpy);

      // Wait for 10 seconds

      sleep(10);
}

When I invoke the compilation commands, I get the following errors:
Code:
wind.cpp:1:21: error: X11/Xlib.h: No such file or directory
wind.cpp: In function 'int main()':
wind.cpp:8: error: 'Display' was not declared in this scope
wind.cpp:8: error: 'dpy' was not declared in this scope
wind.cpp:8: error: 'XOpenDisplay' was not declared in this scope
wind.cpp:11: error: 'DefaultScreen' was not declared in this scope
wind.cpp:11: error: 'BlackPixel' was not declared in this scope
wind.cpp:12: error: 'WhitePixel' was not declared in this scope
wind.cpp:15: error: 'Window' was not declared in this scope
wind.cpp:15: error: expected `;' before 'w'
wind.cpp:18: error: 'w' was not declared in this scope
wind.cpp:18: error: 'StructureNotifyMask' was not declared in this scope
wind.cpp:18: error: 'XSelectInput' was not declared in this scope
wind.cpp:21: error: 'XMapWindow' was not declared in this scope
wind.cpp:24: error: 'GC' was not declared in this scope
wind.cpp:24: error: expected `;' before 'gc'
wind.cpp:25: error: 'gc' was not declared in this scope
wind.cpp:25: error: 'XSetForeground' was not declared in this scope
wind.cpp:29: error: 'XEvent' was not declared in this scope
wind.cpp:29: error: expected `;' before 'e'
wind.cpp:30: error: 'e' was not declared in this scope
wind.cpp:30: error: 'XNextEvent' was not declared in this scope
wind.cpp:31: error: 'MapNotify' was not declared in this scope
wind.cpp:35: error: 'XDrawLine' was not declared in this scope
wind.cpp:36: error: 'XFlush' was not declared in this scope

I have checked to ensure that libX11 is installed (it is) but cannot figure out why the compiler and linker cannot see the library and headers, even when explicitly linked.

Any ideas would be most welcome.
 
Show us the complete command you used to compile this.
 
compile code:
$ g++ -o -window wind.cpp -lX11 -L/usr/X6R11/lib -I /usr/X6R11/include
 
X6R11 doesn't exist. If anything it should be X11R6. But even that shouldn't exist anymore.

Use /usr/local/lib/ and /usr/local/include/.
 
Thanks, this compiles now. However,if I run the program from the console I get a segmentation fault (core dumped) error. Why is this?
 
Back
Top