introductory text for intermediate programming · introductory text for intermediate programming...

35
Introductory Text for Intermediate Programming Andreas Keese [email protected] Dominik J¨ urgens [email protected] Institut f¨ ur Wissenschaftliches Rechnen Technische Universit¨ at Braunschweig http://www.wire.tu-bs.de November 14, 2014

Upload: vannhi

Post on 10-Aug-2018

283 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

Introductory Text forIntermediate Programming

Andreas Keese [email protected]

Dominik Jurgens [email protected]

Institut fur Wissenschaftliches Rechnen

Technische Universitat Braunschweig

http://www.wire.tu-bs.de

November 14, 2014

Page 2: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches
Page 3: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

Contents

1 General Remarks 51.1 How to read this Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 The Working Environment UNIX 92.1 UNIX command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.1.1 Where do I get a command line? . . . . . . . . . . . . . . . . . . 92.1.2 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.1.3 Outputredirection while looking for help . . . . . . . . . . . . . . 14

2.2 The Help System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.2.1 The Program man . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.2.2 The Program apropos . . . . . . . . . . . . . . . . . . . . . . . . 162.2.3 Manpage Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.3 Editors in UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.3.1 Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3 First Steps with the Compiler 193.1 The first Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.2 Warnings by compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4 Using Libraries 214.0.1 Preprocessing, Compiling, Linking . . . . . . . . . . . . . . . . . . 214.0.2 The Preprocessor — Textsubstitution as preparation . . . . . . . 214.0.3 The Compiler — translating language . . . . . . . . . . . . . . . . 224.0.4 Linker — linking programs . . . . . . . . . . . . . . . . . . . . . . 22

5 Tools in Software Engineering 255.1 Build Tool – Make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

5.1.1 Implicit rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.1.2 Own rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305.1.3 More about Make . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

5.2 Version Control – Subversion . . . . . . . . . . . . . . . . . . . . . . . . . 315.2.1 The “Repository” . . . . . . . . . . . . . . . . . . . . . . . . . . . 315.2.2 The “working copy” . . . . . . . . . . . . . . . . . . . . . . . . . 325.2.3 Working on a Working Copy . . . . . . . . . . . . . . . . . . . . . 335.2.4 A Repository with multiple users . . . . . . . . . . . . . . . . . . 345.2.5 Multiple Working Copies, Conflicts and their resolution . . . . . . 35

3

Page 4: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches
Page 5: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

1 General Remarks

This text contains introductory information for the course Intermediate Programming atTechnische Universitat Braunschweig. This course consists of some labs without lecture.

Website To stay in contact with our students and to announce news we use our WWW-Pages at

http://www.wire.tu-bs.de/ADV

If you take part in this course you should check that site occasionally to be informedabout any changes.

Prerequisites Knowledge of another imperative programming language is necessary,however this text also references other introductory texts to help students who don’thave much programming experience.

Listenership The Listenership of this course has varying backgrounds. Electrical en-gineering takes a large part, but other scientific or engineering fields are present.

Because of this scientific and technical oriented listenership no advanced developingenvironment will be used. It would distract from the software developing process.

Learning targets You should learn the following in this course:

• programming algorithms and data structures in C

develop and extend applications

use and develop datastructures

• understanding resource management (especially memory management)

• getting to know software development tools

compiler, linker and debugger

• getting to know software engineering tools

version- and configurationmanagement (Subversion)

tools to compile complex programs (Make)

5

Page 6: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

1 General Remarks

Software development tools If you want to understand the wealth of software devel-opment tools it would be very inhibiting to use development environments like KDEvelopor Eclipse. In this course we use the minimal toolset, which is used in big developmentenvironments as well, albeit ”hidden behind the curtains”. Participants in this courseshould not use integrated development environments.

Choosing the working environment In a scientific and technical working environmentyou will be confronted with different operating systems and most will be Unix-like,especially if you are developing embedded systems or other nonstandard computingdevices. Most of these systems are based on and closely coupled with ANSI C. Thatway developing with C is comparatively easy in these Unix-like systems. LINUX is awell-known example for a Unix-like operating system.

Participants in this course a strongly encouraged to work with Linux through-out the course

Using your own computer for this course You can of course use your own computerto work in this course, but you will have to use a UNIX environment, since this courseincludes learning to work in that environment.

If you have Linux on your computer, using that won’t be a problem. Windows-Usersmust install a UNIX environment. Here are several ways to do this:

Virtual Machine On our website http://www.wire.tu-bs.de/ADV we provide a tuto-rial for installing a virtual machine (menu on the left side, bottom ”VM”) whichhas a similar configuration as the computers in the lab.

Instant Linux There are several Linux versions which you can start directly from CDor DVD without having to install anything. You can find one at http://www.

knoppix.net/.

installing Linux You could install any Linux version of your choice directly to yourharddrive, but that would be to much of an effort just for this course.

1.1 How to read this Text

The present text is not thought to be a text book on programming with the languageC. For this purpose we recommend a dedicated text book which aims at teaching thelanguage.

The Translation This text has been translated to English. We had to be fast withthat, but we hope that the quality is good enough for you to understand the texts.

6

Page 7: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

1.1 How to read this Text

Recommended Literature We recomment the book The C Programming Language ofthe athors Brian W. Kernighan and Dennis Ritchie. It is a classical book by the inventorsof the language and gives a systematic overview about the implemented concepts. If youhave problems with this book, because it is not really an introduction to programming,do not hesitate to find another book in english, but please inform us, so that we canrecomment this book officially.

Structure of this Text At the beginning the text gives a guided introduction to theuse of Unix systems. In chapter 3 the compiler gcc is introduced. You should read thistext in front of a computer to try out the commands which are given in the text.

The thereon following parts discuss practical aspects of software development. In thispart we discuss practical issues, which go beyond understanding the programming lan-guage.

