1 / 13
Download Presentation

SEEM3460 Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SEEM3460 Tutorial Multi-module programming in C

  2. SEEM3460 Tutorial Multi-module programming in C Beforewe start: Please log on to the server Please recall how to edit text files on Linux/Unix

  3. Why Multi-module • Some function can be reused in many place. • Writing all the code in one file will make the code too long and not easy to maintain.

  4. What we will do today • We have two program “ask_reverse” and “ask_palindrome” . But both of them have all the code in one single file. We will partition them into several modules. • Task 1: Partition “ask_reverse” into two parts. One is the main program while another is the “reverse” function. • Task 2: Partition “ask_palindrome” into three parts, i.e. the main program, the “reverse” function and “palindrome” function. We can reuse the “reverse” function in Task 1.

  5. Copy the material • Create the directory • mkdirc_multi • cd c_multi • cd ask • Copy files • cp ~seem3460/distribute/c_multi-module/ask/ask_reverse.cask_reverse.c • cp ~seem3460/distribute/c_multi-module/ask/ask_palindrome.cask_palindrome.c

  6. Framework 1

  7. Target Structure

  8. Modularizing Reverse reverse.c reverse.h ask_reverse.c #include <string.h> #include “reverse.h” void reverse(...) { ... } void reverse(...); #include <stdio.h> #include <string.h> #include “reverse.h” int main() { ... } void reverse(...) { ... }

  9. Modularizing Reverse • Create “reverse.h” and “reverse.c” • How to paste under unix: • Shift+insert, right click (not in x-win32) • With the help of copy and paste • >cpask_reverse.creverse.c • >cpreverse.creverse.h • Edit “reverse.c” and “reverse.h” • Edit “ask_reverse.c”

  10. Compile • Create .o files: • gcc -c reverse.c • gcc -c ask_reverse.c • Link .o files as executables: • gcc -o ask_reverseask_reverse.oreverse.o

  11. Modularizing palindrome palindrome.c palindrome.h ask_palindrome.c #include <string.h> #include “palindrome.h” int palindrome(...) { ... } int palindrome(...); #include <stdio.h> #include <string.h> #include “palindrome.h” #include “reverse.h” int palindrome(...) { ... } void reverse(...) { ... } int main(...) { ... }

  12. Compile • Create .o files: • gcc -c palindrome.c • gcc -c ask_palindrome.c • Link .o files as executables: • gcc -o ask_palindromeask_palindrome.opalindrome.oreverse.o

  13. Framework 2 (for your interest) • cp ~seem3460/distribute/c_multi-module/label/label_rect.clabel_rect.c

More Related