cgcccmakecross-compilingbinutils

How to make gcc and linker find dependent libraries when cross compiling?


I'm cross compiling for armv7l linux on x86_64 linux using the following setup:
toolchain.cmake:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf /tmp/rpi-root)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(cross-compile-demo C)

find_package(LibArchive REQUIRED)

add_executable(demo main.c)
target_link_libraries(demo PRIVATE LibArchive::LibArchive)

main.c:

#include <stdio.h>
#include <archive.h>

int main(int argc, char const *argv[])
{
    archive_bzlib_version();
    return 0;
}

When I build the project using ninja, it failed with "library cannot be found" error:

[william@NoteBook build](master)$ ninja -v
[1/2] /usr/bin/arm-linux-gnueabihf-gcc  -isystem /tmp/rpi-root/usr/include -O3 -DNDEBUG -MD -MT CMakeFiles/demo.dir/main.c.o -MF CMakeFiles/demo.dir/main.c.o.d -o CMakeFiles/demo.dir/main.c.o -c /home/william/Downloads/cross-compile/main.c
[2/2] : && /usr/bin/arm-linux-gnueabihf-gcc -O3 -DNDEBUG  CMakeFiles/demo.dir/main.c.o -o demo  -Wl,-rpath,/tmp/rpi-root/usr/lib  /tmp/rpi-root/usr/lib/libarchive.so && :
FAILED: demo 
: && /usr/bin/arm-linux-gnueabihf-gcc -O3 -DNDEBUG  CMakeFiles/demo.dir/main.c.o -o demo  -Wl,-rpath,/tmp/rpi-root/usr/lib  /tmp/rpi-root/usr/lib/libarchive.so && :
/usr/bin/arm-linux-gnueabihf-ld: warning: libcrypto.so.1.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: libacl.so.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: libexpat.so.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: liblzma.so.5, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: libzstd.so.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: liblz4.so.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: libbz2.so.1.0, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: warning: libz.so.1, needed by /tmp/rpi-root/usr/lib/libarchive.so, not found (try using -rpath or -rpath-link)
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_MD_CTX_free@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_resetStreamHC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_compress_HC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_compressStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_freeStreamHC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_alone_decoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_sha512@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_createDStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_sha256@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_raw_decoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_MD_CTX_new@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_aes_192_ecb@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_properties_encode@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflate'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_isError'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_minCLevel'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_ParserCreate'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_stream_decoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_initCStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_CCtx_setParameter'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzlibVersion'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `crc32'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lstat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `HMAC_CTX_free@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_saveDictHC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_stream_encoder_mt@XZ_5.2'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_saveDict'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_permset@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_properties_size@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_freeStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_versionNumber'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzDecompressInit'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `deflateReset'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_raw_encoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzDecompressEnd'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `deflate'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_SetCharacterDataHandler'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_fd@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_stream_encoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_set_tag_type@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_ParserFree'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `HMAC_Init_ex@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_SetUserData'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzCompressInit'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_clear_perms@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `deflateInit2_'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_ripemd160@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_DStreamOutSize'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_freeCStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_Parse'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_EncryptUpdate@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_sha384@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzCompressEnd'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `fstatat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `deflateInit_'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_compress_HC_continue'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_decompress_safe_usingDict'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_crc32@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_getErrorName'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_add_perm@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflateEnd'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_set_file@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `HMAC_CTX_new@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_initDStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzCompress'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_loadDict'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflateSetDictionary'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_entry@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_perm@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_end@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_free@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_set_qualifier@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_code@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_memusage@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_CStreamOutSize'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_compress_fast_continue'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `deflateEnd'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_cputhreads@XZ_5.2'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_decompress_safe'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_CIPHER_CTX_free@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `BZ2_bzDecompress'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `fstat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_aes_128_ecb@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_init@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflateInit_'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_set_fd@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_decompressStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_properties_decode@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_sha1@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_lzma_preset@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_file@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `XML_SetElementHandler'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflateInit2_'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_createStreamHC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_DigestInit@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_DigestUpdate@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lzma_alone_encoder@XZ_5.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_endStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `inflateReset'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_md5@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `HMAC_Final@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `stat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_createStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_loadDictHC'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `PKCS5_PBKDF2_HMAC_SHA1@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_EncryptInit_ex@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `HMAC_Update@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_aes_256_ecb@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_tag_type@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_get_qualifier@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_createCStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `LZ4_compress_default'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_freeDStream'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `acl_create_entry@ACL_1.0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_CIPHER_CTX_new@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `mknod@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `EVP_DigestFinal@OPENSSL_1_1_0'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `ZSTD_maxCLevel'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

