Can I point CMake to use the (older) libc of a different rootfs?

Hi all! I’m trying to set up CMake to cross compile an application targeted to an old (2017) Linux system (functioning, but slow). I managed to write a toolchain file and I can compile it, but of course the libc picked is the one present on my system (which is way newer than the one present on the target). So I made a copy of the target root fs onto my machine, in /opt/box-root-fs. My idea is that by setting CMAKE_FIND_ROOT_PATH and CMAKE_SYSROOT the compiler would have looked into that root fs, but apparently that’s not the case. If I try to print the compilation commands with VERBOSE=1 cmake I see something like:

/usr/bin/arm-linux-gnueabihf-g++ --sysroot=/opt/box-root-fs   -g -std=gnu++14 -MD -MT CMakeFiles/two_lidars.dir/main.cpp.o -MF CMakeFiles/two_lidars.dir/main.cpp.o.d -o CMakeFiles/two_lidars.dir/main.cpp.o -c /home/abertulli/two_lidars/main.cpp

so the --sysroot is correctly set, but then if I try to print with -v the precise include dirs, I find:

abertulli@SAN34400:~/two_lidars/build$ /usr/bin/arm-linux-gnueabihf-g++ --sysroot=/opt/box-root-fs   -g -std=gnu++14 -MD -MT CMakeFiles/two_lidars.dir/main.cpp.o -MF CMakeFiles/two_lidars.dir/main.cpp.o.d -o CMakeFiles/two_lidars.dir/main.cpp.o -c /home/abertulli/two_lidars/main.cpp -v
Using built-in specs.
COLLECT_GCC=/usr/bin/arm-linux-gnueabihf-g++
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --without-target-system-zlib --enable-multiarch --disable-sjlj-exceptions --with-arch=armv7-a+fp --with-float=hard --with-mode=thumb --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --includedir=/usr/arm-linux-gnueabihf/include --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
COLLECT_GCC_OPTIONS='-g' '-std=gnu++14' '-MD' '-MT' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-MF' 'CMakeFiles/two_lidars.dir/main.cpp.o.d' '-o' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-c' '-v' '-shared-libgcc'  '-mfloat-abi=hard' '-mtls-dialect=gnu' '-mthumb' '-mlibarch=armv7-a+fp' '-march=armv7-a+fp' '-dumpdir' 'CMakeFiles/two_lidars.dir/'
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/cc1plus -quiet -v -imultilib . -imultiarch arm-linux-gnueabihf -isysroot /opt/box-root-fs -MD CMakeFiles/two_lidars.dir/main.cpp.d -MF CMakeFiles/two_lidars.dir/main.cpp.o.d -MT CMakeFiles/two_lidars.dir/main.cpp.o -D_GNU_SOURCE /home/abertulli/two_lidars/main.cpp -quiet -dumpdir CMakeFiles/two_lidars.dir/ -dumpbase main.cpp.cpp -dumpbase-ext .cpp -mfloat-abi=hard -mtls-dialect=gnu -mthumb -mlibarch=armv7-a+fp -march=armv7-a+fp -g -std=gnu++14 -version -fstack-protector-strong -Wformat -Wformat-security -o /tmp/ccC1Bx16.s
GNU C++14 (Ubuntu 11.4.0-1ubuntu1~22.04) version 11.4.0 (arm-linux-gnueabihf)
        compiled by GNU C version 11.4.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/box-root-fs/usr/local/include/arm-linux-gnueabihf"
ignoring nonexistent directory "/usr/lib/gcc-cross/arm-linux-gnueabihf/11/include-fixed"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/include/c++/11
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/include/c++/11/arm-linux-gnueabihf/.
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/include/c++/11/backward
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/include
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/include
 /opt/box-root-fs/usr/include/arm-linux-gnueabihf
 /opt/box-root-fs/usr/include
End of search list.
GNU C++14 (Ubuntu 11.4.0-1ubuntu1~22.04) version 11.4.0 (arm-linux-gnueabihf)
        compiled by GNU C version 11.4.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f8dab96d7e7b711d866ac6ec96f46fba
