1

Whenever I am trying to build something like this in my Makefile -

gcc -o main.o -IStarterWare_Files -c main.c StarterWare_Files/test.h StarterWare_Files/add.h

It throws me error that gcc: cannot specify -o with -c or -S with multiple files. Basically I want my makefile to build the target again if I change for example some macro in one of my header files. My current Makefile is -

EXE    = nextgenrsm
CC     = gcc
LIBS   = StarterWare_Files/
CPPFLAGS = _IStarterWare_Files/


MAIN_OBS = $(patsubst %.c,%.o,$(wildcard *.c))
LIB_OBS  = $(patsubst %.c,%.o,$(wildcard StarterWare_Files/*.c))

all: $(EXE)

$(EXE): $(MAIN_OBS) $(LIB_OBS)
    $(CC) -o $@ $(LDFLAGS) $(MAIN_OBS) $(LIB_OBS) $(LDLIBS)

%.o: %.c 
    $(CC) -o $@ -MD -MP $(CPPFLAGS) $(CFLAGS) -c $^

ALL_DEPS = $(patsubst %.o,%.d,$(MAIN_OBS), $(LIB_OBS))
-include $(ALL_DEPS)

clean:
    rm -f $(LIB_OBS) $(EXE) $(MAIN_OBS) $(ALL_DEPS)

.PHONY: all clean

I can't figure out what changes to make to build my executable again if one of the header files is modified. I don't want to do make clean and make again.

2
  • 2
    Why are you compiling header files? I think you want to change $^ to just $< so that your %.o: %.c rule only adds the first prerequisite (the source file) to the command, not all its header dependencies as well. Commented Jun 30, 2015 at 15:22
  • This is a follow-on from stackoverflow.com/q/31081131/258523 . Commented Jun 30, 2015 at 15:25

1 Answer 1

0

The way the automake system handles this is to not use %.o: %.c but instead list the C file and all of the headers in the C file.

So for example:

main.o: main.c StarterWare_Files/test.h StarterWare_Files/add.h
    $(CC) -o $@ -MD -MP $(CPPFLAGS) $(CFLAGS) -c $^

See makedepends for a tool that will read C files and figure out the make dependencies.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.