CE 155 - Compiling and Running C programs (Home)

The programs posted here are written in C and can be compiled in Linux using gcc (GNU C Compiler). Most of the examples are simple and can be compiled using the following command

gcc -o progname progname.c
where progname.c is the filename of the source code and progname is the name of the resulting compiled output file. Without the "-o progname" option, the default output file name is a.out.

When you play around with the examples, and compile your own version, it is suggested that you add the "-Wall" and "-Werror" switches to the compile command. The resulting compile command will be

gcc -o progname progname.c -Wall -Werror
The "-Wall" switch turns on all warnings while the "-Werror" switch turns all warnings into errors. This will make the compilation process halt on all warnings. This will help guide you in writing correct C code.

To run your program, simple type

./progname

For programs which use special libraries (math, pthreads), the library used should be part as an argument to gcc. The "-lm" argument is used for math.h and "-lpthread" for those that use pthread.h.