Docsity

Introduction to Computer Programming Tools - Study Guide | COMP 4262, Study notes of Computer Science

Partial preview of the text

Download Introduction to Computer Programming Tools - Study Guide | COMP 4262 and more Study notes Computer Science in PDF only on Docsity!

  • COMP 4/6262: Programming UNIX Lecture 21 C Programming Tools April 17,

Outline {

Unix programming philosophy

{

C programming tools

z

compilation

{

single-module programs {

multi-module programs

z

debugging

z

maintaining large multi-module programs

{

the unix file dependency system: make {

the unix archive system: at {

the unix source code control system: sccs

{

Chapter 12, Glass and Ables

{

Source code available: ftp://ftp.prenhall.com/pub/esm/the_apt_series.s-042/glass_ables_unix-3e/

Preferred UI style

Graphical user interfaces (GUI)

vs

Command line interfaces (CLI)

{

What are the consequences of GUIs or

CLIs? {

We know that a poor GUI can make using a

system hard for a novice (or even an expert) {

What happens when the CLI is weak?

Command line interfaces

A weak or nonexistent CLI can lead to problems: {

Outputs can’t be used as inputs {

Remote system access will be difficult {

Non-interactive programs (servers, daemons, and background processes) will require a GUI How do Windows/Mac OS handle these tasks? (Hint: They’re actually moving towards CLIs!)

Unix philosophy 9.

Rule of Representation: Fold knowledge into data soprogram logic can be stupid and robust.

Rule of Least Surprise: In interface design, always do theleast surprising thing.

Rule of Silence: When a program has nothing surprising tosay, it should say nothing.

Rule of Repair: When you must fail, fail noisily and as soonas possible.

Rule of Economy: Programmer time is expensive; conserveit in preference to machine time.

Rule of Generation: Avoid hand-hacking; write programs towrite programs when you can.

Rule of Optimization: Prototype before polishing. Get itworking before you optimize it.

Rule of Diversity: Distrust all claims for “one true way”.

Rule of Extensibility: Design for the future, because it willbe here sooner than you think.

C compilation

{

Compilation

z

Preprocessing

z

Compiling

z

Assembling

z

Linking

{

Text source goes in, executablebinaries come out

Single-model program {

example

z

reverse a string

{

write

z

lecCh12/reversefirst.c

{

compile+link

z

gcc reversefirst.c

(output file: a.out)

z

gcc reversefirst.c –o reversefirst

{

execute

z

reversefirst

Preprocessing

{

The preprocessor takes a C (or C++) file and

translates directives into source code {

Directives are used to make programs

compact and portable to different environments {

They include:

z

#define, #include, #ifdef

z

‘#’ must be the first non-whitespace character

{

Note that directives are not terminated by ‘;’!

More macros

So, the following code segment: #define

test(

f1,

f

f

f

x

test(6,

y

test(x,

Is preprocessed into this before compilation: x

y

x

Compiling & Assembling

{

The compiler translates the preprocessed source intotext assembly language files (often “.s” on Unix, or“.asm” on Windows)

{

The assembler takes the assembly file and translatesinto machine code, in binary object files (“.o” on Unix,“.obj” on Windows)

{

Object files have multiple sections, such as forinstructions and data, and are machine specific

Separating the stages

{

With most compilation systems, it is possible to

perform all of these steps separately {

Why? You may want to use different programs for

particular parts of the process. with g++: -E = only preprocess, do not compile -S = stop after compile, do not assemble -c = compile/assemble, but do not link

Multi-module programs

{

Large software systems are divided inmultiple files

z

library files contain reusable functions

z

those functions are usually used from differentmain programs

{

Each file can be compiled separately intoobject files

{

The object files can be linked together intoan executable

Compiling

{

We can compile each source code fileseparately using the

–c

of cc

>gcc –c reverse.c >gcc –c main1.c

{

As an alternative you can list all the sourcecode files in one line

>gcc –c reverse.c main1.c

{

An object file .o is generated for everysource code file specified

Linking

{

To link the object files into inexecutable type

gcc reverse.o main1.o –o main

{

Run

>main