Programming does not consist of just writing down programs; additionally the programshave to fulfill requirements with respect to quality, extensibility and maintainability.Therefore a systematic proceeding in developing programs is important. For this rea-son, this course does not only focus on learing the language. The labs are designedto introduce many important concepts like pseudo-code, process modelling, algorithmparametrisation, debugging ,build tools and version control,

7

Page 8: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches
Page 9: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2 The Working Environment UNIX

2.1 UNIX command line

Historically the command line is much more prevalent in UNIX. That is due to the fact,that at the time of UNIX’s creation the computer hardware simply was not capable ofdisplaying graphics. Thus many users consider the command line to be a archaic tool,but using it still has some advantages over graphical user interfaces today.

After having come in contact with the system you will learn that things you thought tobe impossible using a graphical interface are quite easy to perform in a elegant way onthe command line. You could for example combine two programs into a new one (see2.1.2).

The command line is simply a program which accepts text input, interprets it andoutputs the results as text. You can think of the command line interpreter as a sort ofservant, who accepts commands and executes them. Since the duty of that servant isn’tsimply cooking coffee, but simplifying the work with files on a computer he has somespecial characteristics. We’ll call our servant here shell .

The basics that you need to work through this course are summarised in the followingchapter.

2.1.1 Where do I get a command line?

Modern Linux versions follow the trend to hide the command line from the user tonot frighten beginners. To find the command line you will need to search through thesubfolders of the ”start menu” and look for something called ”Terminal” or ”Console”.If you start this terminal you will get a little window with the following content:

username@machine: $

In this window you can type commands and the shell running inside of it will interpretand execute them.

2.1.2 Fundamentals

Filesystem

Abstract A filesystem is a system for managing files. Since files are a common conceptin electronical data processing every operating system has such a filesystem.

9

Page 10: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2 The Working Environment UNIX

In an office you use folders for organising large quantities of ”data”. For example inaccounting you would have a folder labelled ”Accounting 2008”. In this folder youshould find all the documents you’d expect. This system was introduced in computersas well; Windows-users might recognise the term ”folder”. However on a computer youcan have a subfolder inside of a folder, which is quite complicated (if not impossible)to achieve in an office. Since using the term ”folder” isn’t quite optimal to describe afilesystem concept, UNIX uses the term ”directory”

There are two ways to describe a filename with a path. You can have a relative path oran absolute path. The relative path is the path relative to the current location and theabsolute path is the path from a global reference point.

Absolute Paths In UNIX there is only one root directory called ”/”, which is the justmentioned global reference point. From this reference point you can describe a path toany file on the computer. To describe a path through the directory in the filesystemUNIX has a special scheme. The scheme uses the symbol ”/” to access the content of adirectory, which was appointed in front of the ”/”. The name ”/bin” labels the directorywith the name ”bin” which is located directly in the root directory ”/”. If you now wantto access files or directories which are located inside the directory ”bin”, you use the”/” again, this time as a separator to get the contents of the directory. ”/bin/ls” isfor example the path to the file ls located inside the directory ”bin” in theroot directory

Relative Paths As mentioned above, you can describe paths relative. All you haveto do is omit the reference point ”/”. The directory you currently reside in is calledworking directory. The command pwd will give you your current location as an absolutepath. the path ”test/testfile” is the path to the file ”testfile” which is locatedin the subdirectory ”test” of the current working directory

Special Symbols In every directory there is a way back. This way is represented bythe symbol ”..”. Using ”..” as a path will get you one step back.

Environment Variables

Environment variables are special addresses inside the command line interpreter whichcan be assigned any value. You could for example define:

TEST=HELLO

This assigns the value HELLO to the variable TEST

If you want this variable to be used by other programs than the current command lineinterpreter you have to export it. This happen with the following command.

export TEST

10

Page 11: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2.1 UNIX command line

You can combine definition and export of a variable in one line.

export TEST=HELLO

To get the current value of an environment variable you simply precede its name with a$.

$TEST

The above command will tell us that the interpreter does not recognise the commandHELLO. So the interpreter interprets every input as a command. If you want to displaythe value of your variable you will need to use the echo command. It will outputanything you put behind it. So the following command will display the current value ofyour variable.

echo $TEST

Starting Programs

To start a program on the command line the command line interpreter needs to knowwhere it is located. A list of directories containing programs is stored in the environmentvariable PATH.

What programs are there? We just learned that the command line interpreter usesthe environment variable PATH to look for programs. So we shall take a look at its value.

echo $PATH

We see that it contains several directories separated by a :. One of these is the directory/bin, which contains many important programs. With the command ls we can take alook at its contents.

ls /bin

As you can see the command ls resides in that directory as well.

Important Commands This section should give you an overview of some importantUNIX commands.

list files ls will show the contents of a directory.

change directory cd PATH changes into the given directory by PATH.

create directory mkdir DIR will create the directory DIR.

remove directory rmdir will delete a directory.

copy file cp SOURCE DESTINATION will copy the file SOURCE to DESTINATION.

delete file rm FILE will delete FILE.

move file mv SOURCE DEST will move SOURCE to DEST.

11

Page 12: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2 The Working Environment UNIX

How do I run a program? If we write our own programs we want to run them as well.But since our working directory is not included in the PATH variable we have to explicitlytell the command line interpreter where our program is located. This can be done intwo ways. Either with an absolute path, which can be quite long and cumbersome totype every time, or with a relative path, which looks like this:

./program

The ”.” stand for the current directory followed by a ”/” to indicate that we want toaccess a file in the current directory and finally the name of our program. If you wouldhave just put program there, the command line interpreter would have searched throughthe directories given in the PATH variable.

Controlling Processes

In this section we would like to show how to control programs at the command line.

