Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5511

C/C++ • Re: CMake and arch-specific compiler options

$
0
0
I used the mk-sbuild wrapper, but as there was no discernible difference in disk space, and no folders magically appeared in /var/lib/schroot/chroots, I concluded that it had not worked.
Which command did you run exactly? Did you run it twice? The first time, it will ask you to edit a config file, and then prompt you to log out and back in again before continuing. After you do so, you'll need to run the same command again.

The output of a successful run looks like this:

Code:

$ mk-sbuild --arch=armhf bookworm --debootstrap-mirror=http://raspbian.raspberrypi.org/raspbian --name=rpi3b-bookworm --debootstrap-keyring "$HOME/.gnupg/pubring.kbx --merged-usr" --skip-proposed --skip-updates --skip-securityI: Target architecture can be executedI: Retrieving InRelease I: Checking Release signatureI: Valid Release signature (key id A0DA38D0D76E8B5D638872819165938D90FDDD2E)I: Retrieving Packages I: Validating Packages I: Retrieving Packages I: Validating Packages I: Retrieving Packages I: Validating Packages I: Resolving dependencies of required packages...I: Resolving dependencies of base packages...I: Checking component main on http://raspbian.raspberrypi.org/raspbian...I: Retrieving adduser 3.134[...]Done building rpi3b-bookworm-armhf. To CHANGE the golden image: sudo schroot -c source:rpi3b-bookworm-armhf -u root To ENTER an image snapshot: schroot -c rpi3b-bookworm-armhf To BUILD within a snapshot: sbuild -A -d rpi3b-bookworm-armhf PACKAGE*.dsc
I just ran this on a RPi 3B+ running Ubuntu 22.04 (64-bit). (I don't have any SD cards with 64-bit RPi OS handy, sorry.)

Then you can install any packages you need using APT:

Code:

$ sudo sbuild-apt rpi3b-bookworm-armhf apt-get install zlib1g-devI: apt-get -oAPT::Get::Assume-Yes=true install zlib1g-devReading package lists... DoneBuilding dependency tree... DoneReading state information... DoneThe following NEW packages will be installed:  zlib1g-dev0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Need to get 901 kB of archives.After this operation, 1303 kB of additional disk space will be used.Get:1 http://ftp.halifax.rwth-aachen.de/raspbian/raspbian bookworm/main armhf zlib1g-dev armhf 1:1.2.13.dfsg-1 [901 kB][...]
You can then verify that the package is installed in the sysroot:

Code:

$ schroot -c rpi3b-bookworm-armhf --directory=/(rpi3b-bookworm-armhf)ubuntu@rpi3:/$ find /usr/include -name zlib.h/usr/include/zlib.h
Or from outside of the chroot:

Code:

$ find /var/lib/schroot/chroots -name zlib.h/var/lib/schroot/chroots/rpi3b-bookworm-armhf/usr/include/zlib.h
Let's now cross-compile the simplest possible C++ program with a non-trivial dependency: it simply includes Zlib and prints its version.

Code:

// main.cpp#include <cstdio>#include <zlib.h>int main() {    std::puts(zlibVersion());}

Code:

# CMakeLists.txtcmake_minimum_required(VERSION 3.16)project(example LANGUAGES CXX)find_package(ZLIB REQUIRED)add_executable(example main.cpp)target_link_libraries(example PRIVATE ZLIB::ZLIB)

Code:

# toolchain.cmake# System informationset(CMAKE_SYSTEM_NAME "Linux")set(CMAKE_SYSTEM_PROCESSOR "armv8")set(CROSS_GNU_TRIPLE "arm-linux-gnueabihf" CACHE STRING "The GNU triple of the toolchain to use")set(CMAKE_LIBRARY_ARCHITECTURE "arm-linux-gnueabihf")set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "armhf")# Compiler flags for your target (e.g. 32-bit RPi 3B+)set(CMAKE_C_FLAGS_INIT       "-mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard")set(CMAKE_CXX_FLAGS_INIT     "-mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard")set(CMAKE_Fortran_FLAGS_INIT "-mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard")# Sysroot and search path configurationset(CMAKE_SYSROOT "/var/lib/schroot/chroots/rpi3b-bookworm-armhf")set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)# Compiler binariesset(CMAKE_C_COMPILER "${CROSS_GNU_TRIPLE}-gcc-12" CACHE FILEPATH "C compiler")set(CMAKE_CXX_COMPILER "${CROSS_GNU_TRIPLE}-g++-12" CACHE FILEPATH "C++ compiler")set(CMAKE_Fortran_COMPILER "${CROSS_GNU_TRIPLE}-gfortran-12" CACHE FILEPATH "Fortran compiler")
Put those three files (main.cpp, CMakeLists.txt, toolchain.cmake) in the same folder somewhere, and open a terminal in that folder. Then just build the project using CMake, copy it to the chroot (or to your target), and run it:

Code:

$ cmake -S . -B build --toolchain=toolchain.cmake # Configure-- The CXX compiler identification is GNU 12.3.0-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Check for working CXX compiler: /usr/bin/arm-linux-gnueabihf-g++-12 - skipped-- Detecting CXX compile features-- Detecting CXX compile features - done-- Found ZLIB: /var/lib/schroot/chroots/rpi3b-bookworm-armhf/usr/lib/arm-linux-gnueabihf/libz.a (found version "1.2.13") -- Configuring done-- Generating done-- Build files have been written to: /home/ubuntu/build

Code:

$ cmake --build build -j4 # Build[ 50%] Building CXX object CMakeFiles/example.dir/main.cpp.o[100%] Linking CXX executable example[100%] Built target example

Code:

$ sudo cp build/example /var/lib/schroot/chroots/rpi3b-bookworm-armhf # Copy to chroot$ schroot -c rpi3b-bookworm-armhf --directory=/ ./example # Run the program1.2.13
To keep the chroots up-to-date, you can use the following command. You can specify multiple schroots in the same command.

Code:

sudo sbuild-update --update --upgrade rpi3b-bookworm-armhf
Let me know if anything is unclear.

Statistics: Posted by tttapa — Thu Aug 29, 2024 6:49 pm



Viewing all articles
Browse latest Browse all 5511

Trending Articles