Including the Paho-MQTT-cpp in cmake

This tutorial is less of a tutorial and more of how to use the paho-mqtt-c/cpp libraries that were generated in the previous post. This tutorial hinges on you having had installed the libraries using sudo make install. Else the commands might not work and you might have to use absolute paths which I have not yet tried.
I always try to setup my code in the following format.
Hello World sample:
sample/
├── build
├── CMakeFiles.txt
├── include
│   ├── CMakeFiles.txt
│   └── test_header.h
└── src
 ├── CMakeFiles.txt
 └── test.cpp
 └── test_header.cpp
This is the basic setup for this tutorial.
sample/CMakeLists.txt
Aside from the project name, this CMakeFiles.txt is a pretty default one which could be used for any project.
project(test)

#This is how you make a comment

#This is where you can set flags such as -Wall -pedantic etc...
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

#This is required
cmake_minimum_required(VERSION 2.8)
include(CheckCXXCompilerFlag)

#A function created to perform the same as in set(CMAKE_CXX...
function(enable_cxx_compiler_flag_if_supported flag)
    string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set)
  if(flag_already_set EQUAL -1)
   check_cxx_compiler_flag("${flag}" flag_supported)
   if(flag_supported)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
   endif()
   unset(flag_supported CACHE)
  endif()
endfunction()

enable_cxx_compiler_flag_if_supported("-Wall")
enable_cxx_compiler_flag_if_supported("-Wextra")
enable_cxx_compiler_flag_if_supported("-pedantic")

#this method points to the include folder for your header files
include_directories(include)

#add subdirectory for the src files
add_subdirectory(src)
You can use the function enable_cxx_compiler_flag_if_supported or the set (CMAKE_CXX …) to add flags to your commands
sample/src/CMakeLists.txt
This is simple, just need to list down your source files (or use a wildcard)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

file(GLOB SOURCE_FILES "*.cpp")
#can also set it as this, but you need to type all names
#set(SOURCE_FILES tester.cpp test_header.cpp)
add_executable( tester ${SOURCE_FILES} )

No need to have a sample/include/CMakeLists.txt here.
To run (assuming you have installed cmake)
cd build
cmake ..
make
All the build files should appear in this directory (in src/)
./src/tester

Moving forward incorporating the paho-mqtt-cpp for the code. Here are the thing you must add do to CMakeLists.txt
enable_cxx_compiler_flag_if_supported("-O2")
enable_cxx_compiler_flag_if_supported("D_NDEBUG")

find_package(OpenSSL REQUIRED)
find_library(paho-mqttpp3 NAMES libpaho-mqttpp3.a REQUIRED)
find_library(paho-mqtt3a NAMES libpaho-mqtt3a.so REQUIRED)

add_library(pahottpp STATIC IMPORTED)
set_target_properties(pahottpp PROPERTIES IMPORTED_LOCATION ${paho-mqttpp3})

add_library(pahomqtt3a SHARED IMPORTED)
set_property(TARGET pahomqtt3a PROPERTY IMPORTED_LOCATION ${paho-mqtt3a})
Add to src/CMakeLists.txt
#This should be added after the add_executable (wherever it its)
target_link_libraries(tester pahottpp)
target_link_libraries(tester pahomqtt3a)
Breaking down the additional lines:
find_package(OpenSSL REQUIRED): for installed packages that were built with CMake. Installed e.g. sudo apt-get install (i’m not entirely sure here)
find_library(add_variable NAMES lib_name REQUIRED): REQUIRED is a flag. Here CMake automatically looks for the lib_name. This works for lib_names that were installed with sudo make install
*Note that one of the libraries I set here is a static and the other is shared.
add_library(add_variable STATIC|SHARED IMPORTED): add a library named variable with the properties STATIC|SHARED, IMPORTED.
set_property(TARGET add_variable PROPERTY IMPORTED_LOCATION ${add_variable}): Here you use the variables you created with add_library and find_library respectively to add the location to the target property of the add_variable.
target_link_libraries(executable add_variable): Here is where you link the add_variables to the executables.
 
To test, copy this into the src/test.cpp: https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/samples/async_publish.cpp
This should now work after rebuilding cmake.
cd build
cmake ..
make
./src/tester
Output:
Initializing for server 'tcp://localhost:1883'...  ...OK
Connecting...Waiting for the connection...  ...OK
Sending message... Delivery complete for token:   ...OK
Sending next message...1  ...with token: 2  ...for message with 9 bytes Delivery complete for token: 2  ...OK
Sending next message... Listener success for token: 0  ...OK
Sending final message... Listener success for token: 0OK
Disconnecting...  ...OK
Useful Links:
http://derekmolloy.ie/hello-world-introductions-to-cmake/
  • Bit in-depth and shows how to build a library (static and shared)
https://rix0r.nl/blog/2015/08/13/cmake-guide/
  • Shows how a complete project directory may look and how to set it up with cmake
https://cmake.org/Wiki/CMake_Useful_Variables
  • Useful built in cmake variables
  • Short description of how to add include directories

2 responses to “Including the Paho-MQTT-cpp in cmake”

  1. Hey! Thanks for the tips. I should have done something like this a long time ago. An update to the Paho C++ library was just released (v1.0.1), which mainly tries to improve and modernize the CMake build system. I don’t know if it messes up the instructions in this post, but either way, I’m somewhat of a CMake novice, so if you could give the new version a test run and report back any issues, I would greatly appreciate it.

    Frank P
    Author/maintainer of the Eclipse Paho C++ library

    Like

    • Wow! First off, thanks for the library! I’m a bit busy at this moment, but once I get some time, I’ll take a look an update my instructions. Thanks again!

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.