20
1 CSCI204/MCS9204 Autumn 2004 The C Family and Unix Unix Process Models and Advanced Unix Commands. Daniel F. Saffioti School of IT and CS The University of Wollongong Objectives of this lecture. In this lecture we are going to discuss the following; – Compiler programs under Unix. – Using Shell meta characters. – The Unix Process model. – A few more commands. The wc command. The wc command is a nice little tool which tells you how much data is inside a file. It will tell you the number of lines, words or characters in a file. You can control this using options: $ wc filename . . . The number of lines in the file is determined by the number of newlines ‘\n’ in the file. You should note that PC’s and older Macs use different end of line characters on there files e.g. Macs use \r. The most common options are -c, -l and -w. $ wc -l filename Number of lines in the file. $ wc -w filename Number of words in the file. $ wc -c filename Number of characters in the file. The find command. Sometimes there will be files which you will loose. If you want to find a file you can do so by using the find command. The find command also allows you to find files by looking for traits. If you read the man page you will find that find can take a number of options.

C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

  • Upload
    eword

  • View
    214

  • Download
    0

Embed Size (px)

DESCRIPTION

C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

Citation preview

Page 1: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

1

CSCI204/MCS9204 Autumn 2004The C Family and Unix

Unix Process Models and AdvancedUnix Commands.

Daniel F. SaffiotiSchool of IT and CSThe University of Wollongong

Objectives of this lecture.

In this lecture we are going to discussthe following;

– Compiler programs under Unix.

– Using Shell meta characters.

– The Unix Process model.

– A few more commands.

The wc command.

The wc command is a nice little tool whichtells you how much data is inside a file.

It will tell you the number of lines, words orcharacters in a file. You can control this usingoptions:

$ wc filename . . .

The number of lines in the file isdetermined by the number of newlines‘\n’ in the file.

You should note that PC’s and olderMacs use different end of linecharacters on there files e.g. Macs use\r.

The most common options are -c, -l and-w.

$ wc -l filenameNumber of lines in the file.

$ wc -w filenameNumber of words in the file.

$ wc -c filenameNumber of characters in the file.

The find command.

Sometimes there will be files which you willloose.

If you want to find a file you can do so byusing the find command.

The find command also allows you to findfiles by looking for traits.

If you read the man page you will find thatfind can take a number of options.

Page 2: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

2

The find command typically looks like this;

$ find pathname options

The pathname represents where you wouldlike the search to start. Do not make this /.

The options describe what it is you arelooking for. Refer to the man page for furtherinformation.

Lets say we wish to find the file phd.txt. I thinkit is in my home directory but unsure where.The command would look like this

$ find ~/ phd.txt

OR

$ find /home/cs-staff/d/dfs phd.txt

You may be wondering what ~ is.. Well thismeans the path of your home directory.

In both cases the find command will attemptto find phd.txt in my home directory. It willdescend into all subdirectories.

You can also specify to find that you wish tofind files which have a particular file namepattern. This is done by using shell wildcards.

In addition to all this you can get find to findfiles on different traits i.e. file type, data ofcreation, permissions etc.

You can even get find to do things when anobject is found using the -exec option.

Aliases (Soft and Hard Links).

Can anyone remember the different betweena soft link and a hard link.

To make a hard link we use the ln command.

$ ln filename newfilename

This will make newfilename a reference tofilename. This is a hard link, thus the sameinode number is recorded in the targetdirectory entry.

Symbolic links/ soft links are created byusing the -s option.

$ ln -s filename newpathname

We can see where links in the filesystem occur by using the ls commandand the -l option.

A soft link has the l as the first characterset in the permission string.

Here is a example;

-rw------- 2 dfs cs-uow 112712 Aug 6 08:59 hardlink_main.cpp

-rw------- 2 dfs cs-uow 112712 Aug 6 08:59 main.cpplrwxrwxrwx 1 dfs cs-uow 8 Aug 1 10:11 softlink_main.cpp ->

main.cpp

