Build and install a small program

Synopsis

Build a command named count-observations to count sightings in the observations file. Each nonempty line counts as one sighting. The build requires a C compiler, the C library development files, and make. Installation takes place under the ordinary account’s home directory.

Check the tools and prepare the source

Check whether the shell can locate the compiler driver and the build tool.

Shell input
command -v cc
command -v make

Each command should print a name or path. If either is missing, arrange for the development tools to be installed before proceeding. On OpenBSD, the comp installation set includes the compiler collection, headers, and libraries. On Debian GNU/Linux 13, the build-essential package depends on the compiler, C development headers, and make. A missing stdio.h during compilation also indicates that the development environment needs attention. OpenBSD installation sets · Debian build-essential

Create and enter a build directory. Keep the observations file in the practice directory immediately above it.

Shell input
mkdir -p "$HOME/unix-practice/build"
cd "$HOME/unix-practice/build"
pwd
ls ../observations.txt

Check for a path ending in /unix-practice/build and a listing for the input file. If the input is absent, return to the three-line example to create it.

Save count-observations.c in the build directory. Alternatively, open vi count-observations.c, enter the following source on a new file, and save it.

File contents · count-observations.c

#include <limits.h>
#include <stdio.h>

int main(void)
{
    unsigned long count = 0;
    int in_record = 0;
    int ch;

    while ((ch = getchar()) != EOF) {
        if (ch == '\n') {
            in_record = 0;
        } else if (!in_record) {
            if (count == ULONG_MAX) {
                fputs("Too many records\n", stderr);
                return 1;
            }
            ++count;
            in_record = 1;
        }
    }
    if (ferror(stdin)) {
        perror("Reading observations");
        return 1;
    }
    if (printf("%lu\n", count) < 0 || fflush(stdout) == EOF) {
        perror("Writing count");
        return 1;
    }
    return 0;
}

The program reads standard input one character at a time. The first character on each nonempty line increments the count. A newline resets the indicator for the next record. Blank lines contribute zero, and a nonempty final line counts even without a final newline. A line containing a space is nonempty. The program reports input and output failures on standard error.

Compile and run

The following command creates or replaces the executable count-observations in the build directory. Check the current directory with pwd before compiling.

Shell input
cc -o count-observations count-observations.c

A successful build normally prints nothing. The compiler driver translates the C source and links the resulting code with the library functions the program calls. Run the new executable on the observations file.

Shell input
./count-observations < ../observations.txt

Expected output

3

The slash in ./count-observations tells the shell to run the program from the current directory. The input redirection opens the observations file for the program. Test the special case of an empty line between two sightings.

Shell input
printf '%s\n' robin '' sparrow | ./count-observations

Expect 2. The empty quoted argument produces the blank line.

Rebuild with make

Save the supplied Makefile beside the C source. For manual entry, open vi Makefile and enter the following contents. Start the indented line with a literal tab. In vi insert mode, press Ctrl-V and then Tab to insert the tab character.

File contents · Makefile

CC = cc

count-observations: count-observations.c
	$(CC) $(CFLAGS) -o count-observations count-observations.c

The name before the colon is the target. The source filename after it is a prerequisite. The indented line supplies the command to rebuild the target. The variable CC names the compiler, and CFLAGS carries optional compiler flags.

Mark the source as recently modified and run the build.

Shell input
touch count-observations.c
make

Expect the compiler command to appear when the source is newer than the executable. Run make a second time. With the target up to date, the compiler should remain idle. If make rejects the rule, check the spelling and capitalization of Makefile and the leading tab. OpenBSD make(1) · Debian make(1)

Install and verify the personal command

Create a private installation directory under the account’s home directory and inspect it.

Shell input
mkdir -p "$HOME/bin"
ls -l "$HOME/bin"

If an unrelated executable named count-observations already exists there, choose another installation name for the remaining commands. Copy the program. The -i option asks for confirmation before replacing an existing file.

Shell input
cp -i ./count-observations "$HOME/bin/count-observations"

Change out of the build directory and run the installed copy by its full path.

Shell input
cd "$HOME"
"$HOME/bin/count-observations" < "$HOME/unix-practice/observations.txt"

A result of 3 confirms that the installed copy can run independently of the build directory. To invoke the command by name in the current shell, add the personal directory to the front of the shell’s command search path.

Shell input
PATH="$HOME/bin:$PATH"
export PATH
command -v count-observations

Check that the printed path ends in /bin/count-observations under the account’s home directory. Run the command by name.

Shell input
count-observations < "$HOME/unix-practice/observations.txt"

The result should again be 3. The search-path change belongs to this shell session. At a fresh login, the full path remains available.

UNIX is a registered trademark of The Open Group.