open-source-parsers / jsoncpp

A C++ library for interacting with JSON.
Other
8.06k stars 2.63k forks source link

Can't Cross Compiling jsoncpp 00.11.z for aarch64 #1512

Closed Derrekito closed 11 months ago

Derrekito commented 11 months ago

Can't seem to get cross-compiling working I'm trying to cross compile from x86_64 host for an ARM64 target.

To Reproduce Steps to reproduce the behavior: 1.cmake -DCMAKE_INSTALL_PREFIX=/usr/aarch64-linux-gnu \ -DCMAKE_SYSTEM_NAME=Linux \ -DCMAKE_SYSTEM_PROCESSOR=aarch64 \ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \ -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \ ..\ && make

  1. file lib/libjsoncpp.so.00.11.0 lib/libjsoncpp.so.00.11.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=9b7e6633cefe354d240f3524a8dda7a25abfd683, not stripped

Expected behavior produce an aarch64 object.

Desktop (please complete the following information):

BillyDonahue commented 11 months ago

Does this cmake invocation usually work? Can you cross-compile other projects successfully with those options?

Derrekito commented 11 months ago

I appreciate your response. However, I usually avoid cmake, so I cannot adequately answer your question. But I've tried passing some of these cross-compilation flags directly and also by creating and passing a toolchain file.

https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html

Here's my aarch-toochain.cmake file

# aarch64-toolchain.cmake

# Set the target system name
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

# Set the cross compilers
set(bin_path /usr/bin)
set(CMAKE_C_COMPILER ${bin_path}/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${bin_path}/aarch64-linux-gnu-g++)

# Set the target architecture
set(CMAKE_SYSTEM_PROCESSOR aarch64)

here's my cmake .. -DCMAKE_TOOLCHAIN_FILE=../aarch-toolchain.cmake output

-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- JsonCpp Version: 00.11.0
-- Compiled with C++11(or newer) standard!
-- Looking for C++ include clocale
-- Looking for C++ include clocale - found
-- Looking for localeconv
-- Looking for localeconv - found
-- Looking for C++ include sys/types.h
-- Looking for C++ include sys/types.h - found
-- Looking for C++ include stdint.h
-- Looking for C++ include stdint.h - found
-- Looking for C++ include stddef.h
-- Looking for C++ include stddef.h - found
-- Check size of lconv
-- Check size of lconv - done
-- Performing Test HAVE_DECIMAL_POINT
-- Performing Test HAVE_DECIMAL_POINT - Success
-- Found Python3: /usr/bin/python3.10 (found version "3.10.12") found components: Interpreter
-- Configuring done
-- Generating done
-- Build files have been written to: /home/derrekito/Downloads/jsoncpp/build

It seems to skip looking at CXX processor, which makes me think this has something to do with the project defaulting to my host compiler.

BillyDonahue commented 11 months ago

I don't know cmake very well.

This line is very suspicious:

    -- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++ - skipped

There's sometimes a config log you can look at to see deeper diagnostics? Maybe someone else with more cmake experience can chime in?

Derrekito commented 11 months ago

My CMakeCache.txt seems to suggest that CMAKE is recognizing the cross-compile args, but isn't actually producing a cross-compiled object.

cat CMakeCache.txt | grep aarch64
CMAKE_ADDR2LINE:FILEPATH=/usr/bin/aarch64-linux-gnu-addr2line
CMAKE_AR:FILEPATH=/usr/bin/aarch64-linux-gnu-ar
CMAKE_CXX_COMPILER_AR:FILEPATH=/usr/bin/aarch64-linux-gnu-gcc-ar-11
CMAKE_CXX_COMPILER_RANLIB:FILEPATH=/usr/bin/aarch64-linux-gnu-gcc-ranlib-11
CMAKE_LINKER:FILEPATH=/usr/bin/aarch64-linux-gnu-ld
CMAKE_NM:FILEPATH=/usr/bin/aarch64-linux-gnu-nm
CMAKE_OBJCOPY:FILEPATH=/usr/bin/aarch64-linux-gnu-objcopy
CMAKE_OBJDUMP:FILEPATH=/usr/bin/aarch64-linux-gnu-objdump
CMAKE_RANLIB:FILEPATH=/usr/bin/aarch64-linux-gnu-ranlib
CMAKE_READELF:FILEPATH=/usr/bin/aarch64-linux-gnu-readelf
CMAKE_STRIP:FILEPATH=/usr/bin/aarch64-linux-gnu-strip
CMAKE_TOOLCHAIN_FILE:FILEPATH=/home/derrekito/Downloads/jsoncpp/aarch64-toolchain.cmake
Derrekito commented 11 months ago

CMakeLists.txt is overriding the compiler settings. Commenting out the set(CMAKE_CXX_COMPILER "/usr/bin/g++") line fixed my issue.

file lib/libjsoncpp.so.00.11.0
lib/libjsoncpp.so.00.11.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=c0f7bfaca719ab7e6a28dd6f21a382192cdebff2, not stripped

I'll edit the CMakeLists.txt to check for the flag before clobbering such settings.

if(NOT CMAKE_CXX_COMPILER)
    set(CMAKE_CXX_COMPILER "/usr/bin/g++")
endif()

The only issue now is that tests fail when the compiled object isn't meant for the host system. Should I press with a pull request anyway??