The mentioned libraries did exist under /tmp/rpi-root/usr/lib:

[william@NoteBook ~]$ ls /tmp/rpi-root/usr/lib/libcrypto.so*
/tmp/rpi-root/usr/lib/libcrypto.so  /tmp/rpi-root/usr/lib/libcrypto.so.1.1

What might be the issue?

Edit 1 (Additional log for -rpath-link):

According to @n. 1.8e9-where's-my-share m., rpath-link should be used instead of rpath. I created a script with the following content:

#!/bin/bash
/usr/bin/arm-linux-gnueabihf-gcc -isystem /tmp/rpi-root/usr/include -O3 -DNDEBUG -o main.c.o -c /home/william/Downloads/cross-compile/main.c
/usr/bin/arm-linux-gnueabihf-gcc -O3 -DNDEBUG main.c.o -o demo -Wl,-rpath-link,/tmp/rpi-root/usr/lib  /tmp/rpi-root/usr/lib/libarchive.so

The problem seems to be solved but it cannot find any glibc references now:

/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `stat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `fstat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `dladdr@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `dlclose@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `fstat@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `fstatat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `dlerror@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_key_create@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_rwlock_unlock@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_setspecific@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/liblzma.so.5: undefined reference to `pthread_join@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_rwlock_destroy@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_rwlock_wrlock@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `mknod@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libarchive.so: undefined reference to `lstat64@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `dlsym@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_getspecific@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_key_delete@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_rwlock_init@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/liblzma.so.5: undefined reference to `pthread_condattr_setclock@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `stat@GLIBC_2.33'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `dlopen@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_once@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/libcrypto.so.1.1: undefined reference to `pthread_rwlock_rdlock@GLIBC_2.34'
/usr/bin/arm-linux-gnueabihf-ld: /tmp/rpi-root/usr/lib/liblzma.so.5: undefined reference to `pthread_create@GLIBC_2.34'
collect2: error: ld returned 1 exit status

Solution

  • TL;DR

    To solve the issue, you can either:


    Longer Version

    Okay, after doing some research and contacting the developers, there are two reasons why this is happening:

    First, there are two ld in the gnu binutils package, one is the original bfd ld and the other is gold ld. The one I'm using in the question is bfd ld. Gold ld does not check library dependencies, so, if I use gold ld, the error won't happen.

    Second, the reason why rpath cannot find dependent libraries is that, if a cross bfd ld is configured with sysroot option, the sysroot will always be added to the rpath paths. You can confirm the issue by adding --verbose to the linker:

    libcrypto.so.1.1 needed by /tmp/rpi/usr/lib/libarchive.so
    attempt to open /usr/arm-linux-gnueabihf/tmp/rpi/usr/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/arm-linux-gnueabihf/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/local/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/tmp/rpi/usr/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/arm-linux-gnueabihf/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/local/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/lib/libcrypto.so.1.1 failed
    attempt to open /usr/arm-linux-gnueabihf/usr/lib/libcrypto.so.1.1 failed
    

    As for the undefined reference error in the edit section, it simply because the libraries under /tmp/rpi-root/usr/lib/ is compiled with a incompatible version of glibc.

    References:
    https://sourceware.org/bugzilla/show_bug.cgi?id=29288#c7