3

I'm a bit new to CMake. So this might be a noob question..

I'm trying to build cpp project with CMake. I would like my program to publish to a mqtt broker. I'm running on Linux.

So I installed https://github.com/eclipse/paho.mqtt.cpp and the corresponding c project by doing (side by side)

$ git clone https://github.com/eclipse/paho.mqtt.c.git
$ cd paho.mqtt.c
$ git checkout v1.3.1

$ cmake -Bbuild -H. -DPAHO_WITH_SSL=ON -DPAHO_ENABLE_TESTING=OFF
$ sudo cmake --build build/ --target install
$ sudo ldconfig

as per the documentation and then doing.

$ git clone https://github.com/eclipse/paho.mqtt.cpp
$ mkdir build
$ cd build
$ cmake ../
$ make
$ sudo make install

which all worked fine without any errors.. Now what do i put in my own cmakelists file in my own project that I can use the libraries? So I can do.

#include "mqtt/async_client.h"

int main(int argc, char *argv[]){
   std::cout << "Hello World!" << std::endl;
   mqtt::async_client cli(DFLT_ADDRESS, "", 120, PERSIST_DIR);


   return 0;
}

I tried a simple.

cmake_minimum_required(VERSION 2.8.9)
project (hello)

find_package(PahoMqttCpp REQUIRED)


add_executable(hello helloworld.cpp)
target_link_libraries(hello ${PAHO_CPP_LIB})

But that does not seem to be enough.. I get

helloworld.cpp:(.text+0xd3): undefined reference to `mqtt::async_client::~async_client()'

Any help would be appreciated.

Eric
  • 336
  • 4
  • 17
darthShana
  • 367
  • 1
  • 4
  • 16
  • I think this has already been answered [here](https://stackoverflow.com/a/55800526/3987854). You shouldn't link using `${PAHO_CPP_LIB}`, but instead use `PahoMqttCpp::PahoMqttCpp`. – Kevin Feb 26 '20 at 04:19
  • unfortunately as the issue shows it does not seem to work i tried target_link_libraries(hello ${PAHO_CPP_LIB} paho-mqttpp3) which got me over a hump but now stuck on undefined reference to symbol 'MQTTProperties_free' – darthShana Feb 26 '20 at 04:33

1 Answers1

5

Okay, after some digging, I found the correct thing to write is:

target_link_libraries(hello paho-mqttpp3 paho-mqtt3as)
Kevin
  • 16,549
  • 8
  • 60
  • 74
darthShana
  • 367
  • 1
  • 4
  • 16
  • Hi. Would you be available to give me some insight on how to build (and, hopefully, how to use) the paho-mqtt lib in C++? You see, I've never wrote code in C++, the closest language I know being C, and I haven't been much successful adding the paho-mqtt lib to a project, yet. My personal e-mail is pedroalvesvalentim@gmail.com . Any help would be much appreciated. – Pedro Alves Mar 10 '20 at 22:53