Thinking in C and *nix (CSC 282 2015S) : EBoards

CSC282 2015S, Class 04: Compilation, Macros, and the C Preprocessor


Overview

Preliminaries

Admin

Questions

Moving .jpg files

Code by NK.

#!/bin/sh
# first argument is where from, second argument is where too.
for file in $(find $1 | grep .jpg) ; do
  echo "copying $file to $2"
  cp $file $2
done

Observation 1: $1 and $2 refer to command-line arguments.

Question: How do we figure out what find $1 | grep .jpg does?

Conclusion: The command find $1 | grep .jpg finds all the jpg files within a directory.

Question: Might it find thigns other than jpg files. We might have a filename with .jpg in the middle. (or just any character and jpg). use \.jpg$.

Other improvements: find can actually have some parameters that specify what files to find.

find . -name '*.jpg'

Note time cmd tells you how long a command takes

Detour: How do pipes work?

A C program ecosystem

On the design and implementation of larger programs written in C.

Simple model of C programs: Put everything in one file. (Alternate: Put things in separate files.)

Traditionally, a C project includes

Detour: Steps in compilation

The 161 model: .c -> executable

The actual steps:

For our silly program

Back to the Question

When compiling partb, how does the compiler know about parta?

We've just seen that it doesn't have to. Maybe it's smart.

Why do we not get the warning that 'foo' is not declared?

The C compiler trusts the user.

What should we do?

Quick hack:

    extern double foo(double);

Normal strategy: For every .c file, you create a .h file that gives such declarations.

Here's parta.h

    #ifndef __PARTA_H__
    #define __PARTA_H__

    /**
     * parta.h
     *   Amazing mathemtical utils by NK and cmapsdfay.
     *
     * Released under GPL 3.0.  You should have received a
     * license with this software.  If not, got to
     * Richard Stallman's abode and ask for a copy.
     */
    extern double foo(double);

    #endif __PARTA_H__

Including files

Detour: Why we have included files

Macros

CPP cnditionals

Detour: Standard header file format

Command-line flags

Hack: Generic data types