UNIX/Linux environment
C programming tools
Stefano Quer, Pietro Laface, and Stefano Scanzio
Dipartimento di Automatica e Informatica
Politecnico di Torino
skenz.it/os stefano.scanzio@polito.it
Operating Systems 2
Programming languages
There are more than 200 programming
languages
Many are practically not used
Others have been used mainly in the past
There are several classification techniques /
taxonomies
More used, more loved, more paid, etc.
Operating Systems 3
Programming languages
IEEE Spectrum
Top Programming Languages
2022
Operating Systems 4
Programming languages
PYPL Index e Stack Overflow
Settembre 2022
Operating Systems 5
IDE
Integrated Development Environment (IDE)
Single platform to develop projects in different
languages
They generally provide
Text editor, syntax highlighter, customizable
interfaces, compiler, code auto-save, version
control, debugger, build automation, and
deployment.
https://www.geeksforgeeks.org/
7-best-ides-for-c-c-plus-plus-
developers-in-2022/
Settembre 2022
Operating Systems 6
IDE
Tool Comments
Eclipse Written in JAVA and developed by IBM. It supports C, C++,
C#, Java, Javascript, COBO; Perl, Python, etc.
Visual Studio Written in C++ and developed by Microsoft. It supports C,
C++, C#, CSS, Go, HTML, Java, JavaScript, Python, PHP,
TypeScript and other.
NetBeans Free open source, developed by Apache Software
Foundation. Recommended for students or beginners and
C/C ++
CLion Developed by Jetbrains for programmers in C++, best cross-
platform platform (macOS, Linux, Windows integrated with
Cmake); it supports Kotlin, Python, Swift, etc.
Code::Blocks Open-source C/C++ IDE developed using wxWidgets, a
toolkit GUI; it supports C, C++ e Fortran
CodeLite Free and open-source for C++; one of the best IDE for code
refactoring; supported by Windows and Mac
QtCreator It requires commercial license for its complete version; it is supported
by Windows, Linux, and macOS
Operating Systems 7
Editor
An editor permits to write textual files
(possibly containing programs)
https://www.linuxtechi.com/top-10-text-editors-for-linux-
desktop/
Settembre 2022
Operating Systems 8
Editor
Tool Comments
VIM VI Improved. Default editor UNIX/Linux; present
everywhere. Difficult but widely configurable.
Geany Editor for Linux desktop integrable with the development
tool GTK+
Sublime Text editor and development environment. It supports
different languages (automatic markup)
Brackets Produced by ADOBE in 2014 for Linux. Developed with
HTML; CSS and JavaScript. Light.
Gedit Default editor for the GNOME desktop. Simple user
interface. Light.
VS Code Visual Studio Code, Microsoft, for Windows, UNIX/Linux,
Mac. Very powerfull and usefull editor.
Nano Similar to the editor Pico, released in 2000, but with several
additional features. It allows only a «line interface»
Emacs One of the older Linux editors; developed by Richard
Stallmann (https://youtu.be/jUibaPTXSHk), funder of GNU.
Completelly developed in LISP and C.
Operating Systems 9
Editor: Vim (Vi)
Text editor
Present in all BSD and Unix systems (and also in
embed systems)
Developed since 1976
Last version (8.1) in 2018
Base version (Vi)
Is not functional for extensive file editing
Very useful if other editors cannot be used, or give
some problem
e.g., remote editing
Operating Systems 10
Editor: Vim (Vi)
Expanded and improved over time
Vim = VI Improved
In the newer versions can be used for editing large
projects
Multi-level undo, multi-window, multi-buffer, etc
On-line help, syntax highlighting, etc.
Together with emacs, it is one of the
protagonists of the "war of the editors"
Extensions allow to increase editor features
Operating Systems 11
Editor: Vim (Vi)
Run with the command
vi filename
It provides different operating modes
Command Mode
Cursor positioned in the text
The keyboard is used to issue commands
Input Mode
Text insertion mode
The keyboard is used to insert the text
Directive Mode
Cursor positioned on the last line of the video
The keyboard is used for control directives
Operating Systems 12
Editor: Vim (Vi)
Command Mode Command
Cursor movements ←,↑,→,↓ (h, j, k, l)
Also
Insert Mode (from the cursor) 0-g i
Insert Mode (at the beginning of the line) n-g I
Append Mode (from the cursor) a
Append Mode (at the end of the line) A
Overwrite Mode R
Pass (return) to Command Mode esc (key)
Delete a row dd Also
n-dd
Delete a single character x n-x
Documentation
Local help : man vim
Online resources: http://www.vim.org/docs.php
Resources in PDF: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
Operating Systems 13
Editor: Vim (Vi)
Command Mode (continue) Command
Insert last deletion P
Delete a character X
Cancel the last operation (undo) u
Restore the last change (redo) Ctrl-r
Directive Mode Command
Pass to Directive Mode (last line) :
Show line numbers :set nu
Save the file :w!, :w fileName
Exit without saving the latest changes :q!
Enter the on-line help :help
Learn Vim (from Google): Vim Adventures https://vim-adventures.com/
Operating Systems 14
Editor: emacs
Free text editor
Emacs = Editor MACroS
Developed since 1976
Last version (26.2) in 2019
Initially developed by Richard Stallman
https://en.wikipedia.org/wiki/Richard_Stallman
Preferred by many advanced programmers:
powerful, extensible, flexible
Various versions, but the most popular are
GNU Emacs
Xemacs = next generation Emacs
Operating Systems 15
Editor: emacs
Available for
GNU, GNU/Linux
FreeBSDm, NetBSD, OpenBSD
Mac OS X
MS Windows
Solaris
Operating Systems 16
Editor: emacs
Advantages
Many features, more powerful than the simple text
editor
Fully customizable
Fast execution of complex operations
Disadvantages
Slow learning curve
Written in Lisp
Operating Systems 17
Editor: emacs
Base commands available through
Menu
Character sequences
Control commands: control + character (c-key)
Meta commands: alt + character (m-key)
Documentation
Local help : man emacs
Online resources : http://www.gnu.org/software/emacs/manual/emacs.html
Resources in PDF: http://www.gnu.org/software/emacs/maanual/pdf/emacs.pdf
Operating Systems 18
Compiler and Debugger
Compiler
GCC
G++
Makefile
Configure
Debugger
GDB
Operating Systems 19
Compiler: gcc
Open-Source GNU project
gcc compiler and linker
Supports C and C++
Documentation
Local help : man gcc
Online resources : http://www.gnu.org
Command syntax
gcc <options> <arguments>
Options: list of flags that control the compiler and
the linker; there are options for compilation only, for
linker only, or both
Arguments: list of files that gcc reads and process
depending on the given options
Operating Systems 20
Examples
Compilation of a set of files that produces the
corresponding object files
gcc –c file1.c
gcc –c file2.c
gcc –c main.c
Link of the object files produces the executable
file
gcc –o myexe file1.o file2.o main.o
Compilation and linking with a single command
gcc –o myexe file1.c file2.c main.c
Operating Systems 21
gcc options
Most common options
-c file
Compilation only
-o file
Specifies the executable name; generally indicates
the name of the final executable (after the link
operation)
-g
gcc does not produce optimized code, but inserts
additional information useful for debugging (see
gdb)
-Wall
Output a warning for all possible code errors
Operating Systems 22
gcc options
Do not insert spaces
-Idir
Specify further directories where searching header
files
More than one directory can be specified (-Idir1 –
Idir2 …)
-lm
Specifies to use the math library
-Ldir
Specifies the search directories for pre-existing
libraries to be linked
Operating Systems 23
Example 1
gcc –Wall –g –I. –I/myDir/subDir –o myexe \
myMain.c \
fileLib1.c fileLib2.c file1.c \
file2.c file3.c -lm
Compilation of many source files, followed by
linking and creation of the executable file
Multi-row command
Provides “All Warnings”
Debug option (i.e., do not optimize code9
Find the header files in two directories
Links the math library
Operating Systems 24
Makefile
Support tools for the development of complex
projects
First scripting
Developed since 1998 language used in
Made up of utilities this course
Makefile
Make
Provides a convenient tool to automate the
compilation and linker steps
Help
Extremely flexible
man make instrument, but its main
strength is the verification
of dependencies
Operating Systems 25
Makefile
Makefile has two main aims
Automatically perform repetitive tasks
Avoid (re)doing unnecessary tasks
by verifying the file dependencies and
modification times (e.g., re-compile only the
files that have been modified since the previous
make command)
Two phases
Write a makefile file
A text file similar to a script (shell script or other)
The Makefile file is interpreted with the make
utility
This way you can compile and link
Operating Systems 26
Make options
Make can be executed using different options
Does not execute, just displays the commands
-n
Ignores possible errors and proceeds with the next
commands
-i, --ignore-errors
Output debug information during the execution
-d
--debug=[options]
Options: a = print all info, b = basic info, v =
verbose = basic + other, i = implicit = verbose +
other
Operating Systems 27
Makefile options
The command make can take as argument a
source file (Makefile), with name different than
standard ones
The make command executes by default
the file makefile if it exists
Or the file Makefile if the file makefile does not exist
-f <fileName> (or --file <fileName>)
Allows you to execute the Makefile with name
<fileName>
make --file <fileName>
make --file=<fileName>
make -f <fileName>
Operating Systems 28
Makefile format
Tabulation character
target: dependency
<tab>command
A Makefile includes
Empty lines
They are ignored
Lines starting with "#"
They are comments, and consequently ignored
Lines that specify rules
Each rule specifies a target, some dependencies,
and actions; it can occupy one or more lines
Very long lines can be splitted by inserting the "\"
character at the end of the line
Operating Systems 29
Makefile format
target: dependency
<tab>command
When a Makefile is executed (with the command
make)
The default behavior is to execute the first rule
i.e., the first target in the file
If more targets are specified, the desired target
can be passed as an argument to make
make <targetName>
make –f <myMakefile> <targetName>
Operating Systems 30
Makefile format
A makefile consists of "rules" like this:
target: dependency
<tab>command
Each rule includes
Target Name
Usually the name of a file
Sometimes the name of an action (which is named
"phony" target)
dependency list that must be verified to execute
the target
Command, or list of commands
Each command is preceded by a mandatory TAB
character, invisible but necessary
Operating Systems 31
Example 1: Single target
target:
<tab>gcc –Wall –o myExe main.c -lm
Specifies Notice: TAB
A single target with name target
The target does not have dependencies
Executing the Makefile
The target is executed
Since the target does not have dependencies, the
execution of the target corresponds to the
execution of the compilation command
Operating Systems 32
Example 2: Multiple targets
project1:
<tab>gcc –Wall –o project1 myFile1.c
project2:
<tab>gcc –Wall –o project2 myFile2.c
The Makefile specifies more rules
Need to choose which is the target to execute
The default consists in the execution of the first
target
Executing the command
make
The target project1 is executed
make project2
The target project2 is executed
Operating Systems 33
Example 3: Multiple targets and actions
target:
Command on
<tab>gcc –Wall –o my \
more rows
<tab> main.c \
<tab> bst.c list.c queue.c stack.c
<tab>cp my /home/myuser/bin
clean:
<tab>rm –rf *.o *.txt
Specify more rules
Rules have no dependencies
The first target executes two commands (gcc and
cp)
This first target is executed with the commands
● make
● make target
Operating Systems 34
Example 3: Multiple targets and actions
target:
Command on
<tab>gcc –Wall –o my \
more rows
<tab> main.c \
<tab> bst.c list.c queue.c stack.c
<tab>cp my /home/myuser/bin
clean:
<tab>rm –rf *.o *.txt
The second target removes all the files with
extension .o and all the files with extension .txt
This second target is executed with the command
● make clean
Operating Systems 35
Example 4: dependencies
target: file1.o file2.o
<tab>gcc –Wall –o myExe file1.o file2.o
file1.o: file1.c myLib1.h
<tab>gcc –Wall –g –I./dirI –c file1.c
file2.o: file2.c myLib1.h myLIb2.h
<tab>gcc –Wall –g –I./dirI –c file2.c
Execution of multiple targets in the presence of
dependencies
It checks if target dependencies are more recent
than the current target
In this case, dependencies are performed before
the execution of the current target
This process iterates recursively
Operating Systems 36
Example 4: dependencies
target: file1.o file2.o
<tab>gcc –Wall –o myExe file1.o file2.o
file1.o: file1.c myLib1.h
<tab>gcc –Wall –g –I./dirI –c file1.c
file2.o: file2.c myLib1.h myLIb2.h
<tab>gcc –Wall –g –I./dirI –c file2.c
Target has file1.o and file2.o as dependencies
rule file1.o is checked
If file1.c (or myLib1.h) is more recent than file1.o,
this rule (i.e., the gcc command) is executed
Otherwise this rule is not executed
The same is done for the file2.o rule
At the end the target is executed if necessary
Operating Systems 37
Action name
("phony" target) Example 4: dependencies
target: file1.o file2.o
<tab>gcc –Wall –o myExe file1.o file2.o
... File name
file2.o: file2.c myLib1.h myLIb2.h
<tab>gcc –Wall –g –I./dirI –c file2.c
If the target is not a file name, it is a "phony"
target that should always be executed
To be sure that is always executed
.PHONY : target
Regardless the existence of a file with the same
name and more recent than dependencies
Operating Systems 38
Implicit rules and modularity
There exist very powerful rules for improving
modularity and make more efficient the writing of
makefiles
Use of macros
Use of implicit rules
The dependence between .o and .c is automatic
The dependence between .c and .h is automatic
Recursive dependencies are analyzed automatically
etc.
Operating Systems 39
Example 5: Macro
CC=gcc Definition of macro:
FLAGCS=-Wall -g macro=name
SRC=main.c bst.c list.c util.c (with or without spaces)
project: $(SRC)
<tab>$(CC) $(FLAGS)–o project $(SRC) –lm
Use of the macro:
$(macro)
Macro allows to define
Symbols
Compilers, compilation flags, etc.
Lists
Object files, executables, directories, etc.
Operating Systems 40
Example 6: Multi-Folder
CC=gcc The macro $^ copies the
FLAGCS=-Wall -g list of files reported in the
SDIR=source list of dependencies
HDIR=header The macro $@
ODIR=obj copies the current
"target name"
project: $(ODIR)/main.o $(ODIR)/bst.o
<tab>$(CC) $(FLAGS)–o $@ $^
$(ODIR)/main.o: $(SDIR)/main.c $(HDIR)/main.h
<tab>$(CC) $(FLAGS) -c $^
$(ODIR)/bst.o: $(SDIR)/bst.c $(HDIR)/bst.h
<tab>$(CC) $(FLAGS) -c $^
The macro $< would copy the
first file reported in the list of
dependencies
Operating Systems 41
Debugger: gdb
Software package used to analyze the behavior
of another program in order to identify and
eliminate errors (bugs)
GNU debugger gdb is available for almost all
Operating Systems
It can be used
As a "stand-alone" tool
Particularly inconvenient use
Integrated with many editors (e.g., emacs)
Embedded in some graphical IDE
Abbreviate form of commands can be given
Operating Systems 42
Debugger: gdb
Action Command
Execution commands run (r)
next (n)
next <NumberOfSteps>
step (s)
step <NumberOfSteps>
stepi (si)
finish (f)
continue (c)
Breakpoint commands info break
break (b), ctrl-x-blank
break LineNumber
break FunctionName
break fileName:LineNumber
disable BreakpointNumber
enable BreakpointNumber
Operating Systems 43
Debugger: gdb
Action Command
Print commands print (p)
print expression
display expression
Stack operations down (d)
up (u)
Info args
Info locals
Code listing commands list (l)
list LineNumber
list FirstLine, LastLine
Miscellaneous commands file fileName
exec filename
kill
http://darkdust.net/files/GDB%20Cheat%20Sheet.pdf