comp 1402
play

COMP 1402 Winter 2008 Tutorial #3 Overview of Tutorial #3 Review - PDF document

COMP 1402 Winter 2008 Tutorial #3 Overview of Tutorial #3 Review of Number Representation Creating Archives (TAR utility) Compling C Programs Creating Makefiles Emacs text editor 1 Review of Number Representation


  1. COMP 1402 Winter 2008 Tutorial #3 Overview of Tutorial #3 • Review of Number Representation • Creating Archives (TAR utility) • Compling C Programs • Creating Makefiles • Emacs text editor 1

  2. Review of Number Representation • Review answers for last weeks lab. Part I TAR Utility 2

  3. Using TAR – Tape Archive • TAR (tape archive) is an Linux utility for creating archives that can be stored in a single file. • Similar to the WinZip from Windows (but TAR doesn’t compress files on its own, you need another program for that – GZIP ) • Very helpful for submitting assignments that must include multiple files. • TAR is also used to extract files from archives. Extracting a TAR file • You will need to do this for this tutorial. • You extract the contents of an archive file as follows: – tar –xvf archive.tar • This will extract all files from the archive to the current directory. • The switchs (-xvf mean) – x – extract – v – verbose (have tar tell you what it is doing) – f – file, tells tar the the next item is the file to execute the command on. 3

  4. Creating a TAR file • You will need to do this for this tutorial. • To add the files main.c, helper.c, helper.h to an archive : – tar –cvf archive.tar main.c helper.c helper.h • Here we use the ‘c’ switch, which means create. • If the archive will contain multiple source files it is often easier to use wildcards, ie. to create an archive for an assignment try: – tar –cvf assignment.tar *.c *.h Makefile Listing TAR contents • Sometimes you just want to check the contents of a TAR file without extracting them. Use the following commands: – tar –tf archive.tar • If you want to compress a TAR (or any other) file you can use the GZIP utility as follows: – gzip archive.tar • This will compress the archive and create the file archive.tar.gz . 4

  5. Part II Compiling Projects with GCC Compiling C Programs • The compilation process generates an executable from a C source file (ie. “hello.c”). • To compile a source file use GCC: – eg. gcc -o hello hello.c – Creates an executable called hello from the source file hello.c 5

  6. Source and Header Files • For a simple program, like printing “Hello World” you will use a single source file. • For larger programs code is generally divided into 'source' and 'header' files. • Source files have a “.c” extension, header files a “.h” extension. • Header files contain C source code, but typically just define functions that are implemented in the corresponding .c file. Source and Header Files Source file: max.c Header file: max.h #ifndef MAX_H #include "max.h" #define MAX_H int max(int a, int b) { int max(int a, int b); if(a > b) { return a; #endif } else { Don’t worry about the #ifndef, return b; #define, #endif lines for now – } the header simply declares the } function max () The source file provides an implementation (or definition) of the function max (). 6

  7. Creating Executables • You can create an 'executable' file only from a source file that includes the function “main()”. • Thus you cannot compile 'max.c' to create an executable. • You can however create 'object' files (by using the –c switch), which contain machine instructions: – eg. gcc -c min.c – Creates the object file min.o • While object files cannot be run on their own, they are “linked” to executables. Dependencies • Often one source file will have a dependency on another source file. File main.c File max.c #include <stdio.h> #include "max.h" #include "max.h" int main(int argc, char *argv[]) { int max(int a, int b) { int a, b; if( a > b) { return a; printf("Enter a number.\n"); } scanf("%d",&a); else { printf("Enter a number.\n"); return b; scanf("%d",&b); } } printf("%d is bigger.\n", max(a,b) ); } 7

  8. Compiling Executables with Multiple Source/Header files • If our project includes multiple source files we must compile each with GCC. • We compile only the file with the main() function to an executable. • The other source files are compiled as ‘object’ files. Compilation Example • Say we have a file main.c which uses functions provided by helper.c we would invoke GCC as follows (the –c means create object file): – gcc –c helper.c creates object file helper.o – gcc –o main main.c helper.o • To invoke the second command helper.o must exist, thus the dependency. • helper.o is “linked” with main, so that main can use functions implemented in helper.c. 8

  9. Compiling Large Projects • Invoking GCC to compile a few files is not too much work, but what if your project contains 100 source files! You need to invoke GCC for each one. • If you recompile a object file you must recompile every other source file that depends on it. • The make utility simplifies this process by letting you specify compilation rules (do I create an object or executable file), and dependencies. • When you invoke make it determines which (if any) source files have changed and recompiles any files with dependencies on those files. Part III Makefiles 9

  10. Makefiles • The make utility uses the instructions found in a Makefile (which you must create) • By default the makefile is called “Makefile”. • You invoke make as follows: – make • Or, if you want to specify the makefile name: – make –f Makefile Creating Makefiles • A Makefile is a text file where you specify a set or compilation rules as follows: #Comments proceeded by hash symbol target1: dependencies compilation commands target2: dependencies compilation commands • target: name of executable or object file. • dependencies: names of source/header/object files. • Note that commands MUST be preceded by a <TAB> not spaces! 10

  11. A Basic Makefile • To compile a simple hello world program from the source file helloworld.c. File: Makefile #My first Makefile helloworld: helloworld.c gcc –o helloworld helloworld.c • “helloworld” is the ‘target’ file that we wish to create, it depends on “helloworld.c”. • Invoking make will execute: – gcc –o helloworld helloworld.c • For a single source file make doesn’t save us much work. A More Complex Example • Say we have with files main.c , helper1.c , helper1.h , and helper 2.c and helper2.h , where main depends on helper1.c which depends on helper 2.c, our Makefile would look like: #My first Makefile main: main.c helper1.o helper2.o gcc –o main main.c helper1.o helper2.o helper1.o: helper1.c helper1.h gcc –o helper1.o –c helper1.c helper2.o: helper2.c helper2.h gcc –o helper2.o –c helper2.c 11

  12. Targets and Dependencies • Usually the first target is the executable for the program. • In the dependencies list include the names of any source, header, or object files that the executable must link to. • Dependencies (object files) are usually targets elsewhere in the Makefile, eg: Targets and Dependencies #My first Makefile main: main.c helper1.o helper2.o gcc –o main main.c helper1.o helper2.o helper1.o : helper1.c helper1.h gcc –o helper1.o –c helper1.c helper2.o: helper2.c helper2.h gcc –o helper2.o –c helper2.c • In this example if you change “helper1.h” then run make then the command to create helper1.o is run, and since main depends on helper1.o its command is also run, since one of its dependencies have changed. 12

  13. Which Targets Does Make Build? • make looks at the timestamp of each ‘target’ and if any of it’s dependencies have been modified since the target was generated it executes the commands for that target, if dependencies are older make does nothing. #My first Makefile helloworld: helloworld.c gcc –o helloworld helloworld.c eg. if helloworld.c has been modified since helloworld was created then the command: gcc –o helloworld helloworld.c is executed. Part IV Emacs Text Editor 13

  14. Emacs Text Editor • Emacs is a powerful text editor and development tool for Unix/Linux. • You can run Emacs from the command line as follows: – emacs file.c & • You can compile files from within emacs. • It is easier to use than vi (but what isn't) Emacs (cont) • If you go to the “Tools” menu you can select compile to compile your current source file. • By default Emacs assumes your compile command is “make”, but you can change this if you wish. • If you prefer there are numerous other Linux text editors that have nice features like source code highlighting, you can try gedit or kate . • You run gedit or kate just like emacs: – gedit source.c & 14

Recommend


More recommend