0% found this document useful (0 votes)
15 views

Linux - CPP - start

Uploaded by

Thu TRAN Dan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Linux - CPP - start

Uploaded by

Thu TRAN Dan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Modern C++ for

Computer Vision and


Image Processing

Igor Bogoslavskyi and Cyrill Stachniss


Outline

Course introduction

Linux introduction

C++ syntax
Hello World!

2
What you will learn in course

How to work in Linux


How to write software with modern C++
Core software development techniques
How to work with images using OpenCV
How to implement inverse image search
Check out Google Image Search for
example: https://images.google.com/

3
Why C++? Why Linux? Why?

Over 50 000 developers surveyed


Nearly half of them use Linux
C++ is the most used systems language
(4.5 million users in 2015)
C++11 is a modern language
All companies want C++ in our field
0
Stack Overflow survey: https://insights.stackoverflow.com/survey/2018/
0
CLion survey: https://blog.jetbrains.com/clion/2015/07/infographics-cpp-facts-before-clion/
4
Course structure

The course is split in two parts:


1. Learning the basics
Consists of lectures and homeworks
5 homeworks, 10 points each
25 points moves you to the next part
2. Working on a project
Plan and code inverse image search
Groups of 2 — 3 people
Final project presentation in the end of semester
Exam = project presentation

5
Batteries included!

We will provide you with all the essential tools


for the course:
An Ubuntu virtual machine
Lecture recordings on ECampus, YouTube
Git server to store your code and submit
homework assignments

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

tmp opt usr other home


system
folders

local include bin lib igor

Tree organization starting with root: /


There are no volume letters, e.g. C:, D:
User can only access his/her own folder
8
Understanding files and folders

Folders end with / e.g. /path/folder/


Everything else is files, e.g. /path/file
Absolute paths start with /
while all other paths are relative:
/home/igor/folder/ — absolute path to a folder
/home/igor/file.cpp — absolute path to a file
folder/file — relative path to a file
Paths are case sensitive:
filename is different from FileName
Extension is part of a name:
filename.cpp is different from filename.png
9
Linux terminal

Press Ctrl + Alt + T to open terminal

Linux terminal is a very powerful tool


Most tasks can be done faster from the
terminal than from the GUI
10
Navigating tree from terminal

Terminal is always in some folder


pwd: print working directory
cd <dir>: change directory to <dir>
ls <dir>: list contents of a directory
Special folders:
/ — root folder
~ — home folder
. — current folder
.. — parent folder

11
Structure of Linux commands

Typical structure
${PATH}/command [ options ] [ parameters ]

${PATH}/command: obsolute or relative path


to the program binary
[options]: program-specific options
e.g. -h, or --help
[parameters]: program-specific parameters
e.g. input files, etc.

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

Pressing while typing:


completes name of a file, folder or program
“beeps” if current text does not match any
file or folder uniquely
Pressing + shows all potential matches
Example:
1 igor@igor -work :~> cd te [TAB] [TAB]
2 teaching / temp/ testing /

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]

Can be used with most of terminal


commands: ls, rm, mv etc.

16
Example: placeholders

1 igor@igor - laptop :~/ teaching /demo > ls


2 u01.tex u02.tex u03.tex v01_a.tex v01.pdf v01.tex
v02.pdf v02.tex v03.pdf v03.tex
3 igor@igor - laptop :~/ teaching /demo > ls *. pdf
4 v01.pdf v02.pdf v03.pdf
5 igor@igor - laptop :~/ teaching /demo > ls u*
6 u01.tex u02.tex u03.tex
7 igor@igor - laptop :~/ teaching /demo > ls ?01*
8 u01.tex v01_a .tex v01.pdf v01.tex
9 igor@igor - laptop :~/ teaching /demo > ls [uv ]01*
10 u01.tex v01_a .tex v01.pdf v01.tex
11 igor@igor - laptop :~/ teaching /demo > ls u0 [^12]. tex
12 u03.tex

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

command1; command2; command3


Calls commands one after another
command1 && command2 && command3
Same as above but fails if any of the
commands returns a non-zero code
command1 | command2 | command3
Pipe stdout of command1 to stdin of command2
and stdout of command2 to stdin of command3
Piping commonly used with grep:
ls | grep smth look for smth in output of ls
20
Canceling 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

The shell saves the history of commands in


the ~/.bash_history file
: go to the previous command
: go to the next command
Ctrl + R <query>: search in history
less .bash_history: show history

22
Installing software

Most of the software is available in the system


repository. To install a program in Ubuntu
type this into terminal:
sudo apt update to update information
about available packages
sudo apt install <program> to install the
program that you want
Use apt search <program> to find all
packages that provide <program>
Same for any library, just with lib prefix
23
0
Icon taken from Wikipedia
24
We won’t teach you everything
about C++

Within C++, there is a much


smaller and cleaner language
struggling to get out.
-Bjarne Stroustrup
25
Where to write C++ code

There are two options here:


Use a C++ IDE
CLion
Qt Creator
Eclipse
Use a modern text editor [recommended]
Sublime Text 3 [my preference]
Visual Studio Code
Atom
VIM [steep learning curve]
Emacs [steep learning curve]

0
Most icons are from Paper Icon Set: https://snwh.org/paper
26
Hello World!

Simple C++ program that prints Hello World!


1 # include <iostream >
2
3 int main () {
4 // Is this your first C++ program ?
5 std :: cout << " Hello World !" << std :: endl;
6 return 0;
7 }

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

Every C++ program starts with main


main is a function that returns an error code
Error code 0 means OK
Error code can be any number in [1, 255]
1 int main () {
2 return 0; // Program finished without errors .
3 }

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]

Compile and run Hello World example:


1 c++ -std=c++11 -o hello_world hello_world .cpp
2 ./ hello_world

33
References

Cpp Core Guidelines:


https://github.com/isocpp/CppCoreGuidelines
Google Code Styleguide:
https://google.github.io/styleguide/cppguide.html
Git guide:
http://rogerdudler.github.io/git-guide/
C++ Tutorial:
http://www.cplusplus.com/doc/tutorial/
Book: Code Complete 2 by Steve McConnell

34

You might also like