Question

I want to compile my source files twice with different flags each time. Besides that I need to have these executables which I'll acquire after the compilation in different directories (so I want 'make' to create two folders and put into every folder an executable).

I think that them main problem is that I don't know how to main object files. Think that we can create them with different names (because every set of .o files should somehow differ from the another which has different flags) or put them in the directories where we want to have executables.

Still I have no idea how to do it in elegant way :/

Any help greatly appreciated :)

Was it helpful?

Solution

You haven't given us many details, so I'll suppose you have two source files, foo.c and bar.c, and you're building in two directories, red/ and blue/. Here is a crude makefile that will do the job:

OBJS := foo.o bar.o
RED_OBJS := $(addprefix red/,$(OBJS))
BLUE_OBJS := $(addprefix blue/,$(OBJS))

$(RED_OBJS): red/%.o : %.c
    $(CC) -c $< -o $@

$(BLUE_OBJS): blue/%.o : %.c
    $(CC) -c $< -o $@

red/red_exec: $(RED_OBJS)
    $(CC) $< -o $@

blue/blue_exec: $(BLUE_OBJS)
    $(CC) $< -o $@
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top