1
Unit 10 Linux Operating System 2 Linux Based on the Unix - - PowerPoint PPT Presentation
Unit 10 Linux Operating System 2 Linux Based on the Unix - - PowerPoint PPT Presentation
1 Unit 10 Linux Operating System 2 Linux Based on the Unix operating system Developed as an open-source ("free") alternative by Linux Torvalds and several others starting in 1991 Originally only for Intel based processors
2
Linux
- Based on the Unix operating system
- Developed as an open-source ("free") alternative by
Linux Torvalds and several others starting in 1991
- Originally only for Intel based processors but has
now been ported to other platforms (i.e. ARM processors in your phone, etc.)
- Commonly used in industry and in embedded
devices
3
Using the Command Line
- While it has a GUI interface like
your Mac or Windows PC much
- f its power lies in its rich set of
utilities that are most easily run at the command line (aka command prompt or terminal)
- Here we can navigate the file system
(like you would with Explorer or Finder), start programs (double- clicking an icon), and much more by simply typing commands
Terminal Icon Linux Terminal View Vocareum Terminal View
4
Navigating the File System
- A file system has
– Folders (directories) – Files
- They are organized in a hierarchy
- Everything we can do with a GUI we can do at the command line
5
Some Basic Commands
- Here are some helpful commands to use in
Linux at the command prompt
Command ls List (see) all files in the current folder pwd Present working directory shows the current folder location of the terminal cd dirname Change directory to a new folder cp srcfiles dest Copy file(s) to a new location mv srcfile dest Move/rename files to a new name/location rm srcfiles Remove files from the current folder mkdir dirname Make directory / create a new folder rmdir dirname Remove directory / delete a folder (must be empty first)
6
Directory Structure Ex. 1
- Each circle is a directory
- Each name in the box is a file
- Starting from your home (e.g.
'mark') directory/folder…
- Use cd to change directories
(folders)
– cd Desktop – cd cs102 – cd hw7
- Or go multiple folders at a
time
– cd Desktop/cs102/hw7 mark
Desktop
hw7
Documents
- ther
src
hw7a.cpp hw7b.cpp test2.h test2.cpp
cs102
you start here
home /
7
Directory Structure Ex. 2
- To go up a level use
– cd ..
- To go up 2 levels use
– cd ../..
- Let's go one level to 'cs102'
– cd ..
- Now make a directory
– mkdir hw8
mark
Desktop
hw7
Documents
- ther
src
hw7a.cpp hw7b.cpp test2.h test2.cpp
home cs102
you start here
hw8
Shortcuts: . = Current directory .. = Parent directory (up one) ~ = Home directory * = Wildcard to match filenames Unix commands: pwd = Print current working dir
/
8
Directory Structure Ex. 3
- Let's say we want to start a
new lab with a copy of our
- ld work and just modify it.
Let's copy our work – Recall I'm in cs102 folder currently
– cp hw7/* hw8/ mark
Desktop
hw7
Documents
- ther
src
hw7a.cpp hw7b.cpp test2.h test2.cpp
cs102
you start here
hw8 home /
hw7a.cpp hw7b.cpp Shortcuts: . = Current directory .. = Parent directory (up one) ~ = Home directory * = Wildcard to match filenames Unix commands: pwd = Print current working dir
9
Directory Structure Ex. 4
- Let's now go into the test
folder
– cd test
- Now rename the hw7a.cpp to
hw8.cpp
– mv hw7a.cpp hw8.cpp
- Now delete the hw7b.cpp file
– rm hw7b.cpp
- Remember, you can see all the
files in a folder by typing
– ls
- You can see what
folder/directory you are in by typing
– pwd
mark
Desktop
hw7
Documents
- ther
src
hw7a.cpp hw7b.cpp test2.h test2.cpp
cs102
you start here
hw8 home /
hw7a.cpp hw7b.cpp
10
EDITORS AND COMPILERS
11
Editors
- "Real" developers use editors
designed for writing code
– No word processors!!
- You need a text editor to
write your code
– Eclipse, Sublime, MS Visual Code, Emacs, Atom, and many others
- These often have handy functions for
commenting, indenting, checking matching braces ({..}) etc.
12
Starting An Editor
- From the command line you can navigate to the folder where
you want to create new files or where your files already exist
– cd cs102
- You need to know the name of the editor application (subl
for Sublime, code for MS Visual Code, emacs for Emacs) and you can include a filename after the application name and it will open that file (if it exists) or create it (if it doesn't)
– subl hw8.cpp & – code hw8.cpp & – emacs hw8.cpp &
– The '&' prevents the terminal from freezing until the editor is closed and thus allows you to continue typing in new commands into the terminal while the editor remains open
13
Compilers
- Several free and commercial compilers are
available
– g++: – clang++ – XCode – MS Visual Studio
- Several have "integrated" editors, debuggers
and other tools and thus are called IDE's (Integrated Development Environments)
14
Software Process
Executable Binary Image ("test")
1110 0010 0101 1001 0110 1011 0000 1100 0100 1101 0111 1111 1010 1100 0010 1011 0001 0110 0011 1000
C++ file(s) (test.cpp) Compiler
#include <iostream> using namespace std; int main() { int x = 5; cout << "Hello" << endl; cout << "x=" << x; return 0; }
g++ Load & Execute
$ subl test.cpp & $ subl test.cpp & $ g++ –g –Wall –o test test.cpp
- r
$ make test $ subl test.cpp & $ g++ –g –Wall –o test test.cpp $ ./test
2
Compile & fix compiler errors
1
Edit & write code
3
Load & run the executable program
Std C++ & Other Libraries
Note: Most documentation and books use $ as a placeholder for the command line prompt.
15
g++ Options
- Most basic usage
– g++ cpp_filenames – Creates an executable a.out
- Options
– -o => Specifies output executable name (other than default a.out) – -g => Include info needed by debuggers like gdb, kdbg, etc. – -Wall => show all warnings
- Most common usage form:
– $ g++ -g -Wall hw8.cpp -o hw8
16
Running the Program
- First ensure the program compiles
– $ g++ -g -Wall hw8.cpp -o hw8
- Then run the program by preceding the