# Problem compiling an Xlib program.



## neilms (Aug 7, 2012)

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


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

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


----------



## SirDice (Aug 8, 2012)

Show us the complete command you used to compile this.


----------



## neilms (Aug 8, 2012)

compile code:
`$ g++ -o -window wind.cpp -lX11 -L/usr/X6R11/lib -I /usr/X6R11/include`


----------



## SirDice (Aug 8, 2012)

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


----------



## neilms (Aug 8, 2012)

Thanks, this compiles now. However,if I run the program from the console I get a segmentation fault (core dumped) error. Why is this?


----------



## neilms (Aug 9, 2012)

It's ok, I have solved my problem.


----------



## TheRaven (Oct 23, 2017)

5 Years later...

https://rosettacode.org/wiki/Window_creation/X11#C
CLang
FreeBSD 11.1

Thinking about how I'm going to compile a windowed application using C and XLib with CLang and this post got me everywhere I needed to be. Still relevant and awesome -first X11 windowed application for me- thanks to neilms and SirDice.


----------

