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