Try compiling Linux commands (cross-compilation practice)

Try compiling Linux commands (cross-compilation practice)

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 :

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

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

Start Dccker

$ sudo docker start gcc-ubuntu
$ sudo docker exec -it gcc-ubuntu /bin/bash

Linux command source code

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!

Debian · GitLab sleepenh

Reference: How to find the package that contains the command (example in Ubuntu) Execute the command on the machine that has it.

root@af48c1a975a2:~# which bash #Check the location of the command
/bin/bash
root@af48c1a975a2:~# 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 source code

Get the package in source code

root@af48c1a975a2:~/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
root@af48c1a975a2:~/dev/sleepenh# unzip sleepenh-master.zip
root@af48c1a975a2:~/dev/sleepenh# cd sleepenh-master

Compile

If you compile with Makefaile, it will not be cross-compiled, so modify the Makefile.

root@af48c1a975a2:~/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

root@af48c1a975a2:~/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

root@af48c1a975a2:~/dev/sleepenh/sleepenh-master# ls
Makefile  Makefile.org  changelog  debian  sleepenh  sleepenh.1  sleepenh.1.gz  sleepenh.c

sleepenh has been compiled.

Dynamic link and static link

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

Check the information of the created file

Let's check the sleepenh command that was actually created.

root@af48c1a975a2:~/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.

Compile with static links

So let's compile with a static link.

Make a copy of each directory and prepare it for later comparison.

root@af48c1a975a2:~/dev/sleepenh# cd ~/dev/sleepenh/
root@af48c1a975a2:~/dev/sleepenh# cp -r sleepenh-static sleepenh-static
root@af48c1a975a2:~/dev/sleepenh# cd sleepenh-static
root@af48c1a975a2:~/dev/sleepenh/sleepenh-static# make clean #clean up

Modify the Makefile to compile with static links.

root@af48c1a975a2:~/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

root@af48c1a975a2:~/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

root@af48c1a975a2:~/dev/sleepenh/sleepenh-master# ls
Makefile  Makefile.org  changelog  debian  sleepenh  sleepenh.1  sleepenh.1.gz  sleepenh.c

sleepenh has been compiled.

Check the information of the created file (whether it became a static link)

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

Try compiling Linux commands (cross-compilation practice)
Linux commands
Linux commands
linux commands
Linux commands
Linux commands
[Linux] List of Linux commands used in practice
Network Linux commands
Verbalize Linux commands
Linux user commands
New Linux commands! !!
Basic LINUX commands
Various Linux commands
Typical Linux commands (7)
Try compiling "Linux driver for Dual Sense of PS5"
Frequently used Linux commands
Frequently used Linux commands
Frequently used linux commands
[Linux] Group related commands
Linux commands to remember
New Linux commands! !! Part 2
Try NeosVR on Linux
Studying Linux commands and frustration
Try normal Linux programming Part 7
Try normal Linux programming Part 2
Try normal Linux programming Part 3
Try normal Linux programming Part 4