Process In UNIX every running program is represented by its process and associatedprocess id (short PID). By knowing the PID of a specific running program you cancontrol it with the kill command. If you don’t know the PID already you can find itusing the ps tool.

Stopping Programs To stop a running program you can press the key combination[ctrl+c].

Input– and Outputredirection, Pipelines

Next we’d like to show you the concept of input- and output redirection. With redirectionyou can sort of combine several UNIX-programs together. While programming this canbe quite handy to search through compiler messages or other output.

Nearly all UNIX shell programs work like this: they take input on their standard inputstream stdin, process them and output the results on their standard output streamstdout. An input stream can be a file, keyboard input or the output of another program.An output stream can be the screen, a file or the input stream of another program.

Usually the standard input stream is the keyboard and the standard output stream isthe terminal in which the program was started. For example look at the command cat.It copies data from it’s standard input stream to it’s standard output stream. Let’s tryit out:

cat

You will get a blinking cursor. Type some text and press the Return-key. Your inputwill be printed on the screen again. When you pressed ”Enter”, cat copied your input(from the keyboard) to it’s output screen (the terminal window). The fact that your

12

Page 13: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2.1 UNIX command line

input was already visible while typing shouldn’t bother you here - it had nothing to dowith cat.

Enter some more lines - everytime you press Return, the input is copied to the output.Exit cat by pressing [ctrl+d]. The key combination [ctrl+d] tells cat that the inputis finished and it can stop processing — word of advice: [ctrl+d] and EOF (End of File),that you will get to know while coding C, are not the same — however [ctrl+d] createsa EOF.

Now we want to redirect the output of cat, that means we want to change it’s outputstream. If you want to redirect the output into a file, you put > filename after thecommand.

Let’s try it:

cat > testfile

Input some lines of text, each followed by Return. You might notice that now yourtext is not displayed a second time. That was expected, since the output stream of cathas been redirected from the screen to the file testfile. Finish your input by pressing[ctrl+d].

Use the ls command to verify that a file named testfile was created. To view the contentsof that file we use again cat, but this time we redirect the input instead of the output.This is achieved by putting < filename after the command. Try it out:

cat < testfile

This should output your previously typed lines on the screen.

You can combine this behaviour of cat to copy a file (although generally cp is used forthis purpose):

cat < testfile > testfilecopy

This won’t output anything on the screen. The input stream was the file testfile andthe output stream was redirected into the file testfilecopy. Now check if the file wasreally copied:

cat < testfilecopy

This is the same as:

cat testfilecopy

This example works, because cat was programmed in a way that enables it to interpretthe first parameter as filename for the input stream.

Following are some uses and examples for output redirection. The redirection of input-and output stream work with all UNIX programs, which work with the standard inputand output streams1. Try the following commands (If you want to know, what the sort

command does, you can get help using the man command):

1We will make use of that feature in our exercises to feed our programs with input.

13

Page 14: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2 The Working Environment UNIX

ls > directory

cat directory

sort -r < directory

What has happened here? We redirected the output of ls in a file and then used that fileas input stream for the sort command. This command has sorted our file descendingand outputted it to its output stream, our screen.

With the help of the | operator you can merge several output redirections. The operatorredirects the output stream of the program on its left side into the input stream of theprogram on its right side. Since you can think of it as a sort of ”information pipeline”between two programs, it’s usually called ”pipeline-operator” or short ”pipe”.

Constructs of the form

command1 | command2

work like the following sequence of commands, however without creating a temporaryfile:

command1 > temporaryfile command2 < temporaryfile

We can apply this to our example where we sorted the output of ls – try the followingcommands:

ls | sort -r

ls | sort -r > sorted

cat sorted

ls -lF /usr/bin | sort | less

The last line shows, that you can combine as many pipelines as you like. Here the outputof ls is redirected to sort, sorted and then passed to the less command.

The less command outputs it’s input pagewise on the screen. You can exit it by pressing”q”. If you display something through less, you can scroll with the cursor keys up anddown.

2.1.3 Outputredirection while looking for help

Lastly we’d like to show you how you can look for functions that do something specific.Let’s assume you want to output some text in your C program, but you don’t knowexactly how to do it. So we’ll for the keyword ”output”.

apropos output

This returns a great number of results. Using less to scroll through all results isn’thelpfull either, since you don’t want to read hundreds of command descriptions:

apropos output | less

Since we are only interested in help regarding C commands, we have to limit the outputto section 3 of the man pages. The grep command will help us with that. It looks fora specific pattern and outputs just the lines containing it.

14

Page 15: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2.2 The Help System

apropos output | grep ‘‘(3)’’

This reduced the number of results drastically. We can now add further filters. We wantonly commands that do formatted outputs.

apropos output | grep ‘‘(3)’’ | grep ‘‘format’’

We reduced the results even more and have a better chance of finding what we arelooking for.

Changing the command line language

If you want to change the language of the command line, so that the programs man

and apropos output german or english help texts, you have to change the environmentvariable LANG to the appropiate value. You can get the available languages with thecommand:

locale -a

This document assumes that your system is set to English. The easiest way to achievethis is to set the environment variable LANG to the value C.

2.2 The Help System

There are several help systems availabe in UNIX systems. Especially while programmingit’s very helpfull to know at least know of them.

2.2.1 The Program man

To get help for a UNIX command you can use the man command. Simply use the nameof the program you want help for as the argument to the man command.

You can even get help to the man command itself:

man man

On most systems you will first see a help text for the apropos command (which is partof man), but if you scroll with the space key, you will get to the help for man. You canexit this help by pressing ”q”.

To train the use of man, look at some other help pages. If you want to know more aboutthe copy command cp or the directory listing command ls, try it:

man cp

man ls

15

Page 16: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2 The Working Environment UNIX

