在Linux上使用cmake独立编译PahoMqttCpp示例 [英] Compile PahoMqttCpp sample as standalone with cmake on Linux

查看:1680
本文介绍了在Linux上使用cmake独立编译PahoMqttCpp示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...是使用PahoMqttCpp项目中的示例代码 async_subscribe.cpp https://github.com/eclipse/paho.mqtt.cpp )独立应用程序,然后根据我的需要对其进行修改。

... is to use the sample code async_subscribe.cpp from the PahoMqttCpp project (https://github.com/eclipse/paho.mqtt.cpp) as a standalone application to then modify it for my needs.

Raspberry pi的最新模型( uname -a -> 4.14.98-v7 +#1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU / Linux ),我按照PahoMqttCpp README.md文件中的描述进行操作。我成功编译了C库(v1.2.1),然后成功编译了Cpp部分,最后将两个库都安装到 / usr / local / lib 。在此过程中,未发生任何错误。

On the newest model of Raspberry pi (uname -a--> 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux) I have followed the description in the PahoMqttCpp README.md file. I compiled successfully the C-library (v1.2.1) and then compiled successfuly the Cpp part, finally installed both libraries to /usr/local/lib. During this process, no errors occurred.

然后,作为我自己项目的开始,我复制了 async_subscribe.cpp 进入一个空目录,并开始构建 CMakeLists.txt 进行编译。天真地,我从这个版本开始:

Then, as a start for my own project, I copied the async_subscribe.cpp from the PahoMqttCpp samples into a empty directory and started to build a CMakeLists.txt to compile it. Naively, I started with this 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)

以下 cmake -Bbuild -H。命令未显示错误,输出为

The following cmake -Bbuild -H. command showed no errors, the output was

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

下面的 cmake --build build / 命令没有显示编译错误,但是有很多链接器错误

The following cmake --build build/ command instead showed no compile errors, but a lot of linker errors

[ 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

从PahoMqttCpp文档中,我了解到Cpp库需要PahoMqttC库。在PahoMqttCpp库中的CMakeLists.txt文件中,C部分包含在行 find_package(需要PahoMqttC) https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/ CMakeLists.txt 第26行)。但这也会在第一个cmake命令中给我带来错误。

From the PahoMqttCpp documentation, I learned that the Cpp library needs the PahoMqttC library. In the CMakeLists.txt files from the PahoMqttCpp library, there the C-part is included by a line find_package(PahoMqttC REQUIRED) (https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/CMakeLists.txt line 26). But this produces also errors for me in the first cmake command.

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.

添加一行
set(CMAKE_MODULE_PATH / usr / local / lib / cmake / PahoMqttCpp)到CMakeLists.txt文件可以避免第一个cmake命令中的错误,但是在生成目录中的make commnad失败,并带有相同的未定义引用。

Adding a line set(CMAKE_MODULE_PATH /usr/local/lib/cmake/PahoMqttCpp) to the CMakeLists.txt file avoids the error in the first cmake command, but the make commnad in the build directory fails with the same undefined references.

我想念什么?我还没有在非常复杂的项目中使用过cmake,但是直到现在为止,我认为我已经了解了建立简单项目的基本方法。我非常感谢cmake专家的提示!

What am I missing? I have not used cmake in very complex projects, but I thought I understood the basic way to set up a simple project ...until now. I'd appreciate very much a tip from a cmake expert!!

不幸的是,我仍然遇到错误:

Unfortunately, I am still getting errors:

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






更新



这是我的当前 CMakeLists.txt 文件

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)

仍然产生上面的错误,我不知道如何解决。

that produces still the error above and I don't know how to fix it.

推荐答案

脚本 PahoMqttCppConfig.cmake 确实没有提供这样的变量,例如 PahoMqttCpp_LIBRARIES PahoMqttCpp_INCLUDE_DIRS

Script PahoMqttCppConfig.cmake does NOT provide variables like PahoMqttCpp_LIBRARIES or PahoMqttCpp_INCLUDE_DIRS.

相反,它提供了导入的 target PahoMqttCpp :: PahoMqttCpp 代表PahoMqttCpp库。使用此目标很简单:

Instead, it provides IMPORTED target PahoMqttCpp::PahoMqttCpp which represents PahoMqttCpp library. Using this target is simple:

# This provides both include directories and linked libraries
target_link_libraries(async_subscribe PahoMqttCpp::PahoMqttCpp)






类推,导入的目标 PahoMqttC :: PahoMqttC 用于纯C代码。当使用C ++链接一个 PahoMqttCpp :: PahoMqttCpp 时,该库会自动链接。


By analogy, imported target PahoMqttC::PahoMqttC is provided for pure C code. This library is linked automatically when one links with C++ one PahoMqttCpp::PahoMqttCpp.

这篇关于在Linux上使用cmake独立编译PahoMqttCpp示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