title: Linking error in XCode when using Xlib
body: <p>The following "Apple Mach-O Linker (Id) Error" occurs when I try to build a small C++ X11/Xlib program in XCode (Version 5).</p>
  
  <p><strong>Error:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/lJHOB.png" alt="The displayed linking error"></p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;X11/Xlib.h&gt;
  #include &lt;X11/Xutil.h&gt;
  #include &lt;X11/Xos.h&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main(int argc, const char * argv[])
  {
      Display *d;
      Window w;
      XEvent e;
      string msg = "Hello, World!";
      int s;
  
      d = XOpenDisplay(NULL);
      if (d == NULL) {
          fprintf(stderr, "Cannot open display\n");
          exit(1);
      }
  
      s = DefaultScreen(d);
      w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                              BlackPixel(d, s), WhitePixel(d, s));
      XSelectInput(d, w, ExposureMask | KeyPressMask);
      XMapWindow(d, w);
  
      while (1) {
          XNextEvent(d, &amp;e);
          if (e.type == Expose) {
              XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
              XDrawString(d, w, DefaultGC(d, s), 10, 50, msg.c_str(), (int)msg.length());
          }
          if (e.type == KeyPress)
              break;
      }
  
      XCloseDisplay(d);
      return 0;
  }
  </code></pre>
  
  <p>I've added <strong>/opt/X11/include</strong> and <strong>/opt/X11/lib</strong> to the Header Search Paths in Build Settings as suggested in <a href="https://stackoverflow.com/questions/17006245/mac-osx-lion-x11-cimg-library">another post</a> This linker error is still appearing though.</p>
  
  <p>I'm currently running OSX 10.8.5, and I've installed <strong>XQuartz</strong>.</p>
  
  <p>Am I just missing a build setting, or is there potentially something I'm required to install which I've overlooked?</p>
  
  <p>Any help would be greatly appreciated!</p>