KEMBAR78
Programming in Linux Environment | PDF
Programming in Linux Environment
Seoul National University
Interactive and Networked Robotics Lab.
Dongho Kang
Contents
• Introduction
• Programming using Vim editor
• Compiling and Makefile
• Version control with Git
Motivation
• Demo
• Hello World!
• swap()
• Benefits
• Linux is free!
• Support for most programming languages!
• Much better compatibility!
Demo: Hello World!
• Hello World in C, C++, Java, OCaml, Python
• Various programming language and environment.
Demo: swap()
• Assembly code for swap() function.
• GCC compiler
• Low level programming
Linux
• Unix-like Computer Operating System
• Initially released in 1991 by Linus Torvalds
• Free and open-source software
• GPLv2 and other free & open-source licenses
• Platforms
• IA32 (x86-32) / x86-64 / ARM / PowerPC / SPARC / PA-RISC …
Ubuntu 14.04 LTS
Basic commands: Bash
• apt-get: package management
• cd: change directory
• clear: clear screen
• chmod: change the permission
• cp: copy files/folders
• mv: move files/folders
• dir: list the folders
• grep: search files
• mkdir: make directory
• wget: retrieve web pages of file
• rm: remove files
• rmdir: remove folders
• shutdown: shutdown / restart
• sudo: super user do
Basic commands: Bash
• For more commands:
http://ss64.com/bash/
http://tldp.org/LDP/abs/html/basic.html
Programming using Vim editor
• Vim installation
• HelloWorld.c
• Configuration with vimrc file
• Plugins and Vundle
Vim Installation
$ sudo apt-get update # package index update (if needed)
$ sudo apt-get upgrade # package upgrade (if needed)
$ sudo apt-get install vim # vim install
Vim
$ vim # open vim
$ vim a.c # create a.c and open
$ vim -O a.c b.c # vertical (2 files)
$ vimdiff a.c b.c # compare
HelloWorld.c
#include <stdio.h>
int main()
{
// why no indentation? (vimrc!)
printf("Hello World!n");
return 0;
}
GCC Compiling and Run
# build-essential is gcc, g++ etc..
$ sudo apt-get install build-essential
$ sudo apt-get install gcc
$ gcc HelloWorld.c
$ ./a.out
Hello World!
Vim shortcuts
• Be familiar with shortcuts: https://www.fprintf.net/vimCheatSheet.html
• Basic shortcuts and commands:
:q (quit vim)
:q! (quit without saving)
:w (save file)
:w <file name> (save file with name)
:wq (save and quit)
:wq <file name> (save file with name and quit)
:e <file> (edit file)
:a (append text after the cursor)
:A (append text at the end of the line)
:i (insert text before the cursor)
:I (insert text before the first non-blank)
:o (begin a new line below the cursor)
:O (begin a new line above the cursor)
d (delete text)
dd (delete line)
u (undo)
CTRL-R (redo)
Configuration with vimrc file
• Runtime configuration settings to initialize Vim
• File Path: ~/.vimrc
vimrc example
set nocompatible
set autoindent
set smartindent
set cindent
set number
set shiftwidth=4
set tabstop=4
set laststatus=2
set hlsearch
set showcmd
syntax on
For more example: help vimrc_example.vim in vim command mode or http://vimdoc.sourceforge.net/htmldoc/usr_05.html#vimrc_example.vim
and https://amix.dk/vim/vimrc.html
Plugins and Vundle
• Plugins: better way to customize Vim editor
• e.g. The NERD tree / fugitive / vim-airline …
• http://vimawesome.com/
• Management tools for Vim plugins
• Vundle / NeoBundle / VimPlug / Pathogen
Vundle Installation
• More details: https://github.com/VundleVim/Vundle.vim
• Git clone from Vundle repository (GitHub)
• Configure plugins via vimrc
• Install plugins with Vundle
Vundle example
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'The-NERD-Tree'
Plugin 'bling/vim-airline'
Plugin 'zhaocai/GoldenView.Vim'
Plugin 'airblade/vim-gitgutter'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
Compiling and Makefile
• What is “compiling?”
• Compiling with gcc / g++ / javac …
• Make
• Writing makefile
Compiling
Compiling example
• Simple example
• Function a(), b(), c() declared,
defined in different files.
• Use a(), b(), c() in main().
a.h
void a();
b.h
void b();
c.h
void c();
a.c
void a()
{
…
}
b.c
void b()
{
…
}
c.c
void c()
{
…
}
main.c
int main()
{
…
a();
b();
c();
…
}
Function declaration in .h
Function definition in .c
Function usage in main.c
Process of compiling
• Preprocessing
• Compiling
• Assembling
• Linking
Run (exec)
a.h b.h c.h
a.c b.c c.c main.c
a.o b.o c.o main.o
a.s b.s c.s main.s
Preprocessing
Compiling
Assembling
Linking
Compiling example
# compiling / assembling / linking
$ gcc -S *.c # compile .c to generate .s
$ gcc -c *.s # assemble .s to generate .o
$ gcc *.s # link .o to generate exec file
# generate exec at once
$ gcc *.c # compile, assemble and link .c
# generate exec with a name
$ gcc -o run *.c
Make
• A utility that automatically builds executable programs and
libraries from source code by reading files called Makefiles.
- Wikipedia
• Useful to manage big software projects includes many files.
• Compiling rules are written in “makefile” with Rule Syntax.
• Ref. CMake: generate makefile for cross-platform
Makefile
• Rules for compiling
• Target & Component (dependency)
• targets : prerequisites ; command
• e.g. hello: (no prerequisite) ; @echo “hello”
• Make searches the current directory for the makefile to use.
Writing makefile
target-name ... : list-of-dependencies ...
[TAB] command (gcc command: tab is necessary)
...
...
target-name: name of result file (object file or executable file)
list-of-dependency: dependency for target
command: gcc command for compiling
Make example
• Simple example
• Function a(), b(), c() declared,
defined in different files.
• Use a(), b(), c() in main().
a.h
void a();
b.h
void b();
c.h
void c();
a.c
void a()
{
…
}
b.c
void b()
{
…
}
c.c
void c()
{
…
}
main.c
int main()
{
…
a();
b();
c();
…
}
Function declaration in .h
Function definition in .c
Function usage in main.c
Make example
• Dependency graph
Run (exec)
a.h b.h c.h
a.c b.c c.c main.c
a.o b.o c.o main.o
Makefile example
run: main.o a.o b.o c.o
gcc -o run main.o a.o b.o c.o
main.o: main.c a.h b.h c.h
gcc -c main.c
a.o: a.c a.h
gcc -c a.c
b.o: b.c b.h
gcc -c b.c
c.o: c.c c.h
gcc -c c.c $ make: compile with makefile rules
Makefile macro & label
• Using macro to avoid command repeating
• $ make –p: check what kind of macro you can use.
• Internal macro: $* / $*.c / $*.o / $@ / $< …
• Label: name of macro
• OBJECTS: .o files
• CC: compiler
• CFLAGS: compile flag
Makefile macro & label
• Suffix rule
• Implicit rules for Make
• “how each target is ``made'' from the prerequisites or relied
on make magic to do the right thing.”
For more details: https://wiki.kldp.org/KoreanDoc/html/GNU-Make/GNU-Make.html
Programming with GNU Software (O’REILLY) Chap.7
# Form
s1s2:
commands to get s2 from s1
# Example
.c.o :
$(CC) $(CFLAGS) –c $<
Makefile example refined!
# labels
TARGET = test
OBJS = main.o a.o b.o c.o
CC = gcc
# suffix rules
.SUFFIXES: .c .o
# compile rules
c.o:
$(CC) -c -o $@ $*.c
$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS)
# dependencies
main.o: main.c a.h b.h c.h
a.o: a.c a.h
b.o: b.c b.h
c.o: c.c c.h
clean:
rm -f $(OBJS) $(TARGET)
Version Control with Git
• What is “Git”?
• Git basics
• Git on the server
• GitHub
Why we use Git?
When you need to edit your source code…
Why we use Git?
Hmm…
What is “Git”
• Widely-used version control system for software development.
• Full version tracking / Full-fledged repository with complete history
• Developed by Linus Torvalds (Initially released in 2005)
• Free software
• GNU General Public License v2 / GNU Lesser General Public License 2.1
• https://git-scm.com/
Git basics
• git init / git clone: create (or clone) a new repository
• git add: propose changes
• git commit: commit!
Git basics
• Branch
• git checkout –b <new branch>
• git checkout master
• git branch –d <branch to delete>
• git merge <branch to merge>
Git on the server
• git clone user@host:/remote/repo/path
• git commit
• git push origin <branch>
GitHub
• Web-based Git repository hosting service.
• Founded in 2008.
• Cost
• Free for public repos
• Paid for private repos
• GitHub Student Pack: https://education.github.com/pack
• Free for micro account ($7/month) with five private repos. (only for students)
• Other free developer packs including DigitalOcean, AWS etc. (only for students)
https://github.com/gokoreas/InRoL-Quadrotor-Platform-for-Research/blob/master/USB2PPM_FW/usb2ppm_fw/usb2ppm_fw.ino
Git tutorial
• Create new repository on GitHub.
• Hello Git! (coding)
• Git commit and push to remote.
• Modify code.
• Git commit and push to remote.
Git Clients
• Not for Linux
• GitHub Desktop
• Source Tree
• Linux
• GitKraket (Beta)
• gitg
Whole tutorial
• https://github.com/EastskyKang/Programming-in-Linux-
Environment-Tutorial
• Korean / English
Maybe next time…
• More details…
• Other useful tools for SW development in Linux environment.
• GDB – for debugging
• Open-source libraries / SDKs
• OpenCV / AscTec Research Platform / ROS …

