title: Compiling the Paho MQTT C Client Library
body: <p>I want to integrate the [Paho MQTT C Client Library][1] to one of my C programs. Since I am from EE, I find it difficult achieve this task. However my efforts are described below along with my problem.</p>
  
  <p>The purpose is to simply use username, password and subscribe to perticular MQTT topic using a C program. If this is done, I can proceed to do the rest of things such as saving the data to a .txt file and etc which I am completely familiar with in C. </p>
  
  <p>Since I am from an electronics background, my familiarity with compiling complex projects like these and making them work is not really at best but I really want to get there.</p>
  
  <p>I do know how to compile small projects using "make all" and use the binary executable produced to use such software. I simply do, make clean and make all. </p>
  
  <p>However, the idea of compiling a library doesn't make sense to me. Why do I need to compile any library in the first place? Usually, when I write C program, I integrate someone else's library by uisng #include "library2.h". From this method, I am able to call functions in that library and get things done. Why cant we use Paho the same way? I do not understand why Paho MQTT C library requires compiling. I would like to know the technical reason for this.</p>
  
  <p>Secondly, and most importantly, I would really appreciate if you can provide me step by step guidance to write a simple C program that can subscribe to my MQTT server to printout the messages published in that topic. I use Ubuntu 14.10 LTS. </p>
  
  <p>I understand that this question may be a very basic question. I have asked answers to this question from other people in the lab and also tried fiddling with the example available on [1]</p>
  
  <p>When I do so, I am bombarded with many error messages and I just cant seem to get this to work. May be one of you can shred some light with proper guiding steps for me to get my client to work. </p>
  
  <p>I would really appreciate your efforts on this. It would help me a lot. </p>
  
  <p>update:
  As per request of Gaurav Pathak I post my erros below. </p>
  
  <p>Step 1: I downloaded the coursecode of Paho MQTT libray from 
  github dot com/eclipse/paho.mqtt.c</p>
  
  <p>Step 2: I went into the /home/user/paho.mqtt.c/ and ran <strong>Make Clean</strong> and <strong>Make All</strong></p>
  
  <p>Step 3: Then, I made a copy of the following example code provided on <a href="http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/subasync.html" rel="nofollow noreferrer">http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/subasync.html</a> in side  /home/user/paho.mqtt.c/src . This example code is given below.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "MQTTClient.h"
  #define ADDRESS     "tcp://localhost:1883"
  #define CLIENTID    "ExampleClientSub"
  #define TOPIC       "MQTT Examples"
  #define PAYLOAD     "Hello World!"
  #define QOS         1
  #define TIMEOUT     10000L
  volatile MQTTClient_deliveryToken deliveredtoken;
  void delivered(void *context, MQTTClient_deliveryToken dt)
  {
      printf("Message with token value %d delivery confirmed\n", dt);
      deliveredtoken = dt;
  }
  int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  {
      int i;
      char* payloadptr;
      printf("Message arrived\n");
      printf("     topic: %s\n", topicName);
      printf("   message: ");
      payloadptr = message-&gt;payload;
      for(i=0; i&lt;message-&gt;payloadlen; i++)
      {
          putchar(*payloadptr++);
      }
      putchar('\n');
      MQTTClient_freeMessage(&amp;message);
      MQTTClient_free(topicName);
      return 1;
  }
  void connlost(void *context, char *cause)
  {
      printf("\nConnection lost\n");
      printf("     cause: %s\n", cause);
  }
  int main(int argc, char* argv[])
  {
      MQTTClient client;
      MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
      int rc;
      int ch;
      MQTTClient_create(&amp;client, ADDRESS, CLIENTID,
          MQTTCLIENT_PERSISTENCE_NONE, NULL);
      conn_opts.keepAliveInterval = 20;
      conn_opts.cleansession = 1;
      MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
      if ((rc = MQTTClient_connect(client, &amp;conn_opts)) != MQTTCLIENT_SUCCESS)
      {
          printf("Failed to connect, return code %d\n", rc);
          exit(EXIT_FAILURE);
      }
      printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
             "Press Q&lt;Enter&gt; to quit\n\n", TOPIC, CLIENTID, QOS);
      MQTTClient_subscribe(client, TOPIC, QOS);
      do
      {
          ch = getchar();
      } while(ch!='Q' &amp;&amp; ch != 'q');
      MQTTClient_disconnect(client, 10000);
      MQTTClient_destroy(&amp;client);
      return rc;
  }
  </code></pre>
  
  <p>Step 3: Then I ran GCC client.c -o client</p>
  
  <pre><code>user@userpc:~/paho.mqtt.c/src$ gcc client.c -o client
  /tmp/ccEkSjap.o: In function `msgarrvd':
  client.c:(.text+0xc5): undefined reference to `MQTTClient_freeMessage'
  client.c:(.text+0xd1): undefined reference to `MQTTClient_free'
  /tmp/ccEkSjap.o: In function `main':
  client.c:(.text+0x1eb): undefined reference to `MQTTClient_create'
  client.c:(.text+0x21d): undefined reference to `MQTTClient_setCallbacks'
  client.c:(.text+0x233): undefined reference to `MQTTClient_connect'
  client.c:(.text+0x29a): undefined reference to `MQTTClient_subscribe'
  client.c:(.text+0x2cb): undefined reference to `MQTTClient_disconnect'
  client.c:(.text+0x2da): undefined reference to `MQTTClient_destroy'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>[1]: eclipse dot org/paho/files/mqttdoc/MQTTClient/html/index.html</p>