0

Since the *.o, *.h file and *.c files are stored in different directories, do I need add prefix $(ODIR) or $(IDIR) everytime I write a *.o or *.h file?

Is there a graceful way to do it?

Perhaps some way like $(IDIR)/{a.h, b.h, c.h}?

This is the sample of makefile:

GCC = gcc
CFLAGS = 
CFLAGS_DEBUG_STRICT = -Wall pedantic -ansi -ggdb -DDEBUG 
CFLAGS_DEBUG = -Wall -ggdb -DDEBUG
LFLAGS = 
SDIR = ../src
ODIR = obj
IDIR = ../include
INCLUDES = 
LIDR = ../lib/
LIBS = 
all : keyword_match_test
keyword_match_test : $(ODIR)/keyword_match_test.o $(ODIR)/keyword_match.o 
  $(GCC) $(CFLAGS_DEBUG) -o $@ $+ 

$(ODIR)/keyword_match_test.o : keyword_match_test.c $(IDIR)/keyword_match.h
  $(GCC) $(CFLAGS_DEBUG) -c -o $@ $< -I$(IDIR) 

$(ODIR)/keyword_match.o : $(SDIR)/keyword_match.c $(IDIR)/keyword_match.h  $(IDIR)/global.h
  $(GCC) $(CFLAGS_DEBUG) -c -o $@ $< -I/usr/include/mysql -I$(IDIR)
thiton
  • 35,651
  • 4
  • 70
  • 100
louxiu
  • 2,830
  • 3
  • 28
  • 42

2 Answers2

1

In addition to @macs suggestion with explicitely placed object files, GNU make's vpath directive can help you:

vpath %.h $(IDIR)
vpath %.o $(ODIR)

$(ODIR)/keyword_match_test.o : keyword_match_test.c keyword_match.h
thiton
  • 35,651
  • 4
  • 70
  • 100
0

You can do something like this:

C_SRC = foo.c bar.c
C_OBJ = $(C_SRC:%.c=../obj/%.o)
INCLUDES = $(wildcard *.hpp) $(wildcard *.h) $(wildcard ../../general/*.hpp)

$(C_OBJ): $(C_SRC)
          $(C) $(CFLAGS) -o $(C_OBJ) -c $(C_SRC)

Note the ../obj/ at C_OBJ. So you're giving all source files to the Makefile and it automatically replaces the extensions *.c with *.o and the required directory. You can do the same for includes, or use a wildcard as shown.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
  • Your rule makes each object file depend on all source files, and the command in the recipe will fail, because the `-o` flag takes only one argument, so the compiler will try to interpret all following object files as source files. – eriktous Dec 19 '11 at 14:44
  • Ok, good to know ... never realized that. This wasn't the case for this particular Makefile, so i never saw this error. Thanks! – Sebastian Dec 19 '11 at 14:56