[LINUX] Cross-compile for Raspberry Pi Zero on Debian-Create your own shared library

Overview

This is a continuation of these articles.

Build a simple shared library and a program that calls the shared library on Debian (of VirtualBox on Windows 10) and run it on a Raspberry Pi.

environment

Host: Debian 10.6.0 <Virtual Box 6.1 <WIndows10 Target: Raspberry Pi 3 Model A +

Last time has changed from Raspberry Pi Zero to Raspberry Pi 3. The reason is that the cross-compilation of Raspberry Pi Zero for ARM v6 CPUs did not work well.

The Raspberry Pi 3 for ARM v8 CPUs passed easily.

It sounds like a compiler bug, but if anyone knows what's wrong, please let me know.

This program

There is nothing inside, but the trial program looks like this.

test_so_lib.h


#ifndef TEST_SO_LIB_H
#define TEST_SO_LIB_H

void print_hoge (int num);

#endif

test_so_lib.c


#include <stdio.h>
#include "test_so_lib.h"

void
print_hoge (int num)
{
  int i;
  for (i = 0; i < num; i++) {
    printf("hogehoge\n");
  }
}

main.c


#include "test_so_lib.h"

int
main (void)
{
  print_hoge(5);
  return 0;
}

If you build this normally and run it, it looks like this.

$ gcc -shared test_so_lib.c -o libtestso.so
$ gcc test_so_main.c -o test_so_main -I./ -L./ -Wl,-rpath=./ -ltestso
$ ./test_so_main
hogehoge
hogehoge
hogehoge
hogehoge
hogehoge

Try cross-compiling

For Raspberry Pi Zero

A brief story about the bad Raspberry Pi Zero ... The build environment is as shown in Last article. It looks like this in that environment ...

Debian


$ arm-linux-gnueabi-gcc -march=armv6 -shared test_so_lib.c -o libtestso_armv6.so
$ arm-linux-gnueabi-gcc -march=armv6 test_so_main.c -o test_so_main_armv6 -I./ -L./ -Wl,-rpath=./ -ltestso_armv6

Copy test_so_main_armv6, libtestso_armv6.so created in the build to Raspberry Pi Zero and run it.

RaspberryPiZero


$ ./test_so_main_armv6
./test_so_main_armv6: error while loading shared libraries: libtestso_armv6.so: cannot open shared object file: No such file or directory

I got an error. It looks like the shared library libtestso_armv6.so can't be found. I'm not sure why.

For Raspberry Pi 3

Build environment

Use the cross-compiler for armhf to build the ARM v8 series of Raspberry Pi 3. In Previous article, the place where armel is written is corrected to armhf and it looks like this.

installation of armhf cross compiler

$ sudo apt install crossbuild-essential-armhf

Addition of architecture

$ sudo dpkg --add-architecture armhf
$ sudo nano /etc/apt/sources.list

/etc/apt/sources.list


deb [arch=armel,armhf] http://ftp.jp.debian.org/debian buster main

I don't use armel this time, but if you want to use both, you should write like this.

$ sudo apt update
$ sudo apt install libusb-dev:armhf
$ sudo apt install libjpeg-dev:armhf

By the way, this time I will only use my own shared library, so I only need to install crossbuild-essential-armhf. (I wrote a memo for myself so far)

Build

The architecture specified by -march is armv8-a. (It seems that armv8 cannot be specified)

Debian


$ arm-linux-gnueabihf-gcc -march=armv8-a -shared test_so_lib.c -o libtestso_armv8a.so
$ arm-linux-gnueabihf-gcc -march=armv8-a test_so_main.c -o test_so_main_armv8a -I./ -L./ -Wl,-rpath=./ -ltestso_armv8a

Copy test_so_main_armv8a and libtestso_armv8a.so created in the build to Raspberry Pi 3 and execute.

RaspberryPi3


$ ./test_so_main_armv8a
hogehoge
hogehoge
hogehoge
hogehoge
hogehoge

It moved easily.

Summary

I built it for armv6 with arm-linux-gnueabi-gcc and was worried for a while because it didn't work with Raspberry Pi Zero, but it's the same for armv8-a with arm-linux-gnueabihf-gcc When I build it according to the procedure and see that it works easily, I wonder if arm-linux-gnueabi-gcc itself is a little suspicious.

Maybe there are some options required at build time or the environment is not enough, but I was not sure, so please let me know if anyone can understand it.

Recommended Posts

Cross-compile for Raspberry Pi Zero on Debian-Create your own shared library
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
[Python] Register your own library on PyPI
Mount Windows shared folder on Raspberry Pi
Power on / off your PC with raspberry pi
Play with your Ubuntu desktop on your Raspberry Pi 4
pigpio on Raspberry pi
Control brushless motors with GPIOs on Raspberry Pi Zero
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Cython on Raspberry Pi
Run AWS IoT Device SDK for Python on Raspberry Pi
Access google spreadsheet using python on raspberry pi (for myself)
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Call your own C language shared library from Python using ctypes
Testing uart communication on Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Try implementing k-NN on your own
Raspberry pi initial setting (for myself)
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Initial settings for using GrovePi + starter kit and camera on Raspberry Pi