2.2.2 The Program apropos

The above described form of the man command has one disadvantage: You can only getinformation about a command if you know its exact name.

If you are looking for a command that does a specific task, but you don’t know it’s name,you can use apropos followed by a keyword to search. apropos will search through theshort descriptions of all manpages and look for commands containing your keyword.

If you are looking for general help you can try one of the following:

apropos help

apropos info

apropos manual

With every apropos command you will get a list of commands fitting the keyword witha short description.

2.2.3 Manpage Sections

The output of apropos should be apart from the number in brackets selfexplanatory.First in each line is the name of the described UNIX command, then there is in bracketsa number and then a short description of the command.

For example:

sort(1) - Sorts files, merges files that are already sorted, and

checks files to determine if they have been sorted.

There are very man UNIX commands — a typical UNIX system has around 12,000commands documented by man. To not get crazy while looking for something, thesecommands where put into different categories or sections.

Every manpage, that man can display, resides in exactly one section. For example allcommands that are meant for average users are in section 1, all system routines in section2 and all standard C functions in section 3. The number in brackets tells you in whichsection a command is located. In the manpages of man you will find a description of allsections and their contents.

With man you not only get help for UNIX commands, but also to functions of thestandard C library. That is quite helpfull during programming.

To know what is located in which section helps a lot while looking for manpages. Let’sassume you are looking for help to the C function printf. Your first try might be thiscommand:

man printf

16

Page 17: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

2.3 Editors in UNIX

Unfortunately the displayed manpage is not what we expected. Apparently UNIX knowsmore than one printf Try using apropos:

apropos printf

This will get us a number of commands including one printf which is located in section3. We mentioned above that section 3 is for functions of the standard C library, so thisis the command we were looking for. To look at it’s manpage type:

man 3 printf

Now you get a complete reference of the C function printf. If you use man to lookfor help regarding C functions you should always include the section number in yourstatement.

2.3 Editors in UNIX

2.3.1 Text Editors

To write programs you will have to edit files. There are quite a few different editors onour lab computers to do this. Have a look at the following programs and choose the onethat fits you most:

gedit

kate

nedit

emacs

We recommend using kate. You can start it by looking for it in the start menu or bytyping ”kate &” at the command line.

You can get more information about kate at it’s official website kate-editor.org2 or init’s own help system.

2http://www.kate-editor.org

17

Page 18: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches
Page 19: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

3 First Steps with the Compiler

In order to build an executable program from C source code, a Compiler is needed. Ittranslates the source text into a language that is understandable for the Computer. Inthis practice we will employ the GNU-C-Compiler gcc of the GNU-Organisation1, whichis free and is also in many other Platforms available.

3.1 The first Program

We would like now to show you the appliance of the Compiler with a simple example.Save the following program-code in Listing 3.2 in a file named helloWorld.c.

1 #include <stdio.h>

2 main()

3 {

4 printf("Hello , World\n");

5 }

Listing 3.1: An simple C-Program – helloWorld.c

You have to open the command-line (in the case that you do not know how, then youshould take a look in the part 2.1.1), and change to the directory where the file has beensaved2. Compile the file by typing

gcc helloWorld.c -o helloworld

The parameter -o (for “output”) requires for a further parameter, which gives the nameof the file in which the output should be written. The output of the compilation isan executable program . You can now execute the program, which the compiler hasgenerated by:

./helloworld

Hereby you have compiled and started your first C-Program.

3.2 Warnings by compiling

The compiler normally gives an error message only by syntactic mistakes. It can alsoaware you by dangerous constructions in your program and help you to correct logic

1http://www.gnu.org/2With the command cd as “change directory”

19

Page 20: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

3 First Steps with the Compiler

mistakes by these means. A warning most of the time means that the source-code wassyntactically (or ”grammatically”) correct, but possibly does not do what you want itto do.

This can be very valuable for the prevention of mistakes and should be used. When anerror has find its way into your code, it is difficult to find it again. Therefore you shoulduse any help you can get, to prevent errors. You should apply the -Wall-Option. Thisoption makes the compiler display more warnings.

Try this option with:

gcc -Wall helloWorld.c -o helloWorld

Now you will find two warnings. Correct the warnings, by giving the type int to thefunction main and return the value 0 at the end of the program.

1 #include <stdio.h>

2 int main()

3 {

4 printf("Hallo , Welt\n");

5 return 0;

6 }

Listing 3.2: An easy C-Program – helloWorld.c

This is important, because the return value of programs tell the operation system if theprogram was successfully executed or not. A return value 0 tells the operation systemthat ”everything is OK”.

20

Page 21: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

4 Using Libraries

4.0.1 Preprocessing, Compiling, Linking

The C compiler compiles the ”texts” written in the C programming language into thelanguage understood by the processor inside of the computer; machine code. This ma-chine code differs from computer to computer, so that the C language helps writingprograms that run on not just one particular machine but every computer for which aC compiler exists. The C language is therefore ”platform independent” and ”portable”.

You may know that for translating ”German” into ”English” it’s not enough to translatethe words one by one, you have to have understood the meaning. Compiling programsis very similar. For this reason the C compiler is a very complex tool that is steadilyupdated despite it’s old age.

Translating source code into a executable program is not done in one pass, but in severalsteps. It’s sufficient for us to separate this process into the phases that are really visibleto the user.

1. Preprocessing

2. Compiling

3. Linking

These three phases can be considered as separate processes.

Metaphor: Printing Books A nice Metaphor that explains this process lies in printingbooks. A book has many similarities to a program. At first it’s written by a human in alanguage that that human understands (programming). Normally this takes more thanone day to that in the end you have many files for one book which have to be combined(preprocessing). Then it has to be translated from text into another form which canbe understood by the printer, which essentially is machine code (compiling). At last,when all chapters of the book (modules) are printed they can be binded into a hardcover(linking).

