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

comp 1402
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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
slide-2
SLIDE 2

2

Review of Number Representation

  • Review answers for last weeks lab.

Part I

TAR Utility

slide-3
SLIDE 3

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.

slide-4
SLIDE 4

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.

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

Header file: max.h Source file: max.c #include "max.h" int max(int a, int b) { if(a > b) { return a; } else { return b; } } #ifndef MAX_H #define MAX_H int max(int a, int b); #endif Don’t worry about the #ifndef, #define, #endif lines for now – the header simply declares the function max() The source file provides an implementation (or definition) of the function max().

slide-7
SLIDE 7

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.

#include <stdio.h> #include "max.h" int main(int argc, char *argv[]) { int a, b; printf("Enter a number.\n"); scanf("%d",&a); printf("Enter a number.\n"); scanf("%d",&b); printf("%d is bigger.\n", max(a,b) ); } #include "max.h" int max(int a, int b) { if( a > b) { return a; } else { return b; } }

File max.c File main.c

slide-8
SLIDE 8

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.

slide-9
SLIDE 9

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
  • ther 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

slide-10
SLIDE 10

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!

slide-11
SLIDE 11

11

A Basic Makefile

  • To compile a simple hello world program from the source

file helloworld.c.

#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.

File: Makefile

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

slide-12
SLIDE 12

12

Targets and Dependencies

  • Usually the first target is the executable for

the program.

  • In the dependencies list include the names
  • f 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

  • 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.

#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

slide-13
SLIDE 13

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

  • lder 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

slide-14
SLIDE 14

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 &