Search code examples
cmakepaho

How to add Paho Mqtt C and Cpp libraries to a CMakeLists.txt


I am trying to build a cpp project using CMakeLists.txt.

I installed both Paho Mqtt C and Cpp and their Config.cmake can be found at /usr/local/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig.cmake and /usr/local/lib/cmake/PahoMqttCpp/PahoMqttCppConfig.cmake in my system.

I am trying to compile the async_publish.cpp. For now I could only get it built running the following CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project("Mqtt Test")
#find_package(PahoMqttCpp REQUIRED)
#find_package(eclipse-paho-mqtt-c REQUIRED)

add_executable(mqtt_client mqtt_client.cpp)

target_link_libraries(mqtt_client pthread crypto ssl paho-mqtt3as paho-mqttpp3)

The problem is ... how do I get the libraries from find_package() and pick the right ${whatever_pkg_LIB} and maybe include files?

I tried

target_link_libraries(mqtt_client pthread crypto ssl ${PAHO_MQTT_C_LIBRARIES} ${PAHO_MQTT_CPP_LIBRARIES})

but it didn't work. I got the following error

CMakeFiles/mqtt_client.dir/mqtt_client.cpp.o: In function `main':
mqtt_client.cpp:(.text+0x3ae): undefined reference to `mqtt::async_client::async_client(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, mqtt::iclient_persistence*)'
mqtt_client.cpp:(.text+0x401): undefined reference to `mqtt::async_client::set_connected_handler(std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>)'
mqtt_client.cpp:(.text+0x445): undefined reference to `mqtt::async_client::set_connection_lost_handler(std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)>)'
mqtt_client.cpp:(.text+0x463): undefined reference to `mqtt::connect_options::connect_options()'
mqtt_client.cpp:(.text+0x4b7): undefined reference to `mqtt::message::message(mqtt::buffer_ref<char>, mqtt::buffer_ref<char>, int, bool)'
mqtt_client.cpp:(.text+0x4ee): undefined reference to `mqtt::will_options::will_options(mqtt::message const&)'
mqtt_client.cpp:(.text+0x507): undefined reference to `mqtt::connect_options::set_will(mqtt::will_options const&)'
mqtt_client.cpp:(.text+0x520): undefined reference to `mqtt::connect_options::set_automatic_reconnect(int, int)'
mqtt_client.cpp:(.text+0x561): undefined reference to `mqtt::connect_options::connect_options(mqtt::connect_options const&)'
mqtt_client.cpp:(.text+0x581): undefined reference to `mqtt::async_client::connect(mqtt::connect_options)'
mqtt_client.cpp:(.text+0x6d1): undefined reference to `mqtt::topic::publish(mqtt::buffer_ref<char>)'
mqtt_client.cpp:(.text+0x798): undefined reference to `mqtt::topic::publish(mqtt::buffer_ref<char>)'
mqtt_client.cpp:(.text+0x8d9): undefined reference to `mqtt::async_client::~async_client()'
mqtt_client.cpp:(.text+0xb9c): undefined reference to `mqtt::async_client::~async_client()'
CMakeFiles/mqtt_client.dir/mqtt_client.cpp.o: In function `mqtt::async_client::disconnect()':
mqtt_client.cpp:(.text._ZN4mqtt12async_client10disconnectEv[_ZN4mqtt12async_client10disconnectEv]+0x45): undefined reference to `mqtt::disconnect_options::disconnect_options()'
collect2: error: ld returned 1 exit status
CMakeFiles/mqtt_client.dir/build.make:95: recipe for target 'mqtt_client' failed
make[2]: *** [mqtt_client] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mqtt_client.dir/all' failed
make[1]: *** [CMakeFiles/mqtt_client.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Solution

  • this is the CMakeLists.txt I use to make my project

    cmake_minimum_required(VERSION 3.1) set(CMAKE_CXX_STANDARD 20) project(Farm_interface)

    include_directories(${PROJECT_SOURCE_DIR}/include)

    add_executable(Farm.exe src/main.cpp src/subscriber.cpp) target_link_libraries( Farm.exe paho-mqttpp3 paho-mqtt3as pthread)