4.0.2 The Preprocessor — Textsubstitution as preparation

In the first phase the quelltext is processed by the preprocessor. It interprets the com-mands in the lines starting with # (i.e. #include <stdio.h>). The preprocessor caninsert other files into a file (#include), substitute text (Macros with #define) and omitparts of the source code (#ifdef). The result is a new source code which is handed tothe actual compiler.

21

Page 22: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

4 Using Libraries

4.0.3 The Compiler — translating language

Since you don’t code complex programs in one go, but split it up in little separate parts— the functions, the compiler can’t compile everything at once and has to supportsegmentation as well. This happens in C with the help of so called object files (fileextension ”.o”). An object file contains all functions that are defined in it’s source code,but translated into machine code. For example the compiler invocation

gcc -c codeA.c -o A.o

translates all function to machine code which are defined in codeA.c.

Object files You can think of an object file as a box wherein file cards lie. On everyfile card the name of the functions is written on the front and the machinecode of thefunction on the back. If a program needs a specific function the ”Linker” can simplyreplace the machine code at the right place to form a working program out of these filecards.

You’ll find an exhaustive description of the compilation process in the appendix ??.

4.0.4 Linker — linking programs

The target of developing software is of course of an executable program. Till now wehave a bunch of file cards (object files) on which symbols are linked with machine code.

getting executable programs If you now want to get an executable program, youmust have a main function in your program — with other word a filecard with mainwritten on it.

1 int main()

2 {

3 return 0;

4 }

Listing 4.1: shortest C program – main.c

The program shown in listing 4.1 is complete an can be compiled into an executableprogram, because it has has main function. The compiling is done with the commandmake main.

Linking It could be the case that you use other functions in your code. The programin listing 4.2 can be compiled, translated into machine code (by issuing make main.o),however it cannot be linked, since the symbol sin is missing — with other words thefilecard with sin written on it.

22

Page 23: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

1 #include "math.h"

2 int main()

3 {

4 return sin (0);

5 }

Listing 4.2: a C program with a function call– main.c

To tell the compiler which symbols (or filecards) are present, there is the so calledheader file in which a list of symbols is defined. You’ll find further information about itin section ?? where modularisation is explained.

This problem isn’t really a problem but enables more flexibility. We now have severaloptions to make an executable program with listing 4.2. You can:

link object files Include a self compiled object file containing the sin function.

link libraries Use the C library which provides the sin function.

include definition Implement the function inside your code.

With other words, you can take any filecard box containing the sin function and linkthe definition into the program.

The above mentioned possibilities are interesting for quite different cases. We will lookat each of them again with a short example.

The linking of object files is the first variant that will be shown here. You will need tocreate a source file like listing 4.3. The function has to have the same datatypes for inputand return parameters that are defined in math.h 1, since they belong to the symbol —thus are written on the front side of the filecard.

1 double sin(double in)

2 {

3 return 1;

4 }

Listing 4.3: a C program with function call– main.c

This function doesn’t really implement a sine function, but that does not concern thecompiler.

The inclusion of a library will be looked at in section 4.0.4. A library is a collection ofobject files, so there’s not much to understand — with the filecard-metaphor explaineda library is a collection of filecard boxes.

The last option is the least modular, least flexible and therefore the least recommended.You should define a function directly in your program only when you are completelysure that you will never have to replace it. This variant is shown in listing 4.4. Now thesymbol is defined in the same object file where it is used — with the filecard metaphorexplained we have written a new filecard in the same box.

1Our program links math.h.

23

Page 24: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

4 Using Libraries

1 #include "math.h"

2 double sin(double in)

3 {

4 return 1;

5 }

6 int main()

7 {

8 return sin (0);

9 }

Listing 4.4: a C program with function call– main.c

Linking Libraries

Libraries are an important source for standard algorithms, that are used often andtherefore can be combined in a library. Libraries have the file extension ”.a” and thefilenames always start with the string ”lib”.

Available Libraries In UNIX libraries are usually stored in the directory /usr/lib. Alist of the available libraries on a system can be obtained by typing ls -la /usr/lib/lib*.a.If you want to use one of these libraries (i.e. the C math library libm.a), you have to tellthe compiler with the parameter -l. For the C math library the parameter -lm needsto be added when compiling the executable:

gcc -lm main.c -o main

Creating Libraries With the tool ar you can create your own libraries out of severalobject files. The call

ar sr libMYOWN.a objectA.o objectB.o

creates a library called MYOWN with the object files objectA.o and objectB.o, that willbe automatically searched in the file libMYOWN.a. The library MYOWN now contain allfilecards of objectA.o and objectB.o.

If you want to tell the linker, that this library shall be used to create a program, youhave to invoke the compiler with the parameters -L. -lMYOWN. The -L. tells the linkerto look for libraries in the local directory ”.” and -lMYOWN tells it to use the libraryMYOWN. With make main LDLIBS=’’-L. -lMYOWN’’ you can use make for this case (seesection 5.1.1 for more information).

24

Page 25: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

5.1 Build Tool – Make

The compiler gcc has been already used in simple programs like ”helloWorld.c”. Whenwriting complex programs, you should separate your program into smaller subprogramsand save this subprogram into separate files. Each of this files is compiled indepen-dently from each other to an object-file. These object-files are later bound to create anexecutable program.

A simple example In the first execution of our example program helloWorld.c, thecall to gcc looked like:

gcc world.c -o world

and it generated the file ./world — the executable program.

The same result is generated by the simpler call:

make world

In more complex programs a program will consist of several files, each one will have tobe compiled separately and later bound to the executable program.

25

Page 26: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

Complex example We considered an example which is formed by three files:

1 #include "add.h"

2 #include <stdio.h>

