Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install the library to a non-standard location fails on find lib and undefined headers #427

Open
SaKa1979 opened this issue Mar 8, 2023 · 7 comments

Comments

@SaKa1979
Copy link

SaKa1979 commented Mar 8, 2023

I'm building Paho_cpp as part of a project (Linux). The paho_cpp repository (commit #2ff3d155dcd10564f1816675789284b4efd79eb7) is added as a submodule. The paho_c repository (commit #f7799da95e347bbc930b201b52a1173ebbad45a7) is added as a submodule of paho_cpp.
The /usr/local/lib does not contain any paho libraries.

To build the libraries and install them in a non-standard location I use the following:

C library:

cmake -Bbuild -H. -DPAHO_WITH_SSL=OFF \
                  -DPAHO_BUILD_SHARED=OFF \
                  -DPAHO_BUILD_STATIC=ON \
                  -DPAHO_BUILD_DOCUMENTATION=OFF \
                  -DPAHO_BUILD_SAMPLES=OFF \
                  -DPAHO_BUILD_DEB_PACKAGE=OFF \
                  -DPAHO_ENABLE_TESTING=OFF \
                  -DPAHO_ENABLE_CPACK=OFF \
                  -DPAHO_HIGH_PERFORMANCE=OFF \
                  -DPAHO_USE_SELECT=OFF \
                  -DCMAKE_INSTALL_PREFIX=./build/_install

cmake --build build/ --target install

CPP library:

cmake -Bbuild -H. -DPAHO_BUILD_STATIC=ON \
                  -DPAHO_BUILD_SHARED=OFF \
                  -DPAHO_WITH_SSL=OFF \
                  -DPAHO_BUILD_DEB_PACKAGE=OFF \
                  -DPAHO_BUILD_SAMPLES=ON \
                  -DPAHO_BUILD_TESTS=OFF \
                  -DPAHO_BUILD_DOCUMENTATION=OFF \
                  -DCMAKE_INSTALL_PREFIX=./build/_install \
                  -DCMAKE_PREFIX_PATH=paho_c/build/_install

cmake --build build/ --target install

Without changing anything to the paho_cpp files, find_package(PahoMqttC REQUIRED) fails. So I added the specific paths to the FindPahoMqttC.cmake like so:

find_library(PAHO_MQTT_C_LIBRARIES NAMES ${_PAHO_MQTT_C_LIB_NAME} PATHS ${PROJECT_SOURCE_DIR}/paho_c/build/_install/lib/)
find_path(PAHO_MQTT_C_INCLUDE_DIRS NAMES MQTTAsync.h PATHS ${PROJECT_SOURCE_DIR}/paho_c/build/_install/include/)

In my own project I've added a FindPahoMqttCpp.cmake file to find PahoMqttCpp. This works, the lib is found.
The project's CMakeList contains:

add_library(${LIB_NAME} STATIC ${SOURCES})
target_link_libraries(${LIB_NAME} PRIVATE paho-mqttpp3)

target_include_directories(${LIB_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}
                            ${PROJECT_SOURCE_DIR}/paho_cpp/build/_install/include/

Building results in /usr/bin/ld: cannot find -lpaho-mqttpp3: No such file or directory. So, it's apparently unable to find the library.
I also need to explicitly target the _install/include directory so the mqtt/async_client.h file is located.

First I don't understand why it will not build out-of-the box when using a non-standard installation directory.
Second, why the linker error when the libraries are found by the build system?
Third, Why is necessary to explicitly target the include directory?

I hope I'm in the right place to post these questions and I especially hope somebody can help me out here. I've been struggling with this for multiple days now. Thanks in advance.

@fpagliughi
Copy link
Contributor

Yes, this is the right place! I've been trying to get up to speed on CMake recently, as the build system was implemented by someone else who wandered off a while back. But I'm starting to get the hang of it now, and will see if I can help figure this out.

@philipp-schmidt
Copy link

+1 for updates to cmake, @SaKa1979 I agree the exported targets are not of much help.
I will have a look and see what I can do later today.
@fpagliughi you may have to check the windows build in the end, no way of testing that for me.

@philipp-schmidt
Copy link

I have opted to use the c api directly instead, it works out of the box with cmake.

@philipp-schmidt
Copy link

@SaKa1979 I revisited the cmake build for paho.mqtt.cpp and it turns out it is was easier than I thought.
You only have to target_link_libraries against the exported target PahoMqttCpp::paho-mqttpp3. It will take care of importing all includes and libraries.

So in your case, remove all find_library, find_path and target_include_directories calls, anything that you added in your issue description. All you need is roughly this:

find_package(PahoMqttCpp REQUIRED)
add_executable(myapp ${MYSOURCES})
target_link_libraries(myapp PahoMqttCpp::paho-mqttpp3)

Also you have to set your CMAKE_PREFIX_PATH so that both import targets can be found, paho.mqtt.cpp as well as paho.mqtt.c:

cmake .. -DCMAKE_PREFIX_PATH="/pathto/paho.mqtt.c/build/install/;/pathto/paho.mqtt.cpp/build/install"

@philipp-schmidt
Copy link

I got the final clue from this Stackoverflow post, even though the naming of the targets provided in the answer are wrong. But the general idea was correct:
https://stackoverflow.com/questions/55799122/compile-pahomqttcpp-sample-as-standalone-with-cmake-on-linux

@SaKa1979
Copy link
Author

Thank you very much!
I will try and come back to tell how it went.

@dimonoid
Copy link

Thank you very much! I will try and come back to tell how it went.

How did it go? What was your solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants