This is an introductory guide to UNIX for Macquarie University computing students, written by Mary Gardiner in 2004. It is no longer being updated but is available for modification and republication under the Creative Commons Attribution licence.

UNIX Guide · Compiling C and C++

UNIX: Compiling C and C++

Compiling C

The cc command compiles C code.

If your code was in a file called hello.c, you would compile it with the command cc hello.c.

By default, the compiled file is called a.out and you could run your program by typing ./a.out.

If you want to put your output in a file with another name, say my_program, use the -o flag to cc, like this: cc -o my_program hello.c. Then you could run your program with the command ./my_program.

The C compiler has a number of useful warnings that can help you find bugs in your code. In order to use the C compile with the warnings turned on, use the -Wall and -pedantic options, like this: cc -Wall -pedantic -o my_program hello.c.

Compiling C++

The c++ command compiles C++ code.

If your code was in a file called hello.cc, you would compile it with the command c++ hello.cc.

By default, the compiled file is called a.out and you could run your program by typing ./a.out.

If you want to put your output in a file with another name, say my_program, use the -o flag to c++, like this: c++ -o my_program hello.cc. Then you could run your program with the command ./my_program.

The C++ compiler has a number of useful warnings that can help you find bugs in your code. In order to use the C++ compile with the warnings turned on, use the -Wall and -pedantic options, like this: c++ -Wall -pedantic -o my_program hello.cc.