programming in linux environment

52
Programming in Linux Environment Seoul National University Interactive and Networked Robotics Lab. Dongho Kang

Upload: dong-ho-kang

Post on 16-Apr-2017

260 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Programming in Linux Environment

Programming in Linux Environment

Seoul National University

Interactive and Networked Robotics Lab.

Dongho Kang

Page 2: Programming in Linux Environment

Contents

• Introduction

• Programming using Vim editor

• Compiling and Makefile

• Version control with Git

Page 3: Programming in Linux Environment

Motivation

• Demo

• Hello World!

• swap()

• Benefits

• Linux is free!

• Support for most programming languages!

• Much better compatibility!

Page 4: Programming in Linux Environment

Demo: Hello World!

• Hello World in C, C++, Java, OCaml, Python

• Various programming language and environment.

Page 5: Programming in Linux Environment

Demo: swap()

• Assembly code for swap() function.

• GCC compiler

• Low level programming

Page 6: Programming in Linux Environment

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 …

Page 7: Programming in Linux Environment
Page 8: Programming in Linux Environment

Ubuntu 14.04 LTS

Page 9: Programming in Linux Environment
Page 10: Programming in Linux Environment

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

Page 11: Programming in Linux Environment

Basic commands: Bash

• For more commands:

http://ss64.com/bash/

http://tldp.org/LDP/abs/html/basic.html

Page 12: Programming in Linux Environment

Programming using Vim editor

• Vim installation

• HelloWorld.c

• Configuration with vimrc file

• Plugins and Vundle

Page 13: Programming in Linux Environment

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

Page 14: Programming in Linux Environment

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

Page 15: Programming in Linux Environment

HelloWorld.c

#include <stdio.h>

int main()

{

// why no indentation? (vimrc!)

printf("Hello World!\n");

return 0;

}

Page 16: Programming in Linux Environment

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!

Page 17: Programming in Linux Environment

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)

Page 18: Programming in Linux Environment

Configuration with vimrc file

• Runtime configuration settings to initialize Vim

• File Path: ~/.vimrc

Page 19: Programming in Linux Environment

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.vimand https://amix.dk/vim/vimrc.html

Page 20: Programming in Linux Environment

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

Page 21: Programming in Linux Environment

Vundle Installation

• More details: https://github.com/VundleVim/Vundle.vim

• Git clone from Vundle repository (GitHub)

• Configure plugins via vimrc

• Install plugins with Vundle

Page 22: Programming in Linux Environment

Vundle exampleset 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

Page 23: Programming in Linux Environment
Page 24: Programming in Linux Environment

Compiling and Makefile

• What is “compiling?”

• Compiling with gcc / g++ / javac …

• Make

• Writing makefile

Page 25: Programming in Linux Environment

Compiling

Page 26: Programming in Linux Environment

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

Page 27: Programming in Linux Environment
Page 28: Programming in Linux Environment

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

Page 29: Programming in Linux Environment

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

Page 30: Programming in Linux Environment

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

Page 31: Programming in Linux Environment

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.

Page 32: Programming in Linux Environment

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

Page 33: Programming in Linux Environment

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

Page 34: Programming in Linux Environment

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

Page 35: Programming in Linux Environment

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

Page 36: Programming in Linux Environment

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

Page 37: Programming in Linux Environment

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.htmlProgramming with GNU Software (O’REILLY) Chap.7

# Form

s1s2:

commands to get s2 from s1

# Example

.c.o :

$(CC) $(CFLAGS) –c $<

Page 38: Programming in Linux Environment

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)

Page 39: Programming in Linux Environment

Version Control with Git

• What is “Git”?

• Git basics

• Git on the server

• GitHub

Page 40: Programming in Linux Environment

Why we use Git?

When you need to edit your source code…

Page 41: Programming in Linux Environment

Why we use Git?

Hmm…

Page 42: Programming in Linux Environment

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/

Page 43: Programming in Linux Environment

Git basics

• git init / git clone: create (or clone) a new repository

• git add: propose changes

• git commit: commit!

Page 44: Programming in Linux Environment

Git basics

• Branch

• git checkout –b <new branch>

• git checkout master

• git branch –d <branch to delete>

• git merge <branch to merge>

Page 45: Programming in Linux Environment

Git on the server

• git clone user@host:/remote/repo/path

• git commit

• git push origin <branch>

Page 46: Programming in Linux Environment

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)

Page 47: Programming in Linux Environment
Page 48: Programming in Linux Environment

https://github.com/gokoreas/InRoL-Quadrotor-Platform-for-Research/blob/master/USB2PPM_FW/usb2ppm_fw/usb2ppm_fw.ino

Page 49: Programming in Linux Environment

Git tutorial

• Create new repository on GitHub.

• Hello Git! (coding)

• Git commit and push to remote.

• Modify code.

• Git commit and push to remote.

Page 50: Programming in Linux Environment

Git Clients

• Not for Linux

• GitHub Desktop

• Source Tree

• Linux

• GitKraket (Beta)

• gitg

Page 51: Programming in Linux Environment

Whole tutorial

• https://github.com/EastskyKang/Programming-in-Linux-

Environment-Tutorial

• Korean / English

Page 52: Programming in Linux Environment

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 …