In the above example, symbolic links aredenoted by the leading ‘l’.

Notice on the hard link the link field is 2 asopposed to 1 (2nd column).

Page 3: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

3

The Unix Process Model.

The Unix Process model is complex yetvery powerful/ flexible.

A process in Unix is a unit of execution.You can think of a process as a singleinvocation of a application.

Most processes are created from theshell. Remember the shell is a point ofinteraction between the user and theoperating system.

When a command, which is not part ofthe shell is entered the shell forks a newprocess.

fork(2) is a Unix system call in libCwhich allows a process to duplicateitself.

During the duplication, the process thatis being forked is copied to a newmemory segment.

At that point the new process is identicalto the parent.

Once this is done, another system callcalled exec(2) is invoked which meansexecute on load.

Exec(2) loads the program that is wanting tobe executed into the newly created memorysegment created by the fork system call. Theexec function call overwrites the addressspace with the information of the executablethat is going to be executed in its place.

This is the Unix process model. It involvescreating processes using fork(2) and exec(2).

We typically refer to this as the fork-execcycle.

Processes have a relationship by virtue of thisprocess.

As you can see the new process is createdusing fork(2) and the contents of the newprogram is loaded by using the exec(2)system call.

New ProcessCreated

Shell Program

fork(2) exec(2)

If you were to look at from the memorysubsystems perspective, the process wouldlook like this.

MemorySegment

Shell

MemorySegment

Shell

Program

Before fork/exec. After fork/exec.

Page 4: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

4

However it is not only shells that can dothis.

Everything in Unix depends on thisfork/exec cycle (even systemprocesses).

Sometimes the process may fail due toresource limits.

Describing a Unix Process.

Processes have the following characteristicsin Unix.

– Process State.Unix processes have a state, that is whether or notthey are using the CPU. A process could berunning or sleeping.

– Process Owner and Group.Processes in Unix have a owner (typically theperson who invoked it) and even a group. It issimilar to a file permission.

When processes are created, theyautomatically get the following information.

Some of these properties can be inherited.

– A real owner/ group ID a a effective owner/groupID.

– Process group ID.– Controlling Terminal.– File mode and creation mask information (typically

called umask).– Inherited environment variables.– Resource Limits.

Unix Process Hierarchy.

Just like a file system, the kernel implementsrules for processes.

We know that in Unix there could be amultitude of users. Each user in Unix has auser-id which uniquely identifies them.

Processes are owned by a user and group,just like files.

For example;

$ id

Causes my user-id (8515) and mygroup-id (200) to be printed. Eachprocess gets this information when it isinvoked.

The userid Zero (0).

If you have a user-id of zero (0) then you areconsidered the root user.

The root user also known as the super user isthe most privileged user in a Unixenvironment.

There are certain operations which ONLYroot can perform.

Page 5: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

5

The Hierarchy.

All processes on Unix hinge on a programcalled init.

The program init is responsible for managingthe startup of the machine once the kernel isloaded. The program runs as the root user.

Often this program start up other servicessuch as inetd, secure shell and others whichallow us to use the machine.

Logging into Unix - a example.

When a Unix machine starts up (at UOW) oneof the things init starts up is a program calledsecure shell (sshd).

The secure shell program listens for incomingconnections. When a connection is detected itasks a user to prove there identity beforecreating a shell enabling the user to log ontothe machine.

As you can see init starts the sshdprogram which in turn creates a shell.

Secure Shell

User Shell

initInit starts secureshell. Secure shell listens

for incomingconnections.

Shell invoked.

The proof!

Here is the init process.

root 1 0 0 Jan 29 ? 2:44 /etc/init -

Here is the sshd process that listens forincoming connections.

root 10748 2476 0 09:50:51 ? 2:44 /local/sbin/sshd

Here is my invocation of ssh to handle myconnection.

root 10748 2476 0 09:50:51 ? 2:44 /local/sbin/sshd

And here is my shell.