COLLECT_GCC_OPTIONS='-g' '-std=gnu++14' '-MD' '-MT' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-MF' 'CMakeFiles/two_lidars.dir/main.cpp.o.d' '-o' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-c' '-v' '-shared-libgcc'  '-mfloat-abi=hard' '-mtls-dialect=gnu' '-mthumb' '-mlibarch=armv7-a+fp' '-march=armv7-a+fp' '-dumpdir' 'CMakeFiles/two_lidars.dir/'
 /usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/bin/as -v -march=armv7-a -mfloat-abi=hard -meabi=5 -o CMakeFiles/two_lidars.dir/main.cpp.o /tmp/ccC1Bx16.s
GNU assembler version 2.38 (arm-linux-gnueabihf) using BFD version (GNU Binutils for Ubuntu) 2.38
COMPILER_PATH=/usr/lib/gcc-cross/arm-linux-gnueabihf/11/:/usr/lib/gcc-cross/arm-linux-gnueabihf/11/:/usr/lib/gcc-cross/arm-linux-gnueabihf/:/usr/lib/gcc-cross/arm-linux-gnueabihf/11/:/usr/lib/gcc-cross/arm-linux-gnueabihf/:/usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/bin/
LIBRARY_PATH=/usr/lib/gcc-cross/arm-linux-gnueabihf/11/:/usr/lib/gcc-cross/arm-linux-gnueabihf/11/../../../../arm-linux-gnueabihf/lib/:/opt/box-root-fs/lib/arm-linux-gnueabihf/:/opt/box-root-fs/lib/:/opt/box-root-fs/usr/lib/arm-linux-gnueabihf/:/opt/box-root-fs/usr/lib/
COLLECT_GCC_OPTIONS='-g' '-std=gnu++14' '-MD' '-MT' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-MF' 'CMakeFiles/two_lidars.dir/main.cpp.o.d' '-o' 'CMakeFiles/two_lidars.dir/main.cpp.o' '-c' '-v' '-shared-libgcc'  '-mfloat-abi=hard' '-mtls-dialect=gnu' '-mthumb' '-mlibarch=armv7-a+fp' '-march=armv7-a+fp' '-dumpdir' 'CMakeFiles/two_lidars.dir/main.cpp.'

so the search for include directories still uses the system ones, I think, and I presume the search for libraries does too. How can I make it search for the old libraries present in the copied fs? Thanks!


CMakeLists.txt:

cmake_minimum_required (VERSION 3.7)
PROJECT(two_lidars)

# Change as wished
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE "Debug")

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

###########
# toojpeg #
###########
add_library(toojpeg toojpeg/toojpeg.cpp)

target_include_directories(toojpeg PUBLIC ${PROJECT_SOURCE_DIR}/include)


################
# single lidar #
################

set(SINGLE_LIDAR_NAME single_lidar_listener)

set(single_lidar_files
  single_lidar.cpp
  sick_scan_xd_api_wrapper.c
)

add_executable(${SINGLE_LIDAR_NAME} ${single_lidar_files})

# needs toojpeg
target_link_libraries(${SINGLE_LIDAR_NAME} toojpeg)

# set(SINGLE_LIDAR_INCLUDE_DIRS ${INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include)

# need sick
find_package(sick_scan_xd REQUIRED)
set(SINGLE_LIDAR_INCLUDE_DIRS ${SINGLE_LIDAR_INCLUDE_DIRS} ${SICK_SCAN_XD_INCLUDE_DIR})

include(CMakePrintHelpers)
cmake_print_variables(SICK_SCAN_XD_LIBRARIES SICK_SCAN_XD_INCLUDE_DIR)

target_include_directories(${SINGLE_LIDAR_NAME} PUBLIC ${SINGLE_LIDAR_INCLUDE_DIRS})
target_include_directories(${SINGLE_LIDAR_NAME} PUBLIC /usr/local/include)
target_link_libraries(${SINGLE_LIDAR_NAME} ${SICK_SCAN_XD_LIBRARIES})
target_link_libraries(${SINGLE_LIDAR_NAME} ${CMAKE_DL_LIBS})

########
# main #
########

set(main_files
  main.cpp
)

add_executable(${PROJECT_NAME} ${main_files})

toolchain file TC-arm-linux-gnueabihf.cmake:

# the name of the target operating system
set(CMAKE_SYSTEM_NAME Linux)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER   arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH /opt/box-root-fs)
set(CMAKE_SYSROOT /opt/box-root-fs)

# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

I invoke the configuration with

mkdir -p ./build/ && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../TC-arm-linux-gnueabihf.cmake ..

and then make. The directory /opt/box-root-fs is the / of the target system

EDIT: moved to stackoverflow