David
David

Reputation: 1

Compiling the Paho MQTT C Client Library

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.

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.

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.

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.

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.

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.

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]

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.

I would really appreciate your efforts on this. It would help me a lot.

update: As per request of Gaurav Pathak I post my erros below.

Step 1: I downloaded the coursecode of Paho MQTT libray from github dot com/eclipse/paho.mqtt.c

Step 2: I went into the /home/user/paho.mqtt.c/ and ran Make Clean and Make All

Step 3: Then, I made a copy of the following example code provided on http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/subasync.html in side /home/user/paho.mqtt.c/src . This example code is given below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&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(&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, &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<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
    MQTTClient_subscribe(client, TOPIC, QOS);
    do
    {
        ch = getchar();
    } while(ch!='Q' && ch != 'q');
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

Step 3: Then I ran GCC client.c -o client

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

[1]: eclipse dot org/paho/files/mqttdoc/MQTTClient/html/index.html

Upvotes: 0

Views: 5938

Answers (1)

Gaurav Pathak
Gaurav Pathak

Reputation: 1143

However, the idea of compiling a library doesn't make sense to me.

Compiling the library from source is the first thing that you need to do. Even if you don't want to compile the library to create an archive .a file you need to include the source code (*.c and *h files) of the same library in your project.

Why do I need to compile any library in the first place?

Very Good question! The answer is because it's not easy to find a pre-compiled library for specific processor architecture. You may get a pre-compiled library for x86 or x64 but it is difficult to find a pre-compiled library for other architectures for e.g. Power PC, ARM, etc. So, the best way is to download the source code and either make a library out of it to statically link with your project or just directly include the source code in your existing project and compile it with your project.

Usually, when I write C program, I integrate someone else's library by uisng #include "library2.h".

Don't you need to link the source of the library with your project? Just including the headers won't link the library. I guess you need to first understand what are the steps of the compilation process, and especially what is the significance of linker and linking process. Please read this post for getting an understanding of how the program is compiled and linked. Also, please read this post to understand the difference between library and header file.

When I do so, I am bombarded with many error messages and I just cant seem to get this to work.

If you provide your code and tell us your specific error then may be we can help.

Upvotes: 3

Related Questions