当我使用 CMake 交叉编译客户端时,对 `MQTTClient_create' 的未定义引用

Undefined reference to `MQTTClient_create' When I use CMake to cross compile the client

我已经将paho.mqtt.c交叉编译成so文件,复制到我工作区的lib目录下。我使用 nm 和 readelf 检查它,没有出错。但是我不能使用 make 来编译示例 client.c

$ readelf -h libpaho-mqtt3c.so

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x2a50
  Start of program headers:          52 (bytes into file)
  Start of section headers:          168104 (bytes into file)
  Flags:                             0x5000202, Version5 EABI, soft-float ABI, <unknown>
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         4
  Size of section headers:           40 (bytes)
  Number of section headers:         31
  Section header string table index: 28

$ nm libpaho-mqtt3c.so

00004c44 t MQTTClient_cleanSession
00004acc t MQTTClient_closeSession
000061d0 T MQTTClient_connect
00006234 T MQTTClient_connect5
000062e4 t MQTTClient_connectAll
000058c0 t MQTTClient_connectURI
0000503c t MQTTClient_connectURIVersion
000033b4 T MQTTClient_create
00002db8 T MQTTClient_createWithOptions
000086a0 t MQTTClient_cycle
00003aac t MQTTClient_deliverMessage
00003608 T MQTTClient_destroy
00006be8 T MQTTClient_disconnect

$ 制作

 [ 50%] Linking C executable ../bin/pub
    arm-openwrt-linux-uclibcgnueabi-gcc: warning: environment variable 'STAGING_DIR' not defined
    arm-openwrt-linux-uclibcgnueabi-gcc: warning: environment variable 'STAGING_DIR' not defined
    CMakeFiles/pub.dir/src/pub.c.o: In function `main':
    pub.c:(.text+0x78): undefined reference to `MQTTClient_create'
    pub.c:(.text+0x9c): undefined reference to `MQTTClient_connect'
    pub.c:(.text+0x100): undefined reference to `MQTTClient_publishMessage'
    pub.c:(.text+0x140): undefined reference to `MQTTClient_waitForCompletion'
    pub.c:(.text+0x168): undefined reference to `MQTTClient_disconnect'
    pub.c:(.text+0x174): undefined reference to `MQTTClient_destroy'
    collect2: error: ld returned 1 exit status
    make[2]: *** [CMakeFiles/pub.dir/build.make:85: ../bin/pub] Error 1
    make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/pub.dir/all] Error 2
    make: *** [Makefile:84: all] Error 2

这是我的 CMakeLists

cmake_minimum_required(VERSION 3.16)

project(CPE LANGUAGES C)

set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_C_COMPILER ${PROJECT_SOURCE_DIR}/toolchain/bin/arm-openwrt-linux-uclibcgnueabi-gcc)

set(CMAKE_CXX_COMPILER ${PROJECT_SOURCE_DIR}/toolchain/bin/arm-openwrt-linux-uclibcgnueabi-g++)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

include_directories(${PROJECT_SOURCE_DIR}/include)

# aux_source_directory(./src SRC_LIST)
set(SRC_LIST ${PROJECT_SOURCE_DIR}/src/pub.c)

find_library(LIB ${PROJECT_SOURCE_DIR}/lib)

add_executable(pub ${SRC_LIST})

target_link_libraries(pub ${LIB})

- find_library(LIB ${PROJECT_SOURCE_DIR}/lib)
+ find_library(LIB paho-mqtt3c ${PROJECT_SOURCE_DIR}/lib)