如何获取 Eclipse Paho MQTT C 客户端的静态库?

How to get static libraries for Eclipse Paho MQTT C client?

我正在尝试在 Ubuntu 16.04 上编译来自 Paho repository 的代码,以便生成静态库:

cd ~/Downloads
git clone https://github.com/eclipse/paho.mqtt.c.git
mkdir /tmp/build.paho
cd /tmp/build.paho
cmake -GNinja -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_STATIC=TRUE ~/Downloads/paho.mqtt.c

该过程总是失败并出现以下错误:

-- The C compiler identification is GNU 5.4.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
-- CMake version: 3.5.1
-- CMake system name: Linux
-- Timestamp is 2019-10-22T09:16:51Z
CMake Error at src/CMakeLists.txt:95 (ADD_LIBRARY):
  add_library cannot create target "paho-mqtt3c" because another target with the same name already exists.  The existing target is a shared library created in source directory "/home/<username>/Downloads/paho.mqtt.c/src".  See documentation for policy CMP0002 for more details.

CMake Error at src/CMakeLists.txt:96 (ADD_LIBRARY):
  add_library cannot create target "paho-mqtt3a" because another target with the same name already exists.  The existing target is a shared library created in source directory "/home/<username>/Downloads/paho.mqtt.c/src".  See documentation for policy CMP0002 for more details.

CMake Error at src/CMakeLists.txt:101 (INSTALL):
  install TARGETS given no LIBRARY DESTINATION for shared library target "paho-mqtt3c".

-- OpenSSL hints: 
-- OpenSSL headers found at /usr/include
-- OpenSSL library found at /usr/lib/aarch64-linux-gnu/libssl.so
-- OpenSSL Crypto library found at /usr/lib/aarch64-linux-gnu/libcrypto.so
CMake Error at src/CMakeLists.txt:165 (ADD_LIBRARY):
  add_library cannot create target "paho-mqtt3cs" because another target with the same name already exists.  The existing target is a shared library created in source directory "/home/<username>/Downloads/paho.mqtt.c/src".  See documentation for policy CMP0002 for more details.

CMake Error at src/CMakeLists.txt:166 (ADD_LIBRARY):
  add_library cannot create target "paho-mqtt3as" because another target with the same name already exists.  The existing target is a shared library created in source directory "/home/<username>/Downloads/paho.mqtt.c/src".  See documentation for policy CMP0002 for more details.

CMake Error at src/CMakeLists.txt:176 (INSTALL):
  install TARGETS given no LIBRARY DESTINATION for shared library target "paho-mqtt3cs".

-- OpenSSL hints: 
-- OpenSSL headers found at /usr/include
-- Configuring incomplete, errors occurred!
See also "/home/<username>/Downloads/paho.mqtt.c/build/CMakeFiles/CMakeOutput.log".

我不明白出了什么问题。据我了解,我完全按照存储库文档的步骤进行操作。

我还需要做什么,或者我需要做哪些不同的事情?

在此存储库中,静态库目标名称与共享库目标名称冲突。 CMake 不允许这样做,因为目标名称必须 唯一 。看起来这些不明智的更改被推送到存储库,这破坏了 PAHO CMake 构建(参见 here)。由于问题已经提出,人们会认为很快就会推出修复程序。

但是,如果您需要快速修复,则可以很轻松地完成这项工作。您可以将 -static 后缀添加到 paho.mqtt.c/src/CMakeLists.txt 文件中的静态库名称上。有两个值得注意的部分需要进行此更改,只需在该文件中搜索 PAHO_BUILD_STATIC:

Near line 94(将 -static 添加到 if 语句中引用的库名称中,在下面以粗体显示):

IF (PAHO_BUILD_STATIC)
    ADD_LIBRARY(paho-mqtt3c-static STATIC $ MQTTClient.c)
    ADD_LIBRARY(paho-mqtt3a-static STATIC $ MQTTAsync.c)

    TARGET_LINK_LIBRARIES(paho-mqtt3c-static ${LIBS_SYSTEM})
    TARGET_LINK_LIBRARIES(paho-mqtt3a-static ${LIBS_SYSTEM})

    INSTALL(TARGETS paho-mqtt3c-static paho-mqtt3a-static
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
ENDIF() 

Near line 164(将 -static 添加到 if 语句中引用的库名称中,在下面以粗体显示):

    IF (PAHO_BUILD_STATIC)
        ADD_LIBRARY(paho-mqtt3cs-static STATIC $ MQTTClient.c SSLSocket.c)
        ADD_LIBRARY(paho-mqtt3as-static STATIC $ MQTTAsync.c SSLSocket.c)

        TARGET_LINK_LIBRARIES(paho-mqtt3cs-static ${OPENSSL_LIBRARIES} ${LIBS_SYSTEM})
        TARGET_LINK_LIBRARIES(paho-mqtt3as-static ${OPENSSL_LIBRARIES} ${LIBS_SYSTEM})
        SET_TARGET_PROPERTIES(
        paho-mqtt3cs-static paho-mqtt3as-static PROPERTIES
        VERSION ${CLIENT_VERSION}
        SOVERSION ${PAHO_VERSION_MAJOR}
        COMPILE_DEFINITIONS "OPENSSL=1")

        INSTALL(TARGETS paho-mqtt3cs-static paho-mqtt3as-static
            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
    ENDIF()

进行更改后,清除您的 CMake 缓存(或删除构建文件夹中的 CMakeCache.txt 文件)并重新 运行 CMake。这应该可以解决问题。