Changes between Version 26 and Version 27 of ubuntu


Ignore:
Timestamp:
04/13/2022 08:02:37 PM (3 years ago)
Author:
Tim Harvey
Comment:

added cross-compile examples

Legend:

Unmodified
Added
Removed
Modified
  • ubuntu

    v26 v27  
    353353}}}
    354354
    355 
    356355[=#hello-world.c]
     356== Cross compiling example
     357If you wish to compile applications on a development host intended to run on a target with a different architecture (ie you have an x86 host, and an arm64 target like Venice or Newport) you can use Ubuntu's cross toolchain which consits of a gcc cross-compiler, libc, and binutils.
     358
     359Example:
     360 * cross compile for ARM64 (Venice / Newport) on X86 Ubuntu host:
     361{{{#!bash
     362# install Ubuntu cross-build package
     363sudo apt update
     364sudo apt install crossbuild-essential-arm64 # installs gcc as well as binutils and libs
     365aarch64-linux-gnu-gcc -v # see gcc version if interested
     366# build a simple app, using shared libraries
     367cat << EOF >> helloworld.c
     368#include <stdio.h>
     369
     370int main (int argc, char** argv)
     371{
     372   printf("Hello World\n");
     373   return 0;
     374}
     375EOF
     376aarch64-linux-gnu-gcc helloworld.c -o helloworld
     377file helloworld # shows ELF 64-bit LSB shared object, ARM aarch64, dynamically linked with interpreter /lib/ld-linux-aarch64.so.1
     378aarch64-linux-gnu-objdump -x helloworld | grep NEEDED # show libs needed on target
     379}}}
     380  - this was tested on both an Ubuntu bionic dev host (providing gcc-v7.5) as well as an Ubuntu focal dev host (providing gcc-v9.4.0)
     381 * cross compile for ARM32 (Ventana) on X86 Ubuntu host:
     382{{{#!bash
     383# install Ubuntu cross-build package
     384sudo apt update
     385sudo apt install crossbuild-essential-armel # installs gcc as well as binutils and libs
     386arm-linux-gnueabi-gcc -v # see gcc version if interested
     387# build a simple app, using shared libraries
     388cat << EOF >> helloworld.c
     389#include <stdio.h>
     390
     391int main (int argc, char** argv)
     392{
     393   printf("Hello World\n");
     394   return 0;
     395}
     396EOF
     397arm-linux-gnueabi-gcc helloworld.c -o helloworld
     398file helloworld # shows ELF 32-bit LSB ARM EABI5v1 shared object, dynamically linked with interpreter /lib/ld-linux.so.3
     399arm-linux-gnueabi-objdump -x helloworld | grep NEEDED # show libs needed on target
     400}}}
     401
     402
     403
    357404== Native compiling example
    358405While typically not as fast as cross-compiling on a higher end processing Linux development host it can be simple to compile ANSI-C code natively on a Gateworks board running an Ubuntu OS.