As an example of cross-compilation practice, let's create a program on Ubuntu that runs on Linux with a MIPS CPU. With hands-on, Linux commands that can be used normally are often not embedded devices, which can be a problem. This time, it is an example assuming such a case.
please note :
Please note that we do not guarantee the operation of this procedure. Because you need the embedded hardware to run and the environment suitable for each.
We will use the environment introduced in this article, so please prepare the environment by looking at the article. Building a Gcc cross-compilation environment with Docker on Ubuntu of VirtualBox running on Mac
$ sudo docker start gcc-ubuntu
$ sudo docker exec -it gcc-ubuntu /bin/bash
This time, take the sleepenh command as an example. It was easy to find, so I'll bring it from Debian. I don't know if this is okay!
Reference: How to find the package that contains the command (example in Ubuntu) Execute the command on the machine that has it.
[email protected]:~# which bash #Check the location of the command
/bin/bash
[email protected]:~# dpkg --search /bin/bash #Can be examined with dpkg
bash: /bin/bash
You can see that the "bash" package contains the / bin / bash command.
Get the package in source code
[email protected]:~/dev# mkdir sleepenh #Create working directory
<Please perform the file acquisition and transfer method in the docker environment according to your own environment. Omitted>
sleepenh-master.Got a zip
[email protected]:~/dev/sleepenh# unzip sleepenh-master.zip
[email protected]:~/dev/sleepenh# cd sleepenh-master
If you compile with Makefaile, it will not be cross-compiled, so modify the Makefile.
[email protected]:~/dev/sleepenh/sleepenh-master# vi Makefile
The modified part is the location of "CC =" and change gcc to mips-linux-gnu-gcc. You can now compile sleepenh for MIPS on your PC. (Cross compilation)
Q ?= @
ifneq ($(Q),@)
Q :=
endif
CC = mips-linux-gnu-gcc
CFLAGS = -std=gnu11 -Wall -Wextra -O2 $(EXTRA_CFLAGS)
<< Omitted below >>
make
[email protected]:~/dev/sleepenh/sleepenh-master# make
/bin/sh: 1: git: not found
mips-linux-gnu-gcc -std=gnu11 -Wall -Wextra -O2 -DVCSVERSION='' sleepenh.c -o sleepenh
gzip -9 < sleepenh.1 > sleepenh.1.gz
[email protected]:~/dev/sleepenh/sleepenh-master# ls
Makefile Makefile.org changelog debian sleepenh sleepenh.1 sleepenh.1.gz sleepenh.c
sleepenh has been compiled.
In order to write a program easily, it is important to utilize the provided library. It is also often used in commands. At this time, if there is a problem with the library on a PC or server, it is convenient to update only the library. Therefore, it is common for programs to compile to dynamically reference a library (dynamic link).
However, in an embedded environment, it is convenient to operate in various environments such as no library or versions. Therefore, in order to fix the library to be referenced, it may be compiled in the form of incorporating the library in the execution binary (static link) so that it is not affected by the external environment.
If you compile gcc with the -static option, it will be a "static link", otherwise it will be a "dynamic link".
Let's check the sleepenh command that was actually created.
[email protected]:~/dev/sleepenh/sleepenh-master# file sleepenh
sleepenh: ELF 32-bit MSB executable, MIPS, MIPS-I version 1 (SYSV), dynamically linked, interpreter /lib/ld., for GNU/Linux 3.2.0, with debug_info, not stripped
You can see that a dynamically linked executable file has been created for MIPS (MIPS, MIPS-I version 1 (SYSV)). Since it is a dynamic link, it depends on the operating environment. In some cases, there may be no library to refer to.
So let's compile with a static link.
Make a copy of each directory and prepare it for later comparison.
[email protected]:~/dev/sleepenh# cd ~/dev/sleepenh/
[email protected]:~/dev/sleepenh# cp -r sleepenh-static sleepenh-static
[email protected]:~/dev/sleepenh# cd sleepenh-static
[email protected]:~/dev/sleepenh/sleepenh-static# make clean #clean up
Modify the Makefile to compile with static links.
[email protected]:~/dev/sleepenh/sleepenh-static# vi Makefile
For the correction part, add "-static" to the place of "CFLAGS =". You can now compile sleepenh for MIPS with static links on your PC. (Cross compilation)
Change before
CFLAGS = -std=gnu11 -Wall -Wextra -O2 $(EXTRA_CFLAGS)
After change
CFLAGS = -std=gnu11 -Wall -static -Wextra -O2 $(EXTRA_CFLAGS)
After change
Q ?= @
ifneq ($(Q),@)
Q :=
endif
CC = mips-linux-gnu-gcc
CFLAGS = -std=gnu11 -Wall -static -Wextra -O2 $(EXTRA_CFLAGS)
<< Omitted below >>
make
[email protected]:~/dev/sleepenh/sleepenh-static# make
/bin/sh: 1: git: not found
mips-linux-gnu-gcc -std=gnu11 -Wall -static -Wextra -O2 -DVCSVERSION='' sleepenh.c -o sleepenh
gzip -9 < sleepenh.1 > sleepenh.1.gz
[email protected]:~/dev/sleepenh/sleepenh-master# ls
Makefile Makefile.org changelog debian sleepenh sleepenh.1 sleepenh.1.gz sleepenh.c
sleepenh has been compiled.
Let's check the sleepenh command that was actually created.
sleepenh: ELF 32-bit MSB executable, MIPS, MIPS-I version 1 (SYSV), statically linked, for GNU/Linux 3.2.0, with debug_info, not stripped
You can see that a statically linked executable file has been created for MIPS (MIPS, MIPS-I version 1 (SYSV)). If it is a static link, it will operate regardless of the operating environment. In some cases, there may be no library to refer to, but this is not a problem.
Take the person compiled with this static link to the MIPS Linux device and test the operation.
Recommended Posts