Linux programming

499 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
499
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linux programming

  1. 1. DCN 3013: Network Programming Chapter 1: Introduction by Muhammed Ramiza Ramli KUKTEM
  2. 2. Overview <ul><li>Introduction to Linux Programming </li></ul><ul><li>Writing first C code in Linux </li></ul><ul><li>Using vi editor </li></ul><ul><li>Using Gnu Common Compiler ( gcc) in command prompt </li></ul><ul><ul><li>Source Code </li></ul></ul><ul><ul><li>Object Code </li></ul></ul><ul><ul><li>Loadable modules/Binary file </li></ul></ul><ul><li>Integrated Development environment Anjuta </li></ul>
  3. 3. Linux Environment <ul><li>Linux Desktop </li></ul>
  4. 4. Linux Terminal
  5. 5. Linux Command <ul><li>ls - list a file/dir </li></ul><ul><ul><li>ls, ls -a, ls -l, ls -s </li></ul></ul><ul><li>rm <file> - remove a file </li></ul><ul><ul><li>rm, rm -Rf </li></ul></ul><ul><li>cp <source> <dist> - copy a file </li></ul><ul><li>mv <source> <dist>- move/rename a file </li></ul><ul><li>mkdir - create a dir </li></ul><ul><li>rmdir - remove a dir </li></ul><ul><li>cd <distdir> - change dir into dist dir </li></ul><ul><li>and a lots more commands and variations of them </li></ul>
  6. 6. Output
  7. 7. vi Editor <ul><li>most commonly used editor in Linux is vi </li></ul><ul><li>can be invoked in terminal as below </li></ul><ul><ul><li>Syntax: vi <filename> </li></ul></ul><ul><ul><ul><li>$ vi hello.c </li></ul></ul></ul>
  8. 8. vi Editor <ul><li>vi have two input mode </li></ul><ul><ul><li>command mode </li></ul></ul><ul><ul><li>edit mode </li></ul></ul><ul><li>Command mode were used for </li></ul><ul><ul><li>save, search word, replace character , etc </li></ul></ul><ul><ul><li>can be switch to by pressing Esc button </li></ul></ul><ul><li>Edit mode were used to insert or edit data into file. </li></ul><ul><ul><li>can be switch to by pressing several key </li></ul></ul><ul><ul><ul><li>a – append </li></ul></ul></ul><ul><ul><ul><li>i – insert </li></ul></ul></ul>
  9. 9. vi command <ul><li>in command mode, we can switch to edit mode to start writing by pressing i or a </li></ul><ul><li>to go to the certain location, we can move the cursor by </li></ul><ul><ul><ul><li>h – left, j – up, k – down, and l – right </li></ul></ul></ul><ul><li>to save a file we must be in command mode and using command :w </li></ul><ul><ul><li>:w - write </li></ul></ul><ul><ul><li>:q - quit </li></ul></ul><ul><ul><li>:wq - write and quit </li></ul></ul><ul><ul><li>:q! - quit without save </li></ul></ul><ul><ul><li>:w file1 - write as file1 </li></ul></ul>
  10. 10. vi command <ul><li>Once in edit mode, we can start writing. </li></ul><ul><li>to save the file; switch to command mode by pressing Esc. </li></ul><ul><li>In command mode we can directly go to certain line by typing </li></ul><ul><ul><li>:<linenumber> </li></ul></ul><ul><ul><li>:5 <enter> </li></ul></ul><ul><li>To locate a key word; typing </li></ul><ul><ul><li>/keyword </li></ul></ul><ul><ul><ul><li>cursor will be automatically set on word keyword </li></ul></ul></ul>
  11. 11. Exercise <ul><li>open a new file </li></ul><ul><ul><li>$ vi hello.c </li></ul></ul><ul><li>Once started, vi will be put under command mode </li></ul><ul><li>type i or a to enter the edit mode </li></ul><ul><li>Write a simple C source program that will display “Hello World” in the screen. </li></ul><ul><li>Switch to command mode to save it </li></ul><ul><ul><li>press Esc </li></ul></ul><ul><ul><li>:wq </li></ul></ul><ul><li>Finish </li></ul>
  12. 12. Simple Program <ul><li>#include <stdio.h> </li></ul><ul><li>int main()‏ </li></ul><ul><li>{ </li></ul><ul><li>printf(“Hello World!n”); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  13. 13. gcc <ul><li>every program written in Linux can be compiled by using gcc command </li></ul><ul><li>gcc syntax </li></ul><ul><ul><li>gcc <source> -o <output> </li></ul></ul><ul><ul><li>$ gcc hello.c -o hello </li></ul></ul><ul><li>invoking gcc without specifying the output file </li></ul><ul><ul><li>gcc <source> </li></ul></ul><ul><ul><li>will produced an output as a.out </li></ul></ul><ul><li>To produces an object code; file.o </li></ul><ul><ul><li>gcc -c <source> </li></ul></ul>
  14. 14. Run a program <ul><li>Program written in a source form </li></ul><ul><ul><li>file with .c </li></ul></ul><ul><li>It can be compiled into an object code </li></ul><ul><ul><li>file with .o </li></ul></ul><ul><li>Object code or several object code then can be linked in loadable modules or binary file </li></ul><ul><ul><li>by default – a.out </li></ul></ul><ul><ul><li>any name given by the user </li></ul></ul><ul><li>To run a program type the program name in the command prompt prefix with ./ </li></ul><ul><ul><li>$ ./program </li></ul></ul>
  15. 15. Sample output
  16. 16. Using IDE <ul><li>There are two Integrated Development Environment commonly used in Linux. </li></ul><ul><ul><li>Kdevelop Anjuta </li></ul></ul>

×