dfs 10751 10748 0 09:50:51 pts/42 0:00 bash

Is there a common piece of information?Yes - this is the second and third fields.

What does this mean?

The processes are all related.

The second integer from the left indicates theparent process id.

The first integer from the left is the process-idof the process.

Each process created in Unix is given aprocess-id which uniquely identifies it (duringits executiion).

Page 6: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

6

As you can see each process is related toone another. Notice the relationship betweenthe processes and the process id’s.

init

sshd

Mysshd

shell

ID=1

ID=2476Parent=1

ID=10748Parent=2476

ID=10751Parent=10748

Obtaining this information.

To obtain information about processesthat are currently running on thecomputer we use the ps command.

The ps command has a number ofoptions and arguments.

If I type ps at the $ prompt, I get the following;

$ psPID PPID TTY TIME CMD10751 10748 pts/42 0:00 bash

The first column is the process id (the uniqueidentifier).

For the time being you can ignore the TTYcolumn. The next column is TIME. This is thenumber of minutes and seconds the processhas run on the CPU.

The last field CMD represents the name ofthe command/program.

The PPID column is significant because itdetermines the parent process id of theprocess (in the example on the last slide itshowed the parent of the shell).

The TTY column is the terminal associatedwith the process. Some processes may nothave a TTY associated with the process.

Each login to a Unix machine willNORMALLY have a different TTY associatedwith it.

Running ps on its own gives you, yourcurrent running processes.

What if you want to spy on a friend?

$ ps -u loginname

Where loginname is the username ofthe friend you wish to ‘spy’ on.

To obtain information about a user onUnix we typically use the fingercommand.

$ finger dfs

This will return information about theuser dfs. Instead of giving a usernameyou may opt to give finger a string tomatch i.e. part of a surname.

Page 7: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

7

With reference to ps, you can get informationabout ALL running processes by using the -eoption.

$ ps -e

You can get a full listing (all informationrelated to your processes) by going;

$ ps -f

It should be noted that the default behavior isto show the processes of the user thatinvoked the command.

It is possible to combine these twooptions together to give you a full listingof every process.

Another Example.

Here we have;

$ ps -f -u dfsdfs 10751 10748 1 9:50:55 pts/42 0:01 /usr/bin/bash

As you can see there ius a process calledbash, owned by the user dfs. The PID of thisprocess is 10751 and its parent is 10748. It isassociated with the TTY pts/42 and hasconsumed 0:01 seconds of CPU time.

Multiple Processes.

Remember this?

$ nedit main.cpp

What happened?

Well the current terminal would execute thenedit program with the file main.cpp. Until thecommand terminated the window wasinactive.

When we execute a program the shellnormally waits for it to terminate beforeallowing us to do anything else.

This situation can be resolved by using the &operator.

The & operator allows us to run more thenone command. It basically means to put theprocess in the background and continueworking on the foreground process.

So our hopeless situation can beresolved by;

$ nedit main.cpp &

We would then be able to use theterminal whilst editing.

Page 8: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

8

Another example.

To demonstrate this point I have written asimple program;

#include <iostream>using namespace std;

int main(){

long x = 0;for ( ; ; )

cout << x << endl;return 0;

}

Now assuming the source file is compiled intothe executable program, I could do thefollowing;

$ program > junk.out &

The > junk.out makes it so the output ofprogram is sent to the file junk.out ratherthen the display.

When this is done we will see the following;

$ program > junk.out & [1] 15343

The integer returned is the PID. The processwill continue to execute in the background.

I can return to that program any time byusing the fg command.

$ fg process-id

Of course there are many other options/arguments to the fg command.

What do you think fg stands for?

Die, Die and Die.

Sometimes a program will get out of controland start doing strange things. In the eventthis happens the is a command called killwhich can help you.

Kill has the following form;

kill [ [-s] signal ] process-id . . .

Don’t worry about the signal for now.

Using my example from before - I could killprogram by using the following command.

$ kill 15343

This would send a signal to the processinstructing it to terminate. The process wouldterminate.

