Laboratory 1

Laboratory 1 – UNIX Tools and Development Environment

Introduction to the UNIX/Linux environment, development tools, the first C program, the compilation pipeline, and the difference between header files and libraries.

Learning Outcomes

After this laboratory, the student should be able to:

  • log in to the AGH UNIX server,
  • configure the shell environment to use English,
  • configure nano and indent,
  • use basic UNIX commands and manual pages,
  • create, format, compile and run a simple C program,
  • explain the stages of compilation: preprocessing, assembly, object code and linking,
  • explain the difference between a header file and a library,
  • recognize simple examples of undefined behaviour.

Task 1 — Log in to the AGH student server

Use SSH to connect to the AGH UNIX server.

ssh your_login@student.agh.edu.pl
Detailed instructions (if you have never connected before)

Before connecting to the server, make sure that your UNIX account is activated.

Go to the AGH network services panel:

https://panel.agh.edu.pl

After logging in:

  1. Open UNIX accounts
  2. Activate the account on student.agh.edu.pl

Then connect to the server using SSH:

ssh your_login@student.agh.edu.pl

If you connect for the first time, the system may ask you to confirm the host key.

yes

Task 2 — Configure the terminal environment to use English

After logging in, configure the shell so that terminal tools display messages in English.

Step 1 — Open the profile file

LANG=en nano ~/.profile

The prefix LANG=en forces nano to start in English.

Step 2 — Add locale settings

export LC_ALL=C.UTF-8
export LC_CTYPE=C.UTF-8
export LANG=C.UTF-8
export LC_COLLATE=C.UTF-8
export LANGUAGE=en
export LC_MESSAGES=C.UTF-8

Step 3 — Save the file and exit nano

Press:

Ctrl + X

Then press:

Y

and confirm the filename with:

Enter

Step 4 — Log out and reconnect

exit

Step 5 — Verify the configuration

locale

You should see values similar to:

LANG=C.UTF-8
LANGUAGE=en
LC_MESSAGES=C.UTF-8
LC_ALL=C.UTF-8
If nano still opens in another language, you can always force English with:
LANG=en nano filename

Task 3 — Configure development tools

Prepare the editor and formatter used in the course.

Step 1 — Configure nano

nano ~/.nanorc

Add:

set linenumbers
set autoindent
set tabsize 4
set tabstospaces

Step 2 — Configure indent

nano ~/.indent.pro

Add:

-kr
-i4
-l120
-nut
-bad
-bap
option effect
-krK&R style (if (...) {)
-i4indentation = 4 spaces
-l120maximum line length = 120
-nutuse spaces, not tabs
-badblank line after declarations
-bapblank line after function definitions

Task 4 — Manual pages and basic UNIX commands

Learn how to use documentation and navigate the file system.

Step 1 — Learn how to use man

man man
key action
Spacenext page
Enternext line
bprevious page
/textsearch text
nnext search result
qquit

Step 2 — Explore documentation of basic commands

man mkdir
man pwd
man ls
man cat
man tree
help cd

Step 3 — Directory navigation exercise

pwd
cd ~
ls
mkdir I2PL
ls
cd I2PL
pwd
mkdir lab1
ls
cd lab1
pwd
cd
tree
cd I2PL/lab1
ls

Step 4 — Create a simple directory structure

Create the following structure:

I2PL
└── lab1
    └── test

Verify it using:

tree

Task 5 — Create the first C program

Create a simple program that prints text on the screen.

nano hello.c
#include <stdio.h>

int main(void) {
    printf("Hello\n");
    return 0;
}

Task 6 — Format the source code

Use indent to format the program according to the course style.

indent hello.c

Compare the formatted file with the backup copy:

diff hello.c hello.c~

Task 7 — Compile and run the program

Compile the program and execute it from the current directory.

Step 1 — Compile

gcc hello.c -o hello

Step 2 — Run

./hello

Task 8 — Explore the compilation pipeline

Examine the stages performed by the compiler.

Step 1 — Preprocessing

gcc -E hello.c > hello.i
wc -l hello.i

Step 2 — Assembly generation

gcc -S hello.c

Step 3 — Object file

gcc -c hello.c

This creates:

hello.o

Step 4 — Full compilation and linking

gcc hello.c -o hello
command stage output
gcc -Epreprocessing.i
gcc -Sassembly generation.s
gcc -cobject file generation.o
gccfull compilation + linkingexecutable file

Task 9 — Header file vs library

Understand the difference between declarations and implementations.

Step 1 — Locate the header file

whereis stdio.h

Step 2 — Check linked libraries

ldd hello

Step 3 — Find printf in the C library

nm -D /lib/x86_64-linux-gnu/libc.so.6 | grep " printf@"
element role
stdio.hheader file with declarations
libc.so.6library with implementations
printffunction declared in the header and implemented in the library
A header file tells the compiler what exists. A library provides the actual code.

Task 10 — Variables and formatted output

Try formatted output with integers and floating-point values.

#include <stdio.h>

int main(void) {
    int x = 1;
    int y;

    printf("x=%d, y=%d\n", x, y);
    printf("x=%5d, y=%5d\n", x, y);

    float f1 = 12.0f;
    float f2 = 12.3678f;

    printf("f1=%f, f2=%f\n", f1, f2);
    printf("f1=%8.2f, f2=%8.2f\n", f1, f2);

    return 0;
}

Compile and run the program several times. Observe that y may have a different value each time.

Task 11 — Undefined behaviour

See what happens when the format specifier does not match the argument type.

#include <stdio.h>

int main(void) {
    printf("%d\n", 3.14);
    return 0;
}

Step 1 — Compile normally

gcc test2.c -o test2

Step 2 — Run several times

./test2

Step 3 — Compile with warnings

gcc -Wall test2.c -o test2
The format specifier %d expects int, but 3.14 is a double. This is an example of undefined behaviour.

Task 12 — Running programs and PATH

Understand why programs in the current directory are executed with ./program.

Step 1 — Try running without ./

test2

You should see:

command not found

Step 2 — Run correctly

./test2

Step 3 — Check PATH

echo $PATH
In Linux the current directory is usually not included in PATH. That is why programs in the current directory are run with ./program.

What will be checked during the next laboratory

The student should be able to:

  • log in to the server,
  • configure the shell environment and development tools,
  • use manual pages and basic UNIX commands,
  • create, format, compile and run a simple C program,
  • explain the meaning of gcc -E, gcc -S, gcc -c and gcc,
  • explain the difference between a header file and a library,
  • recognize simple examples of undefined behaviour.