[cs631apue] Compiling multiple .c files

Jan Schaumann jschauma at stevens.edu
Thu Sep 22 23:21:41 EDT 2016


mseaton <mseaton at stevens.edu> wrote:

> My understanding is, we should be able to compile source files 
> individually without a main method

Correct.  Compiling multiple source files into object files works fine;
the error you're seeing happens at the linking stage.  If you just run

cc file.c

then the compiler will perform a number of steps, including the attempt
to create an executable, for which it needs the 'main' function.

Use

cc -c file1.c
cc -c file2.c
cc -c main.c
cc file1.o file2.o main.o

to compile multiple source files into separate object files and then
link them together.  (Here 'main.c' might contain the 'main' function,
but 'file1.c' and 'file2.c' do not.)

We will cover this in more detail in a future lecture, but in the mean
time, a search engine of your preference will also be able to guide you
to a better understanding.

For example:
https://www.google.com/#q=compiling+multiple+.c+files

-Jan


More information about the cs631apue mailing list