title: Makefile, Compiling and Linking
body: <p>I have a question regarding compiling and linking in Makefile (and perhaps in general).</p>
  
  <p>I have a server.c file which consists of the main program which has a <code>main()</code> function. <code>server.c</code> includes rio.c. I have a module called <code>rio</code> which consists of <code>rio.c</code> and <code>rio.h</code>. It has no <code>main()</code> function.</p>
  
  <p>I have two questions, how to actually write the Makefile, and the best practice for doing such a thing.</p>
  
  <p><strong>Q1: How to write the Makefile</strong></p>
  
  <p>I have the following Makefile:</p>
  
  <pre><code>CC = gcc
  CFLAGS = -Wall -Werror -Wmissing-prototypes
  OBJS = server.o rio.o
  
  all: $(OBJS)
      $(CC) $(CFLAGS) $(OBJS) -o sysstatd
  
  server.o: server.c
      $(CC) $(CFLAGS) -c server.c
  
  rio.o: rio.c rio.h
      $(CC) $(CFLAGS) -c rio.c
  
  clean:
      rm -f *~ *.o sysstatd
  </code></pre>
  
  <p>I am having linking issues with this. It says that I have multiple definitions of all the functions used in C. I'm not sure how this is possible since server.c is compiled with the <code>-c</code> flag so nothing is actually linked. It should know that some functions exist but not actually link them until the <code>all</code> rule compiles both object files together and produces a single object file which has everything linked.</p>
  
  <p>What is the issue here?</p>
  
  <p><strong>Q2: Best practice</strong>
  Since I have a module and then another file which contains the main program, should I compile the main program, <code>server.c</code>, as a separate module and then compile both together in <code>all</code>, or compile server.c in all and add the rio.o module there? Note that this still produces the same linking problem I have above so I'm pretty sure I have my issue lies somewhere else.</p>