Saturday, October 30, 2021

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

Issue

I have cross-compiled the paho.mqtt.c to the so file and copy them to my lib directory in the workspace. I use the nm and readelf to check it, nothing goes wrong. But I can't use make to compile the sample 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

$ make

 [ 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

here's my 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})

enter image description here


Solution

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


Answered By - Ashechol