However if I mean business I could specify asignal.

$ kill -9 15343

Means to kill now, you must die. The -9is a signal, commonly known asSIGKILL.

The other kill command was much morepleasant, as it sent the signal SIGINTwhich is Interrupt.

Try to avoid signals such as -9 becausethey are potentially dangerous.

Page 9: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

9

Using Shell Meta Characters/Wildcards. The shell is a interpreter and by virtue of this

fact it can do complex tasks on the fly.

Infact the shell is actually a powerfulprogramming environment.

One of the things the shell can do is interpretspecial characters. These are called metacharacters/ wildcards. These meta charactersinfluence the output/ execution of shellcommand.

Wildcards/ Meta Characters are usedwhen we do not wish to a specify afilename or string. We use them to aidon matching on a particular pattern.

We will go through each wildcard oneby one and illustrate its behavior withexamples.

The * Wildcard.

The * (asterik) is the most common wildcard.

The wildcard tells the shell to search in thecurrent directory for files and directorieswhich match any sequence of characters.

$ echo *

Means the * would substitute for all objects inthe current working directory. The result is alisting just like the ls command.

It is possible to use this wildcard withliteral strings. For example:

$ ls h*

Means match all objects in the currentworking directory that begin with hfollowed by anything. In this case ls willoutput matches contents.

For time to time I do things like this;

$ cat */main.cpp

This means output main.cpp for eachdirectory in the current workingdirectory. I typically do it for assignmentprocessing.

The ? Wildcard.

The ? Wildcard will match any singlecharacter in a filename.

$ cat ?ain.cpp

The above pattern would see any fileending with ain.cpp but beginning withany character being outputted.

Page 10: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

10

Here is another example;

ls ?04

Will list the contents of any directory that’send with 04.

I could even do things like this

ls /share/cs-pub/?11

This would list the contents of the directories111 and 911 in /share/cs-pub.

Notice how the ? Substitutes.

The [ ] wildcard.

The [ ] wildcard is used to match adomain of characters. Inside the [ ] wenormally specify all the characterswhich we wish to match on.

One set of [ ] matches one character.

For example;

ls /home/ug/[abc]

Would output the contents of the directories;

/home/ug/a/home/ug/b/home/ug/c

As you would expect it is possible to usecombinations of wildcards.

The contents of [ ] does not need to beordered in the previous examples.

However if you are lazy, you can specifydomains i.e. match a character from withinthese bounds.

This is done by using the - operator.

[a-z][A-Z][A-Za-z][0-9]

Don’t take me literally.

The cat command accepts wildcards but alsoaccepts strings to represent filenames.

Sometimes you may want to use a wildcard ina filename or use white space.

The problem with white space in the shell isthat it acts as a delimeter. The shell see’s itas a command separator.

For example, if you have the file

My Diary.txt

When you enter the command

$ cat My Diary.txt

The cat command will attempt to output Mythen Diary.txt.

There are several solutions to this problem.

Page 11: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

11

Space in shell has a meaning just like thewildcards. If you do not wish to have thedefault meaning be used - you simply quotethe string using “ and “.

$ cat “My Diary.txt”

Anything between the “ “ which has anymeaning to the shell will be ignored. Thismeans we can use spaces and wildcardswithout worrying about the shell translatingthem.

Quoting is very important in shell.

Another way around this problem ofcharacters having meaning in the shell is toescape them using \.

For example;

$ cat My Diary.txt

Will give us strange behavior.

$ cd “My Diary.txt”

Will output the file My Diary.txt.

$ cat My\ Diary.txt

Will output the file My Diary.txt.

Another example;

$ nedit the cat sat on the mat.txt &

Will not give you the file “the cat sat on themat.txt”.

If you would like to access that file then quoteit or escape it.

$ nedit “the cat set on the mat.txt” &

OR

$ nedit the\ cat\ sat\ on\ the\ mat.txt &

