Basic Linux commands and Compilation
ITSC 3181 Introduction to Computer Architecture
[Link]
Department of Computer Science
Yonghong Yan
yyan7@[Link]
[Link]
1
Contents
• Basic Linux commands
– We will use Ubuntu Linux VM on the lab machine
– You can use the VM on your own computer and laptop as well.
• Compiling and linking
2
Linux Basic Commands
It is all about dealing with files and folders
Linux folder: /home/yanyh/… • rm (remove a filer/folder)
– $ rm foo
• ls (list files in the current folder) – $ rm -rf foo
– $ ls -l – $ rm -i foo
– $ ls -a
– $ ls -la
– $ rm -- -foo
– $ ls -l --sort=time • cat (print the file contents to
– $ ls -l --sort=size –r terminal)
• cd (change directory to) – $ cat /etc/motd
– $ cd /usr/bin – $ cat /proc/cpuinfo
• pwd (show current folder name) • cp (create a copy of a file/folder)
– $ pwd – $ cp foo bar
• ~ (home folder) – $ cp -a foo bar
– $ cd ~
• ~user (home folder of a user) • mv (move a file/folder to
– $ cd ~weesan another location. Used also for
renaming)
• What will “cd ~/weesan” do? – $ mv foo bar
• mkdir (create a folder)
3
– $ mkdir foo
Basic Commands (cont)
• df (Disk usage) Search a command or a file
– $ df -h /
• which
– $ du -sxh ~/ – $ which ls
• man (manual) • whereis
– $ man ls – $ whereis ls
– $ man 2 mkdir • locate
– $ man man – $ locate stdio.h
– $ man -k mkdir – $ locate iostream
• Manpage sections • find
– 1 User-level cmds and apps – $ find / | grep stdio.h
• /bin/mkdir – $ find /usr/include | grep stdio.h
– 2 System calls
• int mkdir(const char *, …); Smarty
– 3 Library calls 1. [Tab] key: auto-complete the command
• int printf(const char *, …);
sequence
2. é key: to find previous command
3. [Ctl]+r key: to search previous command
4
Editing a File: Vim
• 2 modes • Delete
– dd (delete a line)
– Input mode
– d10d (delete 10 lines)
• ESC to back to cmd mode
– d$ (delete till end of line)
– Command mode – dG (delete till end of file)
• Cursor movement – x (current char.)
– h (left), j (down), k (up), l (right)
• Paste
– ^f (page down)
– p (paste after)
– ^b (page up)
– ^ (first char.) – P (paste before)
– $ (last char.) • Undo
– G (bottom page) – u
– :1 (goto first line) • Search
• Swtch to input mode – /
– a (append) • Save/Quit
– i (insert)
– :w (write)
– o (insert line after
– O (insert line before)
– :q (quit)
– :wq (write and quit)
– :q! (give up changes)
5
C Hello World
• vi hello.c #include <stdio.h>
• Switch to editing mode: i or a /* The simplest C Program *
• Switching to control mode: ESC int main(int argc, char **arg
• Save a file: in control mode, :w printf("Hello World\n");
• To quit, in control mode, :q return 0;
}
• To quit without saving, :q!
• Copy/paste a line: yy and then p, both from the current
cursor
– 5 line: 5yy and then p
• To delete a whole line, in control mode, : dd
6
Other Editors to Use
• Sublime, Emacs, etc
7
C Syntax and Hello World
#include inserts another file. “.h” files are called “header”
files. They contain declarations/definitions needed to
interface to libraries and code in other “.c” files.
What do the < >
mean?
A comment, ignored by the compiler
#include <stdio.h>
The main() function is always
/* The simplest C Program */ where your program starts
int main(int argc, char **argv) running.
{
Blocks of code (“lexical
printf(“Hello World\n”); scopes”) are marked by { … }
return 0;
}
Return ‘0’ from this function
8
8
Compilation Process in C
• Compilation process: gcc hello.c -o hello
– Constructing an executable image for an application
– FOUR stages
– Command:
gcc <options> <source_file.c>
• Compiler Tool
– gcc (GNU Compiler)
• man gcc (on Linux m/c)
– icc (Intel C compiler)
9
4 Stages of Compilation Process
Preprocessing
gcc -E hello.c -o hello.i
hello.c à hello.i
Compilation (after preprocessing)
gcc -S hello.i -o hello.s
Assembling (after compilation)
gcc -c hello.s -o hello.o
Linking object files
gcc hello.o -o hello
Output à Executable ([Link])
Run à ./hello (Loader)
10
4 Stages of Compilation Process
1. Preprocessing (Those with # …)
– Expansion of Header files (#include … )
– Substitute macros and inline functions (#define …)
2. Compilation
– Generates assembly language
– Verification of functions usage using prototypes
– Header files: Prototypes declaration
3. Assembling
– Generates re-locatable object file (contains m/c instructions)
– nm app.o
0000000000000000 T main
U puts
– nm or objdump tool used to view object files
11
4 Stages of Compilation Process (contd..)
4. Linking
– Generates executable file (nm tool used to view exe file)
– Binds appropriate libraries
• Static Linking
• Dynamic Linking (default)
• Loading and Execution (of an executable file)
– Evaluate size of code and data segment
– Allocates address space in the user mode and transfers them
into memory
– Load dependent libraries needed by program and links them
– Invokes Process Manager à Program registration
12
Compiling a C Program
• gcc <options> program_name.c
• Options: Four stages into one
-----------
-Wall: Shows all warnings
-o output_file_name: By default [Link] executable file is
created when we compile our program with gcc. Instead,
we can specify the output file name using "-o" option.
-g: Include debugging information in the binary.
• man gcc
13