OpSys Fall 2006 - HW1 FAQ

Click on a question to expand it (for the details).
Click on the question title again to hide the details.

+ Using Make

Question:

How do I use the Unix make command?


Answer:

First you need to create a file named Makefile and place it in the same directory as your code. When you run the make command it reads this file (by default - you can tell it to read other files if you want) and process the rules listed in the file.

A Makefile can contain lots of things including definitions, generic rules and specific rules. For now I'll only discuss using specific rules, check the man page for make (or check out the GNU Make manual) for a complete description of all the possibilities...

A rule in a Makefile includes three basic elements:

  • A target file that you want built.
  • A list of files on which the target depends (called the dependencies). If any of these files change, make will know it need to rebuild the target.
  • A command (or list of commands) that are used to build the target

The syntax for this is shown below:

target: dependencies ...
        commands
        ...

IMPORTANT!: The command line(s) must start with a tab character!

Here is a rather simple Makefile that will build an executable named "hw1" from a C program named "hw1.c" using gcc:

hw1: hw1.c
        gcc -Wall -o hw1 hw1.c

Here is another Makefile, this one will build an executable named "hw1" from two C files: "hw1main.c" and "hw1subs.c", and will include the readline and curses libraries (including paths to the includes and library as is needed on CS FreeBSD machines).

hw1: hw1main.c hw1subs.c
        gcc -Wall -o hw1 -I/usr/local/include -L/usr/local/lib 	hw1main.c hw1subs.x -lreadline -lcurses

+ What is -Wall ?

Question:

What is -Wall that you always seem to include as an option to gcc? For example,

gcc -Wall -o hw1 hw1.c

Answer:

-Wall tells gcc to "show all warnings". This is generally a good idea, as the compiler might find unusual stuff in your code that you should know about. There are all sorts of warnings you can turn on/off with options to the compiler - check the man page for gcc for the gory details...

+ Using code found in the homework writeup

Question:

Can we use the code provided in the homework writeup as a starting point? Specifically, can we use the code from testregex.c including the function get_match() ?


Answer:

Yes, feel free to use any of the code you find helpful. It is assumed that you use the code in testregex.c as a starting point for using the regular expression library.

NOTE: For this and all other assignments, if you use sample code or functions provided by Dave, it is expected that you understand the code, and know how and what it does. If there are bugs in Dave's code (or you use it in the wrong way) - it's your fault. In other words, don't blame Dave if you borrow his code and it doesn't do what you want (you should make sure it does what you want before using it!).

+ Problems with the setenv man page

Question:

I type man setenv and I don't get a man page that seems to have anything to do with the setenv function. How can I find out how to use setenv?


Answer:

There is a command named setenv and a function. To tell the man program that you want to see information about the function, try this:

man 3 setenv

The 3 tells man to look in section 3. Section 1 are user commands, section 2 are system calls, section 3 are library functions (there are more sections as well...).

+ Compiler Issues (like syntax error before 'int')

Question:

I keep getting a compiler error - it doesn't seem to like my variable declarations. What's up?


Answer:

Traditional C does not allow you to declare any variables in a function after you have some lines of code (non-declarations). So something like this won't work:

void blah(){

  int i;
  for (i=0;i<100;i++) {
    printf("i is %d\n",i);
  }

  int j;
  for (j=0;j<100;j++) {
    printf("j is %d\n",j);
  }
}

You would need to declare j before the first loop:

void blah(){

  int i;
  int j;
  for (i=0;i<100;i++) {
    printf("i is %d\n",i);
  }

  for (j=0;j<100;j++) {
    printf("j is %d\n",j);
  }
}

Note that the C99 standard does allow declaring variable in the middle of a function. On the CS freebsd machines you can use gcc3 instead of gcc, it supports the C99 standard. You can get the details of C99 support in gcc3 here: http://gcc.gnu.org/c99status.html

+ How do I create an environment variables before running the program?

Question:

The assignment indicates that we need to be able to access existing environment variables, like those shown when we type "set" into BASH. Can we create some of our own?


Answer:

You can create your own environment variables (tell the shell to add a new environment variable before you run your program):

bash> export JOE=FRED
bash> ./hw1
hw1prompt> print JOE
JOE = FRED