Other Shell Meta Characters andWildcards. Some shells have a ~ tilda to represent the

home directory. This is particularly true inBash.

If you entered the command;

$ cd ~/

It has the same effect as;

$ cd

Shell Variables.

When you log onto a Unix machine, you aregiven a shell.

In order to define the functionality of yourenvironment, variables called shell variablesare defined.

You can see many of these being defined inthe file .profile.

As you may recall the .profile file was used todefine the look/feel of your environment.

Changing the values of these variables givesyou different functionality e.g.

WINDOW_MANAGER=fvwm2

can be changed to

WINDOW_MANAGER=olvwm

It is possible to change shell variable valuesin the shell.

Page 12: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

12

To find out what shell variables exist youenter the command env.

$ env

One of these variables is HOME.

This variable defines the absolute path ofyour home directory.

These shell variables can be used in anycommand. The shell substitutes them forthere value.

To extract the value of a shell variablewe prefix it with a $ sign.

$ cd $HOME

This will change you current workingdirectory to your home directory.

We will talk more about the shell lateron in this subject. There is so muchmore to learn.

Unix Redirection and Pipes.

Remember the cat program.

$ cat filename

If you cat a file, the file is displayed. Howeverif it is more then one screen in size, it falls offthe end of the screen.

We may want to process the programs outputbefore displaying it.

What if we have a program (think abouthow I mark your assignments). What doI do if I want to test your assignmentusing a script?

We may not want the keyboard to bedirectly attached to the program.

What if we want to send the output of aprogram to another program for inlineprocessing?

Unix supports redirection and pipes.These tools are designed to solve theproblems I just raised.

STDIN, STDOUT and STDERR.

Remember in CSCI111/CSCI121 how wehad cin, cout and cerr.

What is the different?

– CIN is standard input (STDIN - file descriptor 0)– COUT is standard output (STDOUT - file

descriptor 1)– CERR is standard error (STDERR - file descriptor

2)

Page 13: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

13

When you read or write to one of thesestreams/ descriptors the data goes to aspecific place.

Typically if you read from STDIN the inputcomes from the keyboard.

If you write to STDOUT the output goes to thedisplay.

If you write to STDERR the output goes to thedisplay.

UNIX is very flexible in this regard because itallows you to change where these guys point.

Redirecting output and input.

From time to time you will want to changewhere input or output goes/ comes from.

Sometimes we will want t program to take itsinput from a file, as opposed to a keyboard.

We do this by using the >, >> and <operators.

This is trivial in Unix but it can be aharder task in legacy operating systemssuch as Mac OS 9 and Windows… (Ishouldn’t call Windows legacy yet).

Redirection Operators.

There are a number of redirection operators:

> redirect standard output to a file< redirect standard input from file>> redirect standard output to file by

appending.

As you can see the operators control wherethe input/output comes from in our programs.

Here is a example:

$ ls > filename

Will redirect the output of the ls command tothe file called filename. This means the ls’sstandard output is written to the file.

If we wanted to add the output of the lscommand to a existing file we would go;

$ ls >> filename

We can even do this:

cat file1 file2 file3 > outputfile

This will result in the cat programopening each file and concatenatingthem. The output (standard output) isredirected to the file outputfile.

So as far as you can see we can controlwhere output is sent to. It does notalways need to go to the terminal.

Page 14: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

14

Notice that > and >> only work forstandard output.

We can even control where input comes fromfor a program, by using the < operator.

For example;

$ mailx dfs < /etc/inetd.conf

Will send the user dfs a email. The contentsof the mail will be what ever is in the file/etc/inetd.conf.

Input redirection allows us to state where thedata for standard input comes from.

The program will read from standardinput till EOF.

You should note that we can mimic EOFin Unix by pressing Control d. Oftenrepresented as ^d.

Another example.

Here is a simple program in C++ that reads astring from standard input. It does this till EOFis reached.

#include <iostream>using namespace std;

int main(){

char name[100];while (cin >> name){

cout << “Got “ << name << endl;}return 0;

}

