1

I try to compile for ARM 32bit a C project, which contains links to zlib & minizip static libraries. I work with CLion IDE on Ubuntu 20. I had installed toolchain:

sudo apt-get install gcc-arm-linux-gnueabihf

Then I downloaded ARM deb packages, from which I extracted libz.a & libminizip.a:

zlib1g-dev_1.2.11.dfsg-2ubuntu1_armhf.deb
libminizip-dev_1.1-8build1_armhf.deb

Then I try to compile, but have:

====================[ Build | all | Default-System ]============================
/home/user/external/clion-2023.1.1/bin/cmake/linux/x64/bin/cmake --build /home/user/Clion/myproject/cmake-build-default-system --target all -j 1
[3/3] Linking C executable updater
FAILED: updater 
: && /usr/bin/arm-linux-gnueabihf-gcc -fsanitize=address  CMakeFiles/updater.dir/main.c.o CMakeFiles/updater.dir/base64.c.o -o updater  /home/user/Clion/myproject/lib/arm/libz.a  /home/user/Clion/myproject/lib/arm/libminizip.a  /home/user/Clion/myproject/lib/arm/libjansson.a && :
/usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: /home/user/Clion/myproject/lib/arm/libminizip.a(unzip.o): in function «unzReadCurrentFile»:
(.text+0x144e): undefined link to «crc32»

Here is CmakeLists.txt:

cmake_minimum_required(VERSION 3.17)
project(updater C)

set(CMAKE_C_STANDARD 99)

add_executable(updater main.c unpack.h)


set(ZLIB_INCLUDE_DIR /home/user/Clion/myproject/zlib-arm/include)
set(ZLIB_LIBRARY /home/user/Clion/myproject/lib/arm/libz.a)
set(MINIZIP_LIBRARY /home/user/Clion/myproject/lib/arm/libminizip.a)
target_include_directories(updater PRIVATE ${ZLIB_INCLUDE_DIR})
target_link_libraries(updater PRIVATE ${ZLIB_LIBRARY})
target_link_libraries(updater PRIVATE ${MINIZIP_LIBRARY})

The survey of the libz.a seems OK:

nm /home/user/Clion/myproject/lib/arm/libz.a | grep crc32
crc32.o:
00000645 T crc32
0000064d T crc32_combine
00000481 t crc32_combine_
00000651 T crc32_combine64
00000001 t crc32_little
0000063d T crc32_z
         U crc32
         U crc32

I suspect that downloaded libraries might be incompatible. But what to replace them with? Or build them manually - but how? Any advice?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    Just a guess ... Try reversing the order of the `target_link_libraries` statements. I presume that `MINIZIP_LIBRARY` wants to call things in `ZLIB_LIBRARY` but `ZLIB_LIBRARY` comes _first_ in the [generated] linker command. linkers do one pass through the `.a` arguments, so when it sees the minizip `.a`, it's already to late to pull in stuff from the zlib `.a` file. You may need to adjust the link order of `libjansson.a` as well. – Craig Estey Apr 11 '23 at 01:47
  • I did as you said, and you guess is great! It works! – Олег Сидоров Apr 11 '23 at 17:55
  • I recognized the link ordering issue from the output. But, I said "guess" simply because I'm not super familiar with `cmake`, so I wasn't sure if changing the `target_link_libraries` would work. For more on the ordering issue, see my answer: [gcc ld: method to determine link order of static libraries](https://stackoverflow.com/a/34168951/5382650) – Craig Estey Apr 11 '23 at 21:38

1 Answers1

1

Your link command:

arm-linux-gnueabihf-gcc ... libz.a libminizip.a ...

is incorrect: since libminzip.a requires symbols from libz.a, it should be before libz.a on the link line.

To understand this better, read this post.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362