Linux v2
Linux v2
Linux v2
LINUX ON EMBEDDED
SYSTEM
HCMUTE – Faculty of Mechanical Engineering
Lecturer: PhD. Bui Ha Duc
1
2
3
Linux vs Windows
• First look - Graphic user interface:
Windows Linux
Ubuntu
Linux Linux
Fedora Debian
4
Linux vs Windows
• First look - Terminal:
Linux Terminal
5
Linux vs Windows
Linux Windows
Open Source; $$
“Free” to distribute, download, modify
Can be installed on a wide variety of On PC's desktops, laptops, servers
computer hardware (mobile phones, and some phones
tablet computers, video game
consoles, supercomputers)
File system support: Ext2, Ext3, Ext4 File system support: FAT, FAT32,
NTFS
Text command: BASH Text command: DOS – like commands
User interface: GNOME or KDE Windows Aero
6
Linux Kernel
• Kernel is the core of Linux operating systems
• Official website:
https://www.kernel.org/
• OS based on Linux kernel – Linux Distributions
• E.g. Ubuntu, Fedora, Red hat, Debian…
Linux Kernel
• Check your linux kernel:
• Open terminal
• Type “uname –r”
2.6.32-431.11.2.el6.x86_64
Kernel Version Major Revision Minor Revision
Ubuntu desktop
11
Ubuntu desktop
Generic Architecture of an Embedded
Linux System
Read Request
Boot Loader
Warm up
• Open Ubuntu -> Files -> Home
Linux Terminal
• The command line is an interesting beast
• Most of time you communicate with the embedded board
via Terminal
• Terminal use BASH language
• Linux ignores the extension and looks inside the file to
determine what type of file it is.
• Linux is Case Sensitive
e.g. file1.TXT ≠ file1.txt
• Careful with space in file names
• The Linux command line does not have an undo feature.
Perform destructive actions carefuly.
18
Linux Terminal
Useful commands: Absolute and Relative Paths
• pwd – Print Working Directory Absolute paths specify a
user@bash: pwd location (file or directory) in
relation to the root directory,
/home/haduc always begin with a forward
• ls – list all folders and files in the slash ( / )
working directory e.g. /home/ryan
user@bash: ls Relative paths specify a
location (file or directory) in
bin Documents public_html relation to where we currently
user@bash: ls -l /etc are in the system, not begin
• cd [location] – move to the [location with a slash.
folder] e.g. Documents
e.g. cd /usr/local/bin
cd ../games
19
Linux Terminal
• man <command> - Look up the manual page for a
particular command.
• mkdir [options] <Directory> - Create a new folder
e.g. mkdir chuyende
• rmdir [options] <Directory> - Remove a folder
• touch [options] <filename> - Create a blank file
• cp [options] <source> <destination> - copy a file or
folder
• mv [options] <source> <destination> - move a file or
folder
• rm [options] <file> - remove a file
• gedit <file> - open Text Editor to edit file
20
Linux Terminal
Permission: specify what a particular person may or may
not do with respect to a file or directory
Linux Terminal
• chown [option] [path] – Change the ownership of a file
or directory
E.g. to change the owner of /foo and subfiles to “root”, run:
chown -R root /foo
E.g. get permission to modify /var/www/html :
sudo chown -R pi:pi /var/www/html
• passwd – change user password
• su – switch from user to root account
• exit – return to user account
• history – display all previous command
• clear – clear the terminal screen
22
Linux Terminal
• df – shows the size, used space, and available space on
the mounted filesystems
e.g. df -h -> show list of file system
• echo – print a string or string variable on terminal screen
e.g. echo Hello
echo $PATH
echo $HOME
• grep – searches for lines which contain a search pattern
e.g grep train *.txt
→ searching for the word “train” in all
text files in the current directory
23
Linux Terminal
• Unzip Files in Linux
• Installing unzip
sudo apt-get install unzip
• Unzip a file
unzip filename.zip
• Save as “hello.c”.
• On Terminal, move to “bai_tap” folder and type
gcc -o hello hello.c
• To run the program, type
./hello
25
When we type a command on the command line, the system runs through a
preset series of directories, looking for the program we specified.
To override this behavior, we can provide path for the program with “./”
26
Makefile
• Makefile is a program building tool which runs on Unix,
Linux
• Makefile simplify the procedure of building a program that
may need various modules.
• Makefile determine how the modules need to be compiled
or recompiled together
• For example, let’s assume we have the following source
files:
• main.cpp
• hello.cpp
• factorial.cpp
• functions.h
28
Makefile
• We can compile using command:
gcc main.cpp hello.cpp factorial.cpp -o hello
But need to careful about the sequence of the function
calls.
Makefile
CC = gcc
CFLAGS = -g
target hello: main.cpp hello.cpp factorial.cpp
action $(CC) $(CFLAGS) $? -o $@
clean:
rm *.o hello
• Structure of target:
target: dependencies
• Action lines should start after a tab
• CC, CFLAGS: Macro, need to predefine
• E.g. CC = gcc Program for compiling C programs
CFLAGS = -g Extra flags to give to the C compiler
LDFLAGS = -lGL Extra flags to give to compiler when they
are supposed to invoke the linker
Refer to this link for more detail on macro
https://www.tutorialspoint.com/makefile/makefile_macros.htm
30
Makefile
CC = gcc
CFLAGS = -g
target hello: main.cpp hello.cpp factorial.cpp
action $(CC) $(CFLAGS) $? -o $@
clean:
rm *.o hello
Example
• What will happen if this makefile is called ?
CC=gcc
OUTPUT=Hello
all:Hello.o display.o
$(CC) -o $(OUTPUT) Hello.o display.o
Hello.o:Hello.c
$(CC) -c Hello.c
display.o:display.c
$(CC) -c display.c
32
CMake
• CMake is a meta build system.
(refer to https://cgold.readthedocs.io/en/latest/overview/cmake-can.html for more info)
• Installing Cmake
sudo apt-get -y install cmake
• Compile a project with Cmake
• Step 1: create an empty folder and put your code files into it
• Step 2: create a CMakeLists.txt in the same folder
33
CMake
• Step 3: Open CMakeLists.txt, insert following lines
cmake_minimum_required(VERSION 2.8)
CMake
• Step 4: Build the project
cmake .
make
• Step 5: Recompile if the source code is changed
make