Date post: | 23-Jun-2015 |
Category: |
Technology |
Upload: | anas-assayuti |
View: | 341 times |
Download: | 2 times |
DCN 3013: Network Programming
Chapter 1: Introductionby
Muhammed Ramiza RamliKUKTEM
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
Linux Environment
➲ Linux Desktop
Linux Terminal
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
Output
vi Editor
➲ most commonly used editor in Linux is vi➲ can be invoked in terminal as below
Syntax: vi <filename>$ vi hello.c
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
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
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
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
Simple Program
#include <stdio.h>
int main(){ printf(“Hello World!\n”); return 0;}
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>
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
Sample output
Using IDE➲ There are two Integrated Development
Environment commonly used in Linux. Kdevelop Anjuta