eclipse-paho / paho.mqtt.cpp

Other
1.03k stars 438 forks source link

Paho C library can be built along with the C++ library in a single step? #489

Closed Ambica2022 closed 7 months ago

Ambica2022 commented 7 months ago

Hi,

I tried steps mentioned here https://github.com/eclipse/paho.mqtt.cpp?tab=readme-ov-file#building-the-paho-c-library-1 to build both paho C++ and C at a time. But not happening and encountering an error like C is not available. How to do this? Any clue? Thanks in advance.

Regards, Ambica

hmb commented 7 months ago

Hi @Ambica2022, two things come to mind here:

This results in the following build steps, for which I use a small Makefile:

PAHO_SRCDIR = paho.mqtt.cpp
PAHO_BUILDDIR = paho_build
PAHO_DESTDIR = ../paho_install

paho.mqtt.cpp/CHANGELOG.md:
    git clone --branch v1.3.2 --recurse-submodules https://github.com/eclipse/paho.mqtt.cpp.git

pahocpp-clone: paho.mqtt.cpp/CHANGELOG.md

pahocpp-cmake: pahocpp-clone
    cmake -B "$(PAHO_BUILDDIR)" -S "$(PAHO_SRCDIR)" -DPAHO_WITH_MQTT_C=ON

pahocpp: pahocpp-cmake
    DESTDIR=$(PAHO_DESTDIR) cmake --build "$(PAHO_BUILDDIR)" --target install

Then make pahocpp does the trick for me.

Good Luck Holger

Ambica2022 commented 7 months ago

Hi @hmb,

Thank you. It really helps. But when I use C++ library with other CMake project encountering the below error. /usr/bin/ld: cannot find -leclipse-paho-mqtt-c::paho-mqtt3as collect2: error: ld returned 1 exit status

All CMake variables related to paho C++ library are properly set in my project's CMakeLists.txt file. I guess no CMake variables related to paho C library need to be set in my project's CMakeLists.txt file. Any clue on this?

Thanks, Ambica.

hmb commented 7 months ago

Hi @Ambica2022,

to use the generated lib I simply use the following cmake lines:

list(APPEND CMAKE_PREFIX_PATH "/usr/local")
find_package(PahoMqttCpp REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${PAHO_MQTT_C_INCLUDE_DIRS})

This is assuming the paho libs have been copied to /usr/local after the install step. The path should be the directory where the paho lib is installed in the first step (paho_install in my example) + usr/local. E.g:

It is the directory where you find the include, lib etc.. directories:

paho_install/
└── usr
    └── local        <---------- this one
        ├── include
        │   └── mqtt
        ├── lib
        │   └── cmake
        │       ├── eclipse-paho-mqtt-c
        │       └── PahoMqttCpp
        └── share
            └── doc
                └── paho-mqtt-cpp
                    └── samples

Of course, you have to link against the lib as well:

target_link_libraries(${PROJECT_NAME} PRIVATE PahoMqttCpp::paho-mqttpp3-static)

Cheers Holger

Ambica2022 commented 7 months ago

Thank you @hmb

Regards, Ambica