3 int main()

4 {

5 printf("Hello World 1+1=%i\n",add (1 ,1));

6 }

Listing 5.1: Example-Program bsp main.c

1 #ifndef __ADDH // WHEN__ADDH is not defined

2 #define __ADDH // THEN define__ADDH (avoids multiple Definition)

3 int add(int a,int b);

4 #endif // END

Listing 5.2: Example-header: add.h

1 int add(int a,int b)

2 {

3 return a+b;

4 }

Listing 5.3: Example-Implementation: add.c

We want to compile this project, so you could do it manually by using the followingcommands:

gcc add.c -o add.o

gcc bsp main.c add.o -o bsp main

or you can use the tool make. However for make, the dependencies between the files mustbe specified. The dependencies have to be defined in a makefile:

1 bsp_main: add.o add.h

2 add.o: add.h

Listing 5.4: Example Makefile: Makefile

The first line states in words:

The target ”bsp main“ depends on the files ”add.h“ and ”add.o”

and in the second line states the following:

The target ”add.o“ depends on ”add.h”

Call now

make

to ask make to build the target bsp main. In the Makefile we have defined, thatbsp main depends on add.o and add.h and in addition add.o depends on add.h. Inorder to fullfill our request (to build the target bsp main), make must firstly constructadd.o, before bsp main can be generated. Call make a second time

make

26

Page 27: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5.1 Build Tool – Make

and you get the answer:

make: ‘bsp main’ is up to date.

The tool make has recognised, that the work is already done, and all the files are up todate.

Now do the following change in the file add.c :

1 #include <stdio.h> // <-- changed

2 int add(int a,int b)

3 {

4 printf("CALL!\n"); // <-- changed

5 return a+b;

6 }

Listing 5.5: Modified example: add.c

In this way bsp main is not up to date any more, and the program has to be compiledagain. This will demonstrate a great example of make: make now a third call

make

due to our definitions of interdependencies of the targets, make recognises which newfiles must be constructed. Make knows from our Makefile, the bsp main depends fromadd.o. It also knows, that the file add.o can be created with help of add.c, and withthis is clear, first add.o and thereupon bsp main must be recreated.

From the example into the generality When you change a program source file, youmust recompile the file into an object-file and later link the executable program. Whenyou change a head-file, you must compiled again all the files that include it.

For the recompilation of a program one must know exactly, which files must be re-compiled, or one needs to recompiled every file all over again. To recognise which filesmust be new compiled, is a non-trivial task in a complex program where header-filesare potentially interdependent and other complications may occur. When one forgetsto recompile a file, it is possible that the program can be executed, but the resultingbehaviour is inconsistent. In the praxis it may lead to hours of searching fornon-existing errors.

Make is able to compute a list of steps which are to be executed to create the requestedgoals properly. To give make the possibility to do so you just have to specify theinterdependencies of the files in the project.

By calling make all files will be recompiled, which are dependent on any file which hasbeen modified since the last compilation. With this, only the files that are necessarywill be recompiled. This save time.

The call to make The program make can be called in the following way:

make -f makefileName targets

27

Page 28: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

• Thereby the option -f makefileName specifies the filename of the file in whichmake will find the dependencies.

When this option is absent the default file is chosen which is Makefile or makefile.

• The targets-Option declares, which targets specified in the Makefile should becreated. When the target-option is not given the target is chosen from the Makefilewhich was defined first (!!) (In the given example this would be the bsp main).

Practical application Our tip: Your Makefiles should normally have the name Makefile.As first targets you should specify the program (in our example bsp main) in Makefile,the the call

make

compiles the whole program by default.

When you write a program, you should always create a Makefile, in which you writeall the necessary dependencies for the recompilation. You can then edit your programarbitrarily being sure, that the command make will do everything whats necessary.

Artificial targets When you want to generate several independent targets by means ofmake, an artificial target should be defined in the Makefile. This artificial target thanhave to depend on all other targets which shall be created. This target can be called forexample as all.

all: targetA targetB targetC

The target all depends here of all the other targets targetA, targetB and targetC.With this definition make will take implicitly, that there shall be created a file calledall. You can tell make that all is just an artificial target, by marking the target all

to be PHONY. This can be done with the following line.

.PHONY: all

Doing so the target all and all its dependencies will be generated, even if a file withthe name “all” exists.

5.1.1 Implicit rules

During the development of a program, it is common that additional parameters (nor-mally called flags) must be given to the compiler or the linker-program. This flags aredirectly supported by make. In order to understand, how this can be done in make, onemust take a look to implicit rules (”rules integrated in make”), which are used by make.Firstly here is shown a rule for the generation of an object-file:

$(CC) -c $(CFLAGS) $(INPUT) -o $(OUTPUT)

28

Page 29: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5.1 Build Tool – Make

Expressions like $(...) represent the value of the variable ”...”. You can see, that you canswitch the compiler through changing the variable CC and to switch flags by the variableCFLAGS (INPUT and OUTPUT1 will be automatically set by means of the dependencies forexample to INPUT=add.c and OUTPUT=add.o). The command used by make to generatethe add.o-object-file with CFLAGS=-g and CC=gcc would be :

gcc -c -g add.c add.o

In order to control these variables, make has three different available mechanisms:

globally One defines global variables in the Makefile, simply by defining CFLAGS=-g inthe beginning of the Makefile.

individually One defines locally the variable for a particular target. For that one writesa new line (!!!) for a given target like target: CFLAGS=-g defining CFLAGS=-g

