Linux - CPP - start
Linux - CPP - start
Course introduction
Linux introduction
C++ syntax
Hello World!
2
What you will learn in course
3
Why C++? Why Linux? Why?
5
Batteries included!
6
What is Linux?
Linux is a free Unix-like OS
Linux kernel implemented by Linus Torvalds
Extremely popular: Android, ChromeOS,
servers, supercomputers, etc.
Many Linux distributions available
Use any distribution if you have preference
Examples will be given in Ubuntu
7
Linux directory tree
/
SYSTEM USER
11
Structure of Linux commands
Typical structure
${PATH}/command [ options ] [ parameters ]
12
Use help with Linux programs
man <command> — manual
exhaustive manual on program usage
command -h
command --help
usually shorter help message
1 igor@igor -lab :~$ pdfpc -h
2 pdfpc v3 .1.1
3 Usage :
4 pdfpc [ OPTION ...] <pdf -file >
5 Help Options :
6 -h, --help Show help options
7 Application Options :
8 -d, --duration =N Duration in minutes
9 <... etc ... >
13
Using command completion
14
Creating and manipulating
files and folders
mkdir [-p] <foldername> — make directory
Create a folder <foldername>
(with all parent folders [-p])
rm [-r] <name> — remove [recursive]
Remove file or folder <name>
(With folder contents [-r])
cp [-r] <source> <dest> — copy
Copy file or folder from <source> to <dest>
mv <source> <dest> — move
Move file or folder from <source> to <dest>
15
Using placeholders
Placeholder Meaning
* Any set of characters
? Any single character
[a-f] Characters in [abcdef]
[ ̂ a-c] Any character not in [abc]
16
Example: placeholders
17
Standard input/output channels
Single input channel: stdin
Two output channels:
stdout: Standard output: channel 1
stderr: Standard error output: channel 2
Redirecting stdout
command 1> out.txt
command >> out.txt
Redirecting stderr
command 2> out.txt
Redirect stdout and stderr into a file
progamm > out.txt 2>&1
Write stdout and stderr into different files
progamm 1>stdout.txt 2>stderr.txt
18
Working with files
more/less/cat <filename>
Print the contents of the file
Most of the time using cat if enough
find <in-folder> -name <filename>
Search for file <filename> in folder
<in-folder>, allows wildcards
grep <what> <where>
Search for a string <what> in a file <where>
19
Chaining commands
CTRL + C
Cancel currently running command
kill -9 <pid>
Kill the process with id pid
killall <pname>
Kill all processes with name pname
htop (top)
Shows an overview of running processes
Allows to kill processes by pressing F9
21
Command history
22
Installing software
0
Most icons are from Paper Icon Set: https://snwh.org/paper
26
Hello World!
27
Comments and any whitespace
chars are completely ignored
A comment is text:
On one line that follows //
Between /* and */
All of these are valid C++:
1 int main () { return 0;} // Unexpected comment .
1 int main ()
2
3 { return 0;
4 }
1 int main () {
2 return /* Unexpected comment */ 0;
3 }
28
Good code style is important
Programs are meant to be read by
humans and only incidentally
for computers to execute.
-Donald Knuth
Use clang_format to format your code
use cpplint to check the style
Following a style guide will save you time
and make the code more readable
We use Google Code Style Sheet
Naming and style recommendations will be
marked by GOOGLE-STYLE tag in slides
0
Google style sheet: https://google.github.io/styleguide/cppguide.html
29
Everything starts with main
1 int main () {
2 return 1; // Program finished with error code 1.
3 }
30
#include directive
Two variants:
#include <file> — system include files
#include "file" — local include files
Copies the content of file into the current file
1 # include " some_file .h"
2 // We can use contents of file " some_file .h" now.
3 int main () {
4 return 0;
5 }
31
I/O streams for simple
input and output
Handle stdin, stdout and stderr:
std::cin — maps to stdin
std::cout — maps to stdout
std::cerr — maps to stderr
#include <iostream> to use I/O streams
Part of C++ standard library
1 # include <iostream >
2 int main () {
3 int some_number ;
4 std :: cin >> some_number ;
5 std :: cout << " number = " << some_number << std :: endl;
6 std :: cerr << " boring error message " << std :: endl;
7 return 0;
8 }
32
Compile and run Hello World!
We understand text
Computer understands machine code
Compilation is translation
from text to machine code
Compilers we can use on Linux:
GCC
Clang [*] [used in examples]
33
References
34