title: Undefined reference to... when linking library header
body: <p>I'm trying to compile a C program that uses <code>libvncserver</code> but no matter what I do I keep getting <code>undefined reference</code> errors, the library I'm having troubles with is <code>rfb/rfb.h</code>.</p>
  
  <h1>vnc.c code (copied from <a href="http://libvnc.github.io/doc/html/camera_8c-example.html" rel="nofollow">here</a>):</h1>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;rfb/rfb.h&gt;
  #define WIDTH 640
  #define HEIGHT 480
  #define BPP 4
  /* 15 frames per second (if we can) */
  #define PICTURE_TIMEOUT (1.0/15.0)
  /*
  * throttle camera updates
  */
  int TimeToTakePicture() {
  static struct timeval now={0,0}, then={0,0};
  double elapsed, dnow, dthen;
  gettimeofday(&amp;now,NULL);
  dnow = now.tv_sec + (now.tv_usec /1000000.0);
  dthen = then.tv_sec + (then.tv_usec/1000000.0);
  elapsed = dnow - dthen;
  if (elapsed &gt; PICTURE_TIMEOUT)
  memcpy((char *)&amp;then, (char *)&amp;now, sizeof(struct timeval));
  return elapsed &gt; PICTURE_TIMEOUT;
  }
  /*
  * simulate grabbing a picture from some device
  */
  int TakePicture(unsigned char *buffer)
  {
  static int last_line=0, fps=0, fcount=0;
  int line=0;
  int i,j;
  struct timeval now;
  /*
  * simulate grabbing data from a device by updating the entire framebuffer
  */
  for(j=0;j&lt;HEIGHT;++j) {
  for(i=0;i&lt;WIDTH;++i) {
  buffer[(j*WIDTH+i)*BPP+0]=(i+j)*128/(WIDTH+HEIGHT); /* red */
  buffer[(j*WIDTH+i)*BPP+1]=i*128/WIDTH; /* green */
  buffer[(j*WIDTH+i)*BPP+2]=j*256/HEIGHT; /* blue */
  }
  buffer[j*WIDTH*BPP+0]=0xff;
  buffer[j*WIDTH*BPP+1]=0xff;
  buffer[j*WIDTH*BPP+2]=0xff;
  }
  /*
  * simulate the passage of time
  *
  * draw a simple black line that moves down the screen. The faster the
  * client, the more updates it will get, the smoother it will look!
  */
  gettimeofday(&amp;now,NULL);
  line = now.tv_usec / (1000000/HEIGHT);
  if (line&gt;HEIGHT) line=HEIGHT-1;
  memset(&amp;buffer[(WIDTH * BPP) * line], 0, (WIDTH * BPP));
  /* frames per second (informational only) */
  fcount++;
  if (last_line &gt; line) {
  fps = fcount;
  fcount = 0;
  }
  last_line = line;
  fprintf(stderr,"%03d/%03d Picture (%03d fps)\r", line, HEIGHT, fps);
  /* success! We have a new picture! */
  return (1==1);
  }
  /*
  * Single-threaded application that interleaves client servicing with taking
  * pictures from the camera. This way, we do not update the framebuffer
  * while an encoding is working on it too (banding, and image artifacts).
  */
  int main(int argc,char** argv)
  {
  long usec;
  rfbScreenInfoPtr server=rfbGetScreen(&amp;argc,argv,WIDTH,HEIGHT,8,3,BPP);
  if(!server)
  return 0;
  server-&gt;desktopName = "Live Video Feed Example";
  server-&gt;frameBuffer=(char*)malloc(WIDTH*HEIGHT*BPP);
  server-&gt;alwaysShared=(1==1);
  /* Initialize the server */
  rfbInitServer(server);
  /* Loop, processing clients and taking pictures */
  while (rfbIsActive(server)) {
  if (TimeToTakePicture())
  if (TakePicture((unsigned char *)server-&gt;frameBuffer))
  rfbMarkRectAsModified(server,0,0,WIDTH,HEIGHT);
  usec = server-&gt;deferUpdateTime*1000;
  rfbProcessEvents(server,usec);
  }
  return(0);
  }
  </code></pre>
  
  <h1>Compiler output:</h1>
  
  <pre><code>sudo gcc -g -Wall -Wextra -O2 vnc.c
  
  /tmp/cc7dpMCs.o: In function `main':
  /home/arcm/Projects/c/vnc.c:77: undefined reference to `rfbGetScreen'
  /home/arcm/Projects/c/vnc.c:84: undefined reference to `rfbInitServerWithPthreadsAndZRLE'
  /home/arcm/Projects/c/vnc.c:91: undefined reference to `rfbProcessEvents'
  /home/arcm/Projects/c/vnc.c:86: undefined reference to `rfbIsActive'
  /home/arcm/Projects/c/vnc.c:89: undefined reference to `rfbMarkRectAsModified'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I've got <code>libvncserver0</code> and <code>libvncserver-dev</code> installed and i'm using ubuntu 14.04.
  I tried:</p>
  
  <pre><code>sudo gcc -g -Wall -Wextra -O2 vnc.c -lm
  sudo gcc -g -Wall -Wextra -O2 vnc.c -ldl
  sudo gcc -g -Wall -Wextra -O2 -ldl vnc.c
  sudo gcc -g -Wall -Wextra -O2 -I/usr/include/rfb -L/usr/include/rbf/rfb.h vnc.c 
  sudo gcc -g -Wall -Wextra -O2 -I/usr/include/rfb vnc.c
  sudo gcc -g -Wall -Wextra -O2 -L/usr/include/rbf/rfb.h vnc.c
  sudo gcc -g -Wall -Wextra -O2 /usr/include/rbf/rfb.h vnc.c
  sudo gcc -g -Wall -Wextra -O2 -L/usr/include/rbf/rfb.h -ldl vnc.c
  </code></pre>
  
  <p>But I get the same errors everytime. What am I doing wrong and how can I fix it?</p>