only for that target. (Warning: For this in reality a new line must be provided(add.o: add.h CFLAGS=-g does not work !!!)

dynamically One defines the variable when calling make. This looks like that: make

CFLAGS=-g

The same is valid for the implicit rules for linking, however other variables will be used:$(CC) $(LDFLAGS) $(INPUT) $(LDLIBS) $(OUTPUT)

Here are the variables $(CC), $(LDFLAGS) and $(LDLIBS) of our interest. These canbe again defined “globally”, “individually” or “dynamically”. The C-Math library libm

can for example be linked by specifying:

LDLIBS=-lm

Example: Linking the C-Math Library

Make the following change to add.c:

1 #include <math.h> // <-- changed

2 int add(int a,int b)

3 {

4 return sin(a)+sin(b); // <-- changed

5 }

Listing 5.6: Modified implementation example: add.c

The program doesn’t make much sense, but the goal here is the understanding of make.Our Makefile from the last section must be changed, because the function sin is definedin the C-Math Library which needs to be linked explicitly. For this you have the threepossibilities presented in the last section. All of them will be shortly presented here inan example. Firstly the definition of a global variable:

1 Input and Output are ”special variables”, more about this can be found at http://www.gnu.org/

software/make/manual/html_node/Automatic-Variables.html. Such variables are important inthe context of customised rules.

29

Page 30: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

1 LDLIBS=-lm

2 bsp_main: add.o add.h

3 add.o: add.h

Listing 5.7: Example of a Makefile linking C-MATH globally: Makefile

Now the individually:

1 bsp_main: LDLIBS=-lm

2 bsp_main: add.o add.h

3 add.o: add.h

Listing 5.8: Example of a Makefile linking C-MATH locally: Makefile

The dynamic candidate leaves our Makefile without changes, but the call of make changes

make LDLIBS=-lm

However in the dynamic case one must renew the variable every time when calling make,this makes no sense in this example, but may be interesting elsewhere.

Example: Setting Warning Level

Like mentioned in section 3.2, you should use a high compiler-warning-level. Thesewarnings will be activated by the Compiler-Flag -Wall. You should define this flagglobally in your project. This Flag is a Compilerflag, and we therefore have to set thevariables CFLAGS to CFLAGS=-Wall.

The resulting Makefile for the project of our example would be changed as follows

1 CFLAGS=-Wall

2 bsp_main: LDLIBS=-lm

3 bsp_main: add.o add.h

4 add.o: add.h

Listing 5.9: Example of Makefile with C-MATH locally: Makefile

5.1.2 Own rules

Sometimes it happens that one needs customised rules, which modify the predefinedimplicit behaviour of make. For example, one often needs as a rule that erases all theobject-files. This rule will be taken here as an example and will be added to the Makefile:

1 CLEAN_LIST=add.o bsp_main

2 clean:

3 −−−−−−−→rm -f $(CLEAN_LIST)

4 .PHONY:clean

Listing 5.10: The Clean Rule: Makefile

Here we define in line 3, which commands make must called, in order to build the target.We have created a new rule for make. Which can be called by

30

Page 31: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5.2 Version Control – Subversion

make clean

Here it is IMPORTANT, that the commands of a rule must always start with thetabulator symbol. These in our example in Listing 5.10 is symbolised by the “→”.

5.1.3 More about Make

Further information on make can be found in the Manual of GNU Make. The documentcan be found at http://www.gnu.org/software/make/manual/html_node/index.html.

5.2 Version Control – Subversion

In this chapter you will learn about the concept of version control. Managing versions ofa program or of other documents is very important. Imagine for example the situationthat you are working for this lab and one of the following events occur.

data loss You program on your private notebook and from any reason, all the data fromyour hard disk get lost and your have to start all over again.

synchronisation You work together with a colleague and you want to share the workload.

team work You work together with your colleague on the exercises, and one little changeby your colleague lets the program fail from one moment to the other.

logging You and your colleague want to understand why you made a specific change inthe past.

branch You work on a program for some hours and suddenly you have an idea how tomake the whole thing easier and better. Now you make the change and nothingworks any more. Where to get the old working version from?

It is easy to pretend that such events are a daily occurrence in large scale softwareprojects. Motivated by this problems software engineering tools were invented whichtarget one ore more of the mentioned problems. The superordinate concept to solvethis problems is “version-control”. The currently most frequently used version controlsystem is Subversion. It will be presented in this chapter. In our presentation we willproceed practically and theoretical discussions will be skipped, where it is possible.

5.2.1 The “Repository”

The “Repository” is a concept that has been developed in the first version control sys-tems. It is a matter of location2 where all versions of the version controlled files arestored. If several persons want to work together on some files they may proceed asfollows:2In the world of computers a file, a directory, or database

31

Page 32: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

1. make a copy of the old file, which is to be changed

2. make the changes in the copy

3. tell all others that the copy is the new version

Practically, this is a simple ( but manual ) version control system.

For not making all the work by hand there are software systems available which doexactly this job. The most important thing to have for a version control system is arepository, which should be located on a secure computer. This computer should beintegrated into a backup process and should be connect-able over the Internet.

A very good location for a repository in this lab is your personal home directory atthe TU. Come to a compromise with your co-worker, where to create such a commonrepository for the lab, if in your home or in the home directory of your colleague is ofsecondary importance, you just have to fix one position for your common repository.

Create a Subversion-Repository

At first you should change to your personal home directory with the command

cd

Now you can create a new directory for placing the repository there. You may choosethe name REPOS, just as we do in this tutorial:

mkdir REPOS

Now you can already initialise the repository with the subversion structure by using thecommand:

svnadmin create REPOS

This command creates an empty repository in the directory REPOS which contains thestructure which subversion needs to provide its services. The content of this directorydoes not need to be investigated; changes by hand are taboo.

5.2.2 The “working copy”

As discussed in the last section, manual changes to the repository are taboo. Now thequestion arises, how to use the repository at all. The answer is simple: you need touse subversion, because this program is responsible to manage data in the repository.Therefore subversion can create a working copy of the repository content; to create aworking copy is often called “to do a checkout”. For creating a working copy you needagain a directory where the copy shall be stored. Therefore you can create sub-directoryin your home directory again. Let us call it WORK:

mkdir WORK

Now you should checkout a working copy by typing the command:

32

Page 33: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5.2 Version Control – Subversion

svn checkout file://$PWD/REPOS WORK

Here you use for the first time the user interface to subversion — the program svn.The first parameter checkout is a command, that is interpreted by svn. The firstparameter of the checkout-command is always a URL (Uniform Resource Locator), asyou might know them from the Internet (e.g. http://...). Here you do not want toaccess a hypertext (as with http URLs) in the Internet, but a directory which is storedlocally on the same machine on which you are working. Therefore you have to use theURL-scheme file. The Environment-variable $PWD, which has been used in the last callcontains the absolute path of the current working directory. The last parameter WORK isthe relative path to the directory where subversion shall create the working copy. Theanswer which subversion should give after the last call3 is:

Checked out revision 0.

Now you have a working copy of the current version in the repository in the directoryWORK.

5.2.3 Working on a Working Copy

Change at first into the directory of the working copy you have just created:

cd WORK

If you let the system list the content of the directory you will realise, that the directoryis empty. If you think about it, it is no wonder, since you did not put yet anything intothe repository. This we will change now:

svn mkdir homework

This command instructs subversion to create a sub-directory and to mark it to te bepart of the next version.

The last call is equivalent to the following two:

mkdir homework

svn add homework

The difference is obvious: the first call used svn to create the directory homework andsvn marked the directory automatically and the second variant created the directorywithout using subversion and then marked the directory explicitly with the subversioncommand add. Both calls are functional equivalent and produce the same result.

Creating Versions

It is important to understand that not every change in the working copy changes theversion in the repository. Subversion is a version management system, therefore you

3If the language of the command line is put to “C” — see chapter 2.1.3).

33

Page 34: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5 Tools in Software Engineering

have to tell it explicitly, when you exact a state of the files to be a new version. This isto be done with the commit command of subversion.

We have just created the directory homework and we have marked it to enter the newversion. If you would like to apply the changes and make the repository aware of thenew version, you shall type:

svn commit -m ‘‘Directory homework created‘‘

which commits the current state of the working copy to be the new version. The text‘‘Directory homework created‘‘ after the commit parameter -m is the message whichfinds its way to the logbook of the repository. The log message shall contain enoughinformation to explain yourself or other members in the team; what changes you madewith this new version.

Subversion answers to this call by printing:

Adding homework

Committed revision 1.

The repository contains now the new version.

Subversion Commands

You can get a list of available commands which svn supports with the svn commandhelp:

svn help

There you can also find the commands you have already used, but you can also see othercommands which you already know from the UNIX command line (e.g. mkdir, ls, rm,cp).

There are also commands, which may be new to you:

add mark directory or file for inclusion into the version control

remove unmark directory or file for inclusion into the version control

diff shows the difference between a local file an the respective file in the repository

status shows the state of the files and directories of the working copy

commit sends the current state into the repository to define a new version

update updates the working copy to the state in the repository

5.2.4 A Repository with multiple users

Version control gets more interesting if more than one people work together which havetheir own working copy each. Then there are potentially multiple concurrent versionsof the common files. We consider the example that you want to work on the exercisesat in the courses and home on your private computer. Therefore you have to create asecond working copy on your private machine.

34

Page 35: Introductory Text for Intermediate Programming · Introductory Text for Intermediate Programming Andreas Keese a.keese@tu-bs.de Dominik Jurgens d.juergens@tu-bs.de Institut fur Wissenschaftliches

5.2 Version Control – Subversion

Working copy from the Internet

Discover the Internet address of your repository In the last sections you createda subversion repository in your home directory at the Gauß-IT-Centre. To access thisrepository from your computer at home you have to remember the location of the repos-itory in the TU file system. The path of your home directory you get by typing

echo $HOME

The command

echo svn+ssh://rzlx0005.rz.tu-bs.de/$HOME/REPOS

now gives you the URL of your repository. This URL you can use as parameter for thesvn command checkout (as shown in section 5.2.2) from everywhere of the world. Theaccess to the repository is saved by your personal password.

Working with the AFS and subversion at the TU

Your Repository is saved on storage in the TU file system. If you want to providesomeone else access to the created repository (someone as your college in this course),you have to allow his user-name the access to the repository. This you can do with:

find REPOS -type d -exec fs setacl USER rlwikd \;This maybe looks look a little bit complicated, but it adds the user name USER tothe access control list of all sub-directories of the repository with the name REPOS. Forchanging recursively the access control list of every sub-directory the find command isused which executes the command after its parameter -exec for all entities find found.The command fs setacl meaning ”file-system set access control list“ adds the userwith the user-name USER to all entities ”()“ find discovers (that are all directories —therefore the -d) the right rlwikd. Rights management TUs file-system are explainedon http://www.openafs.org/pages/doc/AdminReference/auarf157.htm.

After this command, your colleague has access to the repository.

5.2.5 Multiple Working Copies, Conflicts and their resolution

When you use SVN extensively you will reach the point that two different version ofthe same file are concurrently in existence. The first commit defining the new versionwill complete without error, but a second commit of the other file will create a conflict,because there are two different versions trying to become the new one. In this situationsubversion can nor automatically decide, what you want it to do. Therefore it printsout an error and asks you to resolve the conflict. For supporting you in the resolution,Subversion creates files with each of the versions which are competing. To resolve thesuch errors is out of the scope of this text. In the context of this course it is very unlikelyto have such problems. The resolution is not complicated an described e.g. here:

http://svnbook.red-bean.com/en/1.0/ch03s05.html#svn-ch-3-sect-4.4

35