The loop will terminate at EOF on

standard input.

Lets see how this program behaves withredirection.

Assume the previous program iscompiled into the executable foobar.

$ foobar

In this case we would have to enterdata. We press ^d to terminate theinput.

However if we have a file calleddataset.txt which contains peoplesnames we could do this:

$ foobar < dataset.txt

This would result in the program foobarreading from that file as if it werestandard input.

This would continue to the EOFcharacter.

Page 15: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

15

Redirecting from Special Files.

Remember files such as /dev/null and/dev/zero.

We you can read/write to these files.

Lets say you would like to get the listing ofcontents in directory Q. But you don’t want tosee it.

This may seem ironic but you will see why wedo it later.

We would do something like this;

$ ls Q > /dev/null

This would attempt to output thecontents of directory Q. However theoutput is redirected the the file /dev/nullwhich as you may recall just makes itdisappear.

It is possible to read from files such as/dev/urandom and /dev/zero.

Problems with redirection.

The problem with redirection though issometimes programs are interactive.

Prompts and error messages may be printedto standard error and well redirection mayresult in these disappearing.

Another problem with redirection is we do notsee the other file descriptors.

Pipes and Pipelines.

Lets try to understand this first.

process

STDIN STDOUT

STDERR

Programs/ processes takes input fromsomewhere which comes in via STDIN. Theymay produce errors which may gosomewhere associated with STDERR andsometimes they may produce output whichgoes somewhere associated with STDOUT.

Notice the word somewhere.. Somewhere inactual fact could be anywhere.

Can somewhere be a program? Why not!

Its called a Pipeline.

Unix provides an internal data structure calleda pipe.

The pipe takes the output of one program anduses it as input into another.

So for example the output of program a couldbecome the input of program b. This meansstandard output of a becomes standard inputof b.

Page 16: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

16

As you can see, the output of program abecomes the input of program b.

Program a Program b

Output

Input

Examples of Pipelines.

Here are two examples;

$ ps -ef | more

This would take the output of the ps -efcommand and us it as the input to more.The result is page sized output.

This is a example of a pipeline.

Another example would be;

$ cat /etc/hosts | wc

This would cause the cat command tooutput the file /etc/hosts.

However the output (standard output) issent to wc. The wc program counts thelines, words etc from input.

Another example would be:

$ ls | sort -r

This takes the output of the ls program anduses it as the input to the sort program. Thesort program would reverse the input andthen output it.

It is possible for pipelines to go one forever.

$ a | b | c | d | e | f | g

Pipelines allow a degree of parallelism.

Splitting a Pipeline.

Remember before how from time totime I said program can be interactive.

Sometimes we want to see what theprogram produces in terms of output butwant it to be redirected to a file as well.

This is called splitting a pipeline.

The tee program takes as its argumentthe name of a file.

What it does is takes input, writes theinput to the file and passed it along tothe next stage of the pipelines. This isdone as output of the tee utility.

Page 17: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

17

For example:

$ cat file1 file2 file3 > wc

This would result in the total size of all threefiles being reported. But what if I wanted thecombined output to be stored?

Well I would use tee.

$ cat file1 file2 file3 | tee output | wc

When the command finishes we will have afile which contains all three filesconcatenated.

The C++ Compilation Model

The process of compilation typicallyinvolves taking your source files,resolving preprocessor directives,compiling which produces the assemblycode, then linking symbols with theappropriate libraries.

This yields an executable.

Above is the C++ compilation model.

Preprocessor

Compiler

Assembler

Linker

Source Code

Assembly Code

Object Code

Executable

Libraries

The pre processor.

The pre processor accepts source files asinput and is responsible for.

– Removing comments– Interpreting special pre processor directives such

as #define, #ifdef etc.– File inclusion e.g. $include <iostream.h>

It turns out that the C pre-processor (which isalso used in C++) is the program cpp.

The compiler.

This is an exercise in parsing andtokenising.

