【问题标题】:Compile PahoMqttCpp sample as standalone with cmake on Linux在 Linux 上使用 cmake 将 PahoMqttCpp 示例编译为独立的
【发布时间】:2019-04-22 18:22:11
【问题描述】:

我的目标

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

我的方法

预赛

在最新型号的树莓派 (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。在此过程中,没有发生错误。

我的项目

然后,作为我自己项目的开始,我将 PahoMqttCpp 示例中的 async_subscribe.cpp 复制到一个空目录中,并开始构建一个 CMakeLists.txt 来编译它。天真地,我从这个版本开始:

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. 命令没有显示错误,输出是

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/ 命令反而显示没有编译错误,但有很多链接器错误

[ 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 REQUIRED) 行中(https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/CMakeLists.txt 第 26 行)。但这也会在第一个 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.

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

我错过了什么?我没有在非常复杂的项目中使用过 cmake,但我认为我理解了建立一个简单项目的基本方法......直到现在。我非常感谢 cmake 专家的提示!

尝试第一个答案的方法后编辑

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

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)

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

【问题讨论】:

  • 嗨!我有同样的错误。你找到解决办法了吗?

标签: c++ cmake mqtt paho


【解决方案1】:

脚本PahoMqttCppConfig.cmake 确实提供PahoMqttCpp_LIBRARIESPahoMqttCpp_INCLUDE_DIRS 等变量。

相反,它提供了代表 PahoMqttCpp 库的 IMPORTED target PahoMqttCpp::PahoMqttCpp。使用这个目标很简单:

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

以此类推,导入目标PahoMqttC::PahoMqttC是为纯C代码提供的。此库在一个与 C++ 一个 PahoMqttCpp::PahoMqttCpp 链接时自动链接。

【讨论】:

  • 亲爱的 Tsyvarev,感谢您的提示!这听起来很合理,但我仍然会遇到错误。我在你回答后发表了评论。我添加了您的行并尝试添加前面的行,但仍然没有成功...
【解决方案2】:

我遇到了同样的错误,我可能已经找到了解决方案。 请尝试如下更改命令。

作为静态库;

target_link_libraries(async_subscribe PahoMqttCpp::paho-mqttpp3-static)

作为共享对象;

target_link_libraries(async_subscribe PahoMqttCpp::paho-mqttpp3)

您可以在 \&lt;prefix\&gt;/lib/cmake/PahoMqttCpp/PahoMqttCppTargets.cmake.

【讨论】:

    【解决方案3】:

    这是一个用于编译 C paho 文件的 cmake 示例:

    cmake_minimum_required(VERSION 3.0)
    project("sub")
    
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") 
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
    
    set(APP_SOURCES sub.c)
    
    # creates ./build/bin/MyExe
    add_executable(${PROJECT_NAME} ${APP_SOURCES})
    target_link_libraries(${PROJECT_NAME} paho-mqtt3c)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多