user3466773 user3466773 - 1 year ago 75
C Question

Compile and link a source file containing object files to another data structure

This is my current Makefile:

OBJS=rlmain.o rlist.o queue.o

.PHONY: all clean

all: rlmain

queue.o: $(INCLUDE)queue.c $(INCLUDE)queue.h
$(CC) $(CFLAGS) -c $(INCLUDE)queue.c -o queue.o

rlist.o: $(INCLUDE)rlist.c $(INCLUDE)rlist.h
$(CC) $(CFLAGS) -c $(INCLUDE)rlist.c -o rlist.o

rlmain: main.c rlist.o queue.o
$(CC) $(CFLAGS) main.c rlist.o queue.o -o rlmain

rm -f *.o rlmain

Makefile is in the top directory. In the lower directory called "Structures" is a collection of files for my data structures. My data structures implement a ready list - a linked list of queues.

I managed to get queue.o working using '-c' but I cannot get rlist.o to make because rlist.c does not contain a main method, and it must link to the queue.o functions. I'm using the command

Everything else working except the command. I want to write a command to tell the compiler to link the queue.o functions to the rlist.c compilation but not compile the functions that require main(). What do I do in order to make that happen?

Answer Source
Note: `<tab>` needs to be replaced with a tab character
Note: use `:=` when defining macros, so they are only evaluated once

# select the specific external executables to run
CC := /usr/bin/gcc
RM := /usr/bin/rm

INCLUDE := ./Structures/
CFLAGS  := -c -Wall -Wextra -pedantic -Wconversion -std=gnu99 
OBJS    := main.o rlist.o queue.o

.PHONY: all clean

all: rlmain

queue.o: $(INCLUDE)queue.c $(INCS)
<tab>$(CC) $(CFLAGS) $< -o $@ -I$(INCLUDE)

rlist.o: $(INCLUDE)rlist.c $(INCS)
<tab>$(CC) $(CFLAGS) $< -o $@ -I$(INCLUDE)

main.o: $(INCLUDE)main.c $(INCS)
<tab>$(CC) $(CFLAGS) $< -o $@ -I$(INCLUDE)

rlmain: $(OBJS) 
<tab>$(CC) $(OBJS) $(LFLAGS)

<tab>$(RM) -f *.o rlmain
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download