The compiler produces assembly codefrom the source code received from thepre-processor.

The assembler.

The assembler creates object codewhich is specific to a particular target i.earchitecture.

On some platforms you can see theobjects in the file system.

Page 18: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

18

The linker.

The linker takes the object file andresolves symbols with the appropriatelibraries.

Libraries can be static or dynamic innature.

On Unix the linker is the command ld.

Compilers available to you.

At UOW there are two compiler suitesavailable to you.

– Sun Forte (aka Sun Workshop 6.0). Thebinaries are CC and cc.

– GNU Compiler suite. The binaries are gccand g++.

CC, cc, g++ and gcc.

CC is the Sun C++ compiler where as cc isthe Sun C compiler. We will be using these inthis subject.

gcc is the Free GNU C compiler, whilst g++ isthe C++ compiler.

There are some slight differences betweenthese two compilers.

Using Sun C++ for the first time.

Assume we have a source file calledmain.cpp. How do you compile it?

$ CC main.cpp

In this case no .o file is produced. Instead afile called a.out is produced. This is aexecutable which you can run by typing;

$ ./a.out

Compile errors are sent to STDERR.

If you would just like to perform thecompilation (to just get the .o files) you wouldgo:

$ CC -c main.cpp

The result is a file called main.o in the currentdirectory. This is a object file which has notbeen linked with any libraries.

You could then go:

$ CC main.o

To link the object file to the appropriatelibraries and produce a executable.

You can change the name of the outputfrom a.out to anything you want.

$ CC -o prog main.cpp

This will produce a executable calledprog.

Page 19: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

19

Sometimes your program will havemore then one source file. You mayhave something like this:

$ CC main.cpp subs1.cpp subs2.cpp

This is particularly important for whenyou start to work with classes.Sometimes people just do this;

$ CC *.cpp

What about the GNU Compiler?

Same ideas apply. Arguments are the sametoo.

$ g++ main.cpp

OR

$ g++ -o prog main.cpp

OR

$ g++ -c main.cpp

Advanced Redirection and ErrorMessages.

Consider the following program:

#include <iostream>using namespace std;

int main(){

cout << “Hello” << endl;cerr << “Goodbye” << endl;return 0;

}

When you run the program you will getthe following output.

HelloGoodbye

How can you differentiate betweenstandard output and standard error?

Assume the name of the program isoutput.

By default standard output and standarderror point to the terminal.

You can control whereSTDOUT/STDERR go by using the;

n>

Using this notation, n is redirected towhatever follow the >.

n is the integer file descriptor.

Thus we can do the following;

$ output 1> output.txt

On the screen we will only get

Goodbye

In the file we will get the output

Hello

Page 20: C Family and Unix University of Wollongong 2004 - Lecture Notes week2part2

20

This is equivalent to:

$ output > output.txt

If I did not care for standard output Icould simply throw it in the trash byredirecting to /dev/null.

$ output > /dev/null

How would I control where STDERR goes?

Well we know it is file descriptor 2 so:

$ output 2> output.txt

This would yield the output:

Hello

And the contents of the file output.txt wouldbe:

Goodbye

Consider the following.

Assume we compile a program calledmain.cpp.

$ CC main.cpp

Now assume main.cpp has hundreds ofcompile errors and the output of thecompile errors consumes more then asingle screen.

Because the errors go to the screen we maybe fooled into thinking this would work;

$ CC main.cpp | more

This would break standard output into pages.However the errors don’t come out onstandard output. They come out on standarderror.

Remember the | operator only passesstandard output.

Some programs like compilers printerror messages to standard error. If wewent;

$ CC main.cpp 2> filename

This may be better because the errorcodes will be redirected to the file. But Iwill not be able to see them in real time.

If we wish to see the errors on thedisplay in a sensible fashion we canredirect STDERR to STDOUT using thefollowing notation.

$ CC main.cpp 2>&1 | more

2>&1 means take STDERR and send itto STDOUT. It can be used with anyarbitrary file descriptor.