16
DCN 3013: Network Programming Chapter 1: Introduction by Muhammed Ramiza Ramli KUKTEM

Linux programming

Embed Size (px)

Citation preview

Page 1: Linux programming

DCN 3013: Network Programming

Chapter 1: Introductionby

Muhammed Ramiza RamliKUKTEM

Page 2: Linux programming

Overview

➲ Introduction to Linux Programming➲ Writing first C code in Linux➲ Using vi editor➲ Using Gnu Common Compiler (gcc) in

command prompt Source Code Object Code Loadable modules/Binary file

➲ Integrated Development environment Anjuta

Page 3: Linux programming

Linux Environment

➲ Linux Desktop

Page 4: Linux programming

Linux Terminal

Page 5: Linux programming

Linux Command

➲ ls - list a file/dir ls, ls -a, ls -l, ls -s

➲ rm <file> - remove a file rm, rm -Rf

➲ cp <source> <dist> - copy a file➲ mv <source> <dist>- move/rename a file➲ mkdir - create a dir➲ rmdir - remove a dir➲ cd <distdir> - change dir into dist dir➲ and a lots more commands and variations

of them

Page 6: Linux programming

Output

Page 7: Linux programming

vi Editor

➲ most commonly used editor in Linux is vi➲ can be invoked in terminal as below

Syntax: vi <filename>$ vi hello.c

Page 8: Linux programming

vi Editor

➲ vi have two input mode command mode edit mode

➲ Command mode were used for save, search word, replace character , etc can be switch to by pressing Esc button

➲ Edit mode were used to insert or edit data into file.

can be switch to by pressing several key a – append i – insert

Page 9: Linux programming

vi command

➲ in command mode, we can switch to edit mode to start writing by pressing i or a

➲ to go to the certain location, we can move the cursor by h – left, j – up, k – down, and l – right

➲ to save a file we must be in command mode and using command :w

:w - write :q - quit :wq - write and quit :q! - quit without save :w file1 - write as file1

Page 10: Linux programming

vi command

➲ Once in edit mode, we can start writing.➲ to save the file; switch to command mode

by pressing Esc.➲ In command mode we can directly go to

certain line by typing :<linenumber> :5 <enter>

➲ To locate a key word; typing /keyword

cursor will be automatically set on word keyword

Page 11: Linux programming

Exercise

➲ open a new file $ vi hello.c

➲ Once started, vi will be put under command mode

➲ type i or a to enter the edit mode➲ Write a simple C source program that will

display “Hello World” in the screen.➲ Switch to command mode to save it

press Esc :wq

➲ Finish

Page 12: Linux programming

Simple Program

#include <stdio.h>

int main(){ printf(“Hello World!\n”); return 0;}

Page 13: Linux programming

gcc

➲ every program written in Linux can be compiled by using gcc command

➲ gcc syntax gcc <source> -o <output>$ gcc hello.c -o hello

➲ invoking gcc without specifying the output file

gcc <source> will produced an output as a.out

➲ To produces an object code; file.o gcc -c <source>

Page 14: Linux programming

Run a program

➲ Program written in a source form file with .c

➲ It can be compiled into an object code file with .o

➲ Object code or several object code then can be linked in loadable modules or binary file

by default – a.out any name given by the user

➲ To run a program type the program name in the command prompt prefix with ./

$ ./program

Page 15: Linux programming

Sample output

Page 16: Linux programming

Using IDE➲ There are two Integrated Development

Environment commonly used in Linux. Kdevelop Anjuta