Skip to content

Conversation

@MambaWong
Copy link
Contributor

@MambaWong MambaWong commented Nov 7, 2025

When cross-compiling, CMake miss the correct library files.

  1. CMAKE_FIND_ROOT_PATH can interfere with OpenBLAS detection, causing CMake to miss the correct library include files.
  2. When using find_package(OpenBLAS QUIET), OpenBLAS_LIBRARIES does not contain OpenBLAS::OpenBLAS.
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x278): undefined reference to `sgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x414): undefined reference to `sgeqrf_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x498): undefined reference to `sgeqrf_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x4fc): undefined reference to `dgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x52c): undefined reference to `dgeqrf_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x6c4): undefined reference to `sgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x75c): undefined reference to `sgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x834): undefined reference to `dgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x8bc): undefined reference to `dgeqrf_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x918): undefined reference to `dgels_'
     aarch64-linux-gnu/bin/ld: hal_internal.cpp:(.text._Z12lapack_QR64fPdmiiiS_mS_Pi+0x950): undefined reference to `dgels_'
    

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

…h OpenBLAS detection, causing CMake to miss the correct library files.
@MambaWong MambaWong changed the title fix: during cross-compilation, CMAKE_FIND_ROOT_PATH can interfere with OpenBLAS detection, causing CMake to miss the correct library files. fix: during cross-compilation, CMake miss the correct library files. Nov 9, 2025
@MambaWong MambaWong changed the title fix: during cross-compilation, CMake miss the correct library files. fix: When cross-compiling, CMake miss the correct library files. Nov 9, 2025
@asmorkalov asmorkalov added this to the 4.13.0 milestone Nov 10, 2025
@asmorkalov
Copy link
Contributor

The solution looks a bit wired. Could you describe your use-case: compiler, toolchain file, the way how the BLAS is searched, etc?

@MambaWong
Copy link
Contributor Author

MambaWong commented Nov 10, 2025

I first cross-compiled OpenBLAS, and then used this previously cross-compiled OpenBLAS when cross-compiling OpenCV.

  1. Host machine (a Docker container running on WSL2)

NAME="Ubuntu"
VERSION="20.04.6 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.6 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

  1. Cross compiler
aarch64-mix210-linux-gcc -v

Using built-in specs.
COLLECT_GCC=aarch64-mix210-linux-gcc
COLLECT_LTO_WRAPPER=/opt/linux/x86-arm/aarch64-mix210-linux/host_bin/../libexec/gcc/aarch64-linux-gnu/7.3.0/lto-wrapper
Target: aarch64-linux-gnu
Configured with: /home/pub/workspace/toolchaines/build/aarch64-mix210-linux/../../open_source/aarch64-mix210-linux_src/gcc-7.3.0/configure --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=aarch64-linux-gnu --with-arch=armv8-a --prefix=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210 --enable-threads --disable-libmudflap -enable-libssp --enable-gold=yes --disable-libstdcxx-pch --disable-multilib --enable-multiarch --with-gnu-as --with-gnu-ld --enable-libgomp --enable-gnu-indirect-function --enable-checking=yes --enable-lto --enable-c99 --enable-long-long --disable-nls --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419 --enable-shared --with-pkgversion='HC&C V1R3C00SPC200B042_20221123' --enable-languages=c,c++ --with-headers=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210/target/usr/include --with-sysroot=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210/target --with-build-sysroot=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210/target --with-gmp=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/obj/host-libs/usr --with-mpfr=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/obj/host-libs/usr --with-mpc=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/obj/host-libs/usr --with-build-time-tools=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210/aarch64-linux-gnu/bin --libdir=/home/pub/workspace/toolchaines/build/aarch64-mix210-linux/aarch64_mix210_build_dir/aarch64_mix210/lib --disable-bootstrap --with-system-zlib
Thread model: posix
gcc version 7.3.0 (HC&C V1R3C00SPC200B042_20221123)

  1. toolchain file

set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(GCC_COMPILER_VERSION "" CACHE STRING "GCC Compiler version")
set(GNU_MACHINE "aarch64-mix210-linux" CACHE STRING "GNU compiler triple")
include("${CMAKE_CURRENT_LIST_DIR}/arm.toolchain.cmake")

  1. find OpenBLAS
  • case a

    -DOpenBLAS_DIR=${INSTALL_PREFIX}/lib/cmake/OpenBLAS

    The method used in OpenCV during cross-compilation.

    find_package(OpenBLAS QUIET)

    find_package(OpenBLAS QUIET)

    In this case, OpenBLAS_LIBRARIES is similar to m;OpenMP::OpenMP_C and does not contain OpenBLAS::OpenBLAS. This will cause functions from the OpenBLAS library to be missing at link time.

  • case b

    export OpenBLAS_HOME=${INSTALL_PREFIX}

    find_library(OpenBLAS_LIBRARIES NAMES openblas PATHS ENV "OpenBLAS" ENV "OpenBLAS_HOME" PATH_SUFFIXES "lib" NO_DEFAULT_PATH)
    find_path(OpenBLAS_INCLUDE_DIRS NAMES cblas.h PATHS ENV "OpenBLAS" ENV "OpenBLAS_HOME" PATH_SUFFIXES "include" NO_DEFAULT_PATH)
    find_path(OpenBLAS_LAPACKE_DIR NAMES lapacke.h PATHS "${OpenBLAS_INCLUDE_DIRS}" ENV "OpenBLAS" ENV "OpenBLAS_HOME" PATH_SUFFIXES "include" NO_DEFAULT_PATH)

    In this case, these three lines are unable to locate the corresponding header and library files, even though I am absolutely certain that they exist.
    However, if I add NO_CMAKE_FIND_ROOT_PATH, the files can be found correctly.

In both cases, the line below cannot find the files unless I add NO_CMAKE_FIND_ROOT_PATH.

find_path(${VAR} "${NAME}" ${ARGN} NO_DEFAULT_PATH)


set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${ARM_LINUX_SYSROOT})

When cross-compiling, the toolchain defines CMAKE_FIND_ROOT_PATH. In the CMake documentation, it is described as:

The CMake variable CMAKE_FIND_ROOT_PATH specifies one or more directories to be prepended to all other search directories.

It might be more appropriate to modify it by adding a check for CMAKE_CROSSCOMPILING.

if(CMAKE_CROSSCOMPILING)
  find_path(${VAR} "${NAME}" ${ARGN} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
else()
  find_path(${VAR} "${NAME}" ${ARGN} NO_DEFAULT_PATH)
endif()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants