Using any editor, create a source file with a name of the form
something.c
Use the cc
command to compile, assemble, and link you program:
cc something.c
The resultant executable file will be called a.out
. You can
either run it as it is:
a.out
or you can first rename it to something more meaningful (which is preferable):
mv a.out something
something
The executable can also be given the desired name during linking:
cc -o something something.c
something
The same as for C, except use CC
rather than cc
for the
compiler.
Many Unix systems now have C/C++ compilers developed by the Free Software Foundation's GNU project (GNU stands for GNU's Not Unix). Many users prefer the GNU C compiler to the standard compiler that comes with the Unix distribution, and often GNU C++ is the only version of C++ available.
Using any editor, create a source file with a name of the form
something.c
(C) or something.cc
(C++)
Use the gcc
or g++
command to compile, assemble, and link
your program:
gcc -o something something.c
or
g++ -o something something.cc
The above discussion has assumed your entire program resides in a single source file. If this is not the case, you have several options:
cc -o foo foo.c bar.c baz.c
This will compile the three source files foo.c
,
bar.c
, and baz.c
into object code and will then
link the resultant object code into a single executable foo
.
-c
option on the compiler command line), and then
link them together.
Example:
cc -c foo.c
cc -c bar.c
cc -c baz.c
cc -o foo foo.o bar.o baz.o
The first three commands produce object files foo.o
,
bar.o
, and baz.o
. The latter links them into one
foo
file (but also leaves the object files available for
later use.)
bar.c
into
bar.o
and baz.c
into baz.o
, but need to
compile foo.c
(perhaps because you have edited it recently).
You could use the following command:
cc -o foo foo.c bar.o baz.o
Long before Borland and Microsoft started developing integrated development
environments for program development, Unix was using a utility called
make
to manage the task of building programs consisting of multiple
source files and/or libraries. The make program uses a specification file,
usually named Makefile
or makefile
, that describes the
dependencies between the source files, object files and executables.
The entries in the makefile consist of a line indicating a dependency relationship followed by one or more lines, each of which begins a TAB character, with commands to build the dependent file.
A makefile for the example above might like look like
foo: foo.o bar.o baz.o
cc -o foo foo.o bar.o baz.o
foo.o: foo.c
cc -c foo.c
bar.o: bar.c
cc -c bar.c
baz.o: baz.c
cc -c baz.c
Typing make
at the prompt will cause the file foo
to be
created.
Make knows how to build C and C++ source files, so the only lines really
necessary are the first two -- make will automatically realize that
to get foo.o
the file foo.c
must be complied with the
C compiler.
Now if the file foo.c
has just been edited and you type make
,
only foo.c
will be recompiled before the executable is relinked.
Read the manual page for make
for more information.