Untitled Document Book Cover

PC-based compiling

Course Design

Compiling Hints

Scanf() information.

Assorted C routines.

Errata

Program files

Useful, free, scientific software.

Links (open in new window):
Java Code (unmaintained).

Will's Homepage

Compiling Hints under Unix

Many thanks to Leonardo Frid (Zoology, UBC) who prepared this table that shows the effect of various combinations of various features on compilation. He used bd.c from Chapter 2.

The first column is the compilation command used to compile.
The second column involves declaring drand48() above the main() routine. The declaration is not made in the code I provide.
The third column is whether void is placed inside the main declaration, i.e., int main(void) {...}.
The fourth column tells what happens with the compilation/executable. Syntax error means it didn't compile, constant 10 is a wrong result, correct means the executable produces the numbers shown in the book.

compile with: declare drand48()? use void? result
cc bd.c -o xbd yes no correct
cc bd.c -o xbd yes yes syntax error
cc bd.c -o xbd no no constant 10
cc bd.c -o xbd no yes syntax error*
cc bd.c -o xbd -lm yes no correct
cc bd.c -o xbd -lm yes yes syntax error
cc bd.c -o xbd -lm no no constant 10
cc bd.c -o xbd -lm no yes syntax error
gcc bd.c -o xbd yes no correct
gcc bd.c -o xbd yes yes correct
gcc bd.c -o xbd no no constant 11
gcc bd.c -o xbd no yes constant 11*
gcc bd.c -o xbd -lm yes no correct
gcc bd.c -o xbd -lm yes yes correct
gcc bd.c -o xbd -lm no no constant 11
gcc bd.c -o xbd -lm no yes constant 11
Leonardo says, "Taking out the drand48() and compiling with the -lm flag gives constant 10's as output. Declaring drand48() but not using the -lm flag works. Using void does not work in either case but it does work with with gcc (the GNU C compiler). The important factor then is using the declaration."

This is the case for Leonardo. I tried the starred ones and they worked fine on my Linux system, I then used the "-ansi" flag (enforcing strict adherence to ANSI) and got constant 10s in both cases. If your compiler has a strict adherence to the ANSI standard you may need to globally declare the random number generator. Before main(), add the line:
double drand48();

I hope this information helps.