How can Makefile use separate directories for source code and binaries?

advertisements

I want to be able to split up my bin and my code files into separate directories as it is becoming hard to manage in it's current state.

I ideally would like to have

project_dir
|-Makefile
|-run_tests.sh
|
|__source
|  |-program1.cpp
|  |-program2.cpp
|
|__bin
   |-program1
   |-program2

However I am unable to get this to work with my current system without having to manually write out the rules for every program (bear in mind that every program is a separate program, not a series of objects linked together)

#Current make system
BIN=./bin/
SOURCE=./source/

LIST=program1 program2...

all: $(LIST)

%:  $(SOURCE)%.cpp
    $(CC) $(INC) $< $(CFLAGS) -o $(BIN)[email protected] $(LIBS)

this works except it I it can't see the target in the current path so it think it always rebuilds the binaries even if the source files haven't changed.

My only thought at the moment is to write a program to make a makefile but I don't want to do that.


You were almost there ...

#Current make system
BIN=./bin/
SOURCE=./source/

LIST=$(BIN)/program1 $(BIN)/program2...

all: $(LIST)

$(BIN)/%:  $(SOURCE)%.cpp
    $(CC) $(INC) $< $(CFLAGS) -o [email protected] $(LIBS)

You can also make the LIST easier by using the following

PROG=program1 program2
LIST=$(addprefix $(BIN)/, $(PROG))