c++cross-compilingopenwrt

running cross compiled binary on AllWinner device gives not found error


I want to cross compile a C++ application for AllWinner board runing OpenWRT. I've found the toolchain and trying to build Hello world app using CMake as described in CMake docs. So I have allwinner.cmake file containing

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

# which compilers to use for C and C++
set(CMAKE_C_COMPILER  gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/)

# 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)

Also I have CMakeLists.txt with this contents:

project(hello)
add_executable(hello main.cpp)

I am building the app using this command:

cmake -B build -DCMAKE_TOOLCHAIN_FILE=allwinner.cmake
-DCMAKE_BUILD_TYPE=Release . && cmake --build build

After that i receive hello binary, copy it to the board in /tmp and trying to run it there as follows:

# cd /tmp
# ./hello
sh: ./hello: not found

I do not understand why i receive this error and what does it mean. Running ldd hello gives me:

# ldd hello
    /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
    libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
    libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)

This output looks as something correct.

Could anybody explain me what i am doing wrong?


Solution

  • I used invalid toolchain. It was detected when I compared two ldd outputs. As I've wrote earlier my binary gives this:

    # ldd hello
        /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
        libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
        libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
    

    And ldd of system's ls gives this:

    # ldd /bin/ls
        /lib/ld-musl-aarch64.so.1 (0x7f7b940000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7f7b920000)
        libc.so => /lib/ld-musl-aarch64.so.1 (0x7f7b940000)
    

    As you can see the difference is in libc. My binary wants to use ld-linux-aarch64 and ls binary use ld-musl-aarch64. I've downloaded aarch64-musl cross-compiler, built my binary using it and voila, it works now.