Programming in Linux Environment

  • 1.
    Programming in LinuxEnvironment Seoul National University Interactive and Networked Robotics Lab. Dongho Kang
  • 2.
    Contents • Introduction • Programmingusing Vim editor • Compiling and Makefile • Version control with Git
  • 3.
    Motivation • Demo • HelloWorld! • swap() • Benefits • Linux is free! • Support for most programming languages! • Much better compatibility!
  • 4.
    Demo: Hello World! •Hello World in C, C++, Java, OCaml, Python • Various programming language and environment.
  • 5.
    Demo: swap() • Assemblycode for swap() function. • GCC compiler • Low level programming
  • 6.
    Linux • Unix-like ComputerOperating System • Initially released in 1991 by Linus Torvalds • Free and open-source software • GPLv2 and other free & open-source licenses • Platforms • IA32 (x86-32) / x86-64 / ARM / PowerPC / SPARC / PA-RISC …
  • 8.
  • 10.
    Basic commands: Bash •apt-get: package management • cd: change directory • clear: clear screen • chmod: change the permission • cp: copy files/folders • mv: move files/folders • dir: list the folders • grep: search files • mkdir: make directory • wget: retrieve web pages of file • rm: remove files • rmdir: remove folders • shutdown: shutdown / restart • sudo: super user do
  • 11.
    Basic commands: Bash •For more commands: http://ss64.com/bash/ http://tldp.org/LDP/abs/html/basic.html
  • 12.
    Programming using Vimeditor • Vim installation • HelloWorld.c • Configuration with vimrc file • Plugins and Vundle
  • 13.
    Vim Installation $ sudoapt-get update # package index update (if needed) $ sudo apt-get upgrade # package upgrade (if needed) $ sudo apt-get install vim # vim install
  • 14.
    Vim $ vim #open vim $ vim a.c # create a.c and open $ vim -O a.c b.c # vertical (2 files) $ vimdiff a.c b.c # compare
  • 15.
    HelloWorld.c #include <stdio.h> int main() { //why no indentation? (vimrc!) printf("Hello World!n"); return 0; }
  • 16.
    GCC Compiling andRun # build-essential is gcc, g++ etc.. $ sudo apt-get install build-essential $ sudo apt-get install gcc $ gcc HelloWorld.c $ ./a.out Hello World!
  • 17.
    Vim shortcuts • Befamiliar with shortcuts: https://www.fprintf.net/vimCheatSheet.html • Basic shortcuts and commands: :q (quit vim) :q! (quit without saving) :w (save file) :w <file name> (save file with name) :wq (save and quit) :wq <file name> (save file with name and quit) :e <file> (edit file) :a (append text after the cursor) :A (append text at the end of the line) :i (insert text before the cursor) :I (insert text before the first non-blank) :o (begin a new line below the cursor) :O (begin a new line above the cursor) d (delete text) dd (delete line) u (undo) CTRL-R (redo)
  • 18.
    Configuration with vimrcfile • Runtime configuration settings to initialize Vim • File Path: ~/.vimrc
  • 19.
    vimrc example set nocompatible setautoindent set smartindent set cindent set number set shiftwidth=4 set tabstop=4 set laststatus=2 set hlsearch set showcmd syntax on For more example: help vimrc_example.vim in vim command mode or http://vimdoc.sourceforge.net/htmldoc/usr_05.html#vimrc_example.vim and https://amix.dk/vim/vimrc.html
  • 20.
    Plugins and Vundle •Plugins: better way to customize Vim editor • e.g. The NERD tree / fugitive / vim-airline … • http://vimawesome.com/ • Management tools for Vim plugins • Vundle / NeoBundle / VimPlug / Pathogen
  • 21.
    Vundle Installation • Moredetails: https://github.com/VundleVim/Vundle.vim • Git clone from Vundle repository (GitHub) • Configure plugins via vimrc • Install plugins with Vundle
  • 22.
    Vundle example set nocompatible" be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo Plugin 'The-NERD-Tree' Plugin 'bling/vim-airline' Plugin 'zhaocai/GoldenView.Vim' Plugin 'airblade/vim-gitgutter' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " To ignore plugin indent changes, instead use: "filetype plugin on " " Brief help " :PluginList - lists configured plugins " :PluginInstall - installs plugins; append `!` to update or just " :PluginSearch foo - searches for foo; append `!` to refresh local cache " :PluginClean - confirms removal of unused plugins; append `!` to auto- " " see :h vundle for more details or wiki for FAQ " Put your non-Plugin stuff after this line
  • 24.
    Compiling and Makefile •What is “compiling?” • Compiling with gcc / g++ / javac … • Make • Writing makefile
  • 25.
  • 26.
    Compiling example • Simpleexample • Function a(), b(), c() declared, defined in different files. • Use a(), b(), c() in main(). a.h void a(); b.h void b(); c.h void c(); a.c void a() { … } b.c void b() { … } c.c void c() { … } main.c int main() { … a(); b(); c(); … } Function declaration in .h Function definition in .c Function usage in main.c
  • 28.
    Process of compiling •Preprocessing • Compiling • Assembling • Linking Run (exec) a.h b.h c.h a.c b.c c.c main.c a.o b.o c.o main.o a.s b.s c.s main.s Preprocessing Compiling Assembling Linking
  • 29.
    Compiling example # compiling/ assembling / linking $ gcc -S *.c # compile .c to generate .s $ gcc -c *.s # assemble .s to generate .o $ gcc *.s # link .o to generate exec file # generate exec at once $ gcc *.c # compile, assemble and link .c # generate exec with a name $ gcc -o run *.c
  • 30.
    Make • A utilitythat automatically builds executable programs and libraries from source code by reading files called Makefiles. - Wikipedia • Useful to manage big software projects includes many files. • Compiling rules are written in “makefile” with Rule Syntax. • Ref. CMake: generate makefile for cross-platform
  • 31.
    Makefile • Rules forcompiling • Target & Component (dependency) • targets : prerequisites ; command • e.g. hello: (no prerequisite) ; @echo “hello” • Make searches the current directory for the makefile to use.
  • 32.
    Writing makefile target-name ...: list-of-dependencies ... [TAB] command (gcc command: tab is necessary) ... ... target-name: name of result file (object file or executable file) list-of-dependency: dependency for target command: gcc command for compiling
  • 33.
    Make example • Simpleexample • Function a(), b(), c() declared, defined in different files. • Use a(), b(), c() in main(). a.h void a(); b.h void b(); c.h void c(); a.c void a() { … } b.c void b() { … } c.c void c() { … } main.c int main() { … a(); b(); c(); … } Function declaration in .h Function definition in .c Function usage in main.c
  • 34.
    Make example • Dependencygraph Run (exec) a.h b.h c.h a.c b.c c.c main.c a.o b.o c.o main.o
  • 35.
    Makefile example run: main.oa.o b.o c.o gcc -o run main.o a.o b.o c.o main.o: main.c a.h b.h c.h gcc -c main.c a.o: a.c a.h gcc -c a.c b.o: b.c b.h gcc -c b.c c.o: c.c c.h gcc -c c.c $ make: compile with makefile rules
  • 36.
    Makefile macro &label • Using macro to avoid command repeating • $ make –p: check what kind of macro you can use. • Internal macro: $* / $*.c / $*.o / $@ / $< … • Label: name of macro • OBJECTS: .o files • CC: compiler • CFLAGS: compile flag
  • 37.
    Makefile macro &label • Suffix rule • Implicit rules for Make • “how each target is ``made'' from the prerequisites or relied on make magic to do the right thing.” For more details: https://wiki.kldp.org/KoreanDoc/html/GNU-Make/GNU-Make.html Programming with GNU Software (O’REILLY) Chap.7 # Form s1s2: commands to get s2 from s1 # Example .c.o : $(CC) $(CFLAGS) –c $<
  • 38.
    Makefile example refined! #labels TARGET = test OBJS = main.o a.o b.o c.o CC = gcc # suffix rules .SUFFIXES: .c .o # compile rules c.o: $(CC) -c -o $@ $*.c $(TARGET): $(OBJS) $(CC) -o $@ $(OBJS) # dependencies main.o: main.c a.h b.h c.h a.o: a.c a.h b.o: b.c b.h c.o: c.c c.h clean: rm -f $(OBJS) $(TARGET)
  • 39.
    Version Control withGit • What is “Git”? • Git basics • Git on the server • GitHub
  • 40.
    Why we useGit? When you need to edit your source code…
  • 41.
    Why we useGit? Hmm…
  • 42.
    What is “Git” •Widely-used version control system for software development. • Full version tracking / Full-fledged repository with complete history • Developed by Linus Torvalds (Initially released in 2005) • Free software • GNU General Public License v2 / GNU Lesser General Public License 2.1 • https://git-scm.com/
  • 43.
    Git basics • gitinit / git clone: create (or clone) a new repository • git add: propose changes • git commit: commit!
  • 44.
    Git basics • Branch •git checkout –b <new branch> • git checkout master • git branch –d <branch to delete> • git merge <branch to merge>
  • 45.
    Git on theserver • git clone user@host:/remote/repo/path • git commit • git push origin <branch>
  • 46.
    GitHub • Web-based Gitrepository hosting service. • Founded in 2008. • Cost • Free for public repos • Paid for private repos • GitHub Student Pack: https://education.github.com/pack • Free for micro account ($7/month) with five private repos. (only for students) • Other free developer packs including DigitalOcean, AWS etc. (only for students)
  • 48.
  • 49.
    Git tutorial • Createnew repository on GitHub. • Hello Git! (coding) • Git commit and push to remote. • Modify code. • Git commit and push to remote.
  • 50.
    Git Clients • Notfor Linux • GitHub Desktop • Source Tree • Linux • GitKraket (Beta) • gitg
  • 51.
  • 52.
    Maybe next time… •More details… • Other useful tools for SW development in Linux environment. • GDB – for debugging • Open-source libraries / SDKs • OpenCV / AscTec Research Platform / ROS …