Mon objectif
... est d'utiliser l'exemple de code async_subscribe.cpp
du projet PahoMqttCpp ( https://github.com/eclipse/paho.mqtt.cpp ) comme une application autonome pour ensuite la modifier selon mes besoins.
Mon approche
Préliminaires
Sur le dernier modèle de Raspberry pi ( uname -a
--> 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux
) J'ai suivi la description dans le fichier README.md de PahoMqttCpp. J'ai compilé avec succès la bibliothèque C (v1.2.1) et ensuite compilé avec succès la partie Cpp, finalement j'ai installé les deux bibliothèques à /usr/local/lib
. Pendant ce processus, aucune erreur ne s'est produite.
Mon projet
Ensuite, comme point de départ pour mon propre projet, j'ai copié le fichier async_subscribe.cpp
des échantillons de PahoMqttCpp dans un répertoire vide et a commencé à construire une CMakeLists.txt
pour le compiler. Naïvement, j'ai commencé avec cette version :
cmake_minimum_required(VERSION 3.5)
add_library(PahoMqttC SHARED IMPORTED)
set_target_properties(PahoMqttC PROPERTIES IMPORTED_LOCATION /usr/share/lib/libpaho-mqtt3a.so)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(PahoMqttCpp REQUIRED)
add_executable(async_subscribe async_subscribe.cpp)
include_directories(${PahoMqttCpp_INCLUDE_DIRS})
target_link_libraries(async_subscribe ${PahoMqttCpp_LIBRARIES})
target_link_libraries(async_subscribe ${PahoMqttC_LIBRARIES})
install(TARGETS async_subscribe RUNTIME DESTINATION bin)
Les éléments suivants cmake -Bbuild -H.
ne montre aucune erreur, la sortie est
pi@homepi:~/Develop/CppMosquitto/example $ cmake -Bbuild -H.
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Found PahoMqttC: /usr/local/lib/libpaho-mqtt3a.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Develop/CppMosquitto/example/build
Les éléments suivants cmake --build build/
ne montre aucune erreur de compilation, mais beaucoup d'erreurs d'édition de liens.
[ 50%] Linking CXX executable async_subscribe
CMakeFiles/async_subscribe.dir/async_subscribe.cpp.o: In function `main':
async_subscribe.cpp:(.text+0x154): undefined reference to `mqtt::connect_options::connect_options()'
async_subscribe.cpp:(.text+0x188): 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&, mqtt::iclient_persistence*)'
async_subscribe.cpp:(.text+0x1b0): undefined reference to `mqtt::async_client::set_callback(mqtt::callback&)'
async_subscribe.cpp:(.text+0x1e0): undefined reference to `mqtt::connect_options::connect_options(mqtt::connect_options const&)'
async_subscribe.cpp:(.text+0x200): undefined reference to `mqtt::async_client::connect(mqtt::connect_options, void*, mqtt::iaction_listener&)'
async_subscribe.cpp:(.text+0x2e4): undefined reference to `mqtt::async_client::~async_client()'
async_subscribe.cpp:(.text+0x43c): undefined reference to `mqtt::async_client::~async_client()'
CMakeFiles/async_subscribe.dir/async_subscribe.cpp.o: In function `mqtt::async_client::disconnect()':
async_subscribe.cpp:(.text._ZN4mqtt12async_client10disconnectEv[_ZN4mqtt12async_client10disconnectEv]+0x2c): undefined reference to `mqtt::disconnect_options::disconnect_options()'
CMakeFiles/async_subscribe.dir/async_subscribe.cpp.o: In function `callback::reconnect()':
async_subscribe.cpp:(.text._ZN8callback9reconnectEv[_ZN8callback9reconnectEv]+0x68): undefined reference to `mqtt::connect_options::connect_options(mqtt::connect_options const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/async_subscribe.dir/build.make:94: die Regel für Ziel „async_subscribe“ scheiterte
make[2]: *** [async_subscribe] Fehler 1
CMakeFiles/Makefile2:67: die Regel für Ziel „CMakeFiles/async_subscribe.dir/all“ scheiterte
make[1]: *** [CMakeFiles/async_subscribe.dir/all] Fehler 2
Makefile:127: die Regel für Ziel „all“ scheiterte
make: *** [all] Fehler 2
La documentation de PahoMqttCpp m'a appris que la bibliothèque Cpp a besoin de la bibliothèque PahoMqttC. Dans les fichiers CMakeLists.txt de la bibliothèque PahoMqttCpp, la partie C est incluse par une ligne find_package(PahoMqttC REQUIRED)
( https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/CMakeLists.txt ligne 26). Mais cela produit également des erreurs pour moi dans la première commande cmake.
CMake Error at CMakeLists.txt:6 (find_package):
By not providing "FindPahoMqttC.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"PahoMqttC", but CMake did not find one.
Could not find a package configuration file provided by "PahoMqttC" with
any of the following names:
PahoMqttCConfig.cmake
pahomqttc-config.cmake
Add the installation prefix of "PahoMqttC" to CMAKE_PREFIX_PATH or set
"PahoMqttC_DIR" to a directory containing one of the above files. If
"PahoMqttC" provides a separate development package or SDK, be sure it has
been installed.
Ajout d'une ligne set(CMAKE_MODULE_PATH /usr/local/lib/cmake/PahoMqttCpp)
au fichier CMakeLists.txt évite l'erreur dans la première commande cmake, mais le make commnad dans le répertoire de construction échoue avec les mêmes références indéfinies.
Qu'est-ce que je rate ? Je n'ai pas utilisé cmake dans des projets très complexes, mais je pensais avoir compris la manière de base de mettre en place un projet simple ... jusqu'à maintenant. J'apprécierais beaucoup une astuce d'un expert de cmake ! !!
Modifier après avoir essayé l'approche de la première réponse
Malheureusement, je continue à avoir des erreurs :
pi@homepi:~/Develop/CppMosquitto/example $ cmake -Bbuild -H.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Configuring done
CMake Error at CMakeLists.txt:10 (add_executable):
Target "async_subscribe" links to target "PahoMqttCpp::PahoMqttCpp" but the
target was not found. Perhaps a find_package() call is missing for an
IMPORTED target, or an ALIAS target is missing?
-- Generating done
-- Build files have been written to: /home/pi/Develop/CppMosquitto/example/build
Mise à jour
C'est mon actuel CMakeLists.txt
fichier
cmake_minimum_required(VERSION 3.5)
find_package(PahoMqttCpp REQUIRED)
add_executable(async_subscribe async_subscribe.cpp)
target_link_libraries(async_subscribe PahoMqttCpp::PahoMqttCpp)
install(TARGETS async_subscribe RUNTIME DESTINATION bin)
qui produit toujours l'erreur ci-dessus et je ne sais pas comment la corriger.