Makefiles: Get .cpp from one directory and put the compiled .o in another directory


I'm working on a cross-platform 2D engine for mobile devices (Windows Mobile 6 and Android). My Windows version is pretty much ready, but I still need to make sure the same functionality is available on Android.

What I want is one Makefile in the root of the project and several Makefile's for the project itself and the test applications.


I'm basing my attempts on the following Makefile:

 include ../makeinclude PROGS = test1 SOURCES = $(wildcard *.cpp) # first compile main.o and start.o, then compile the rest OBJECTS = main.o start.o $(SOURCES:.cpp=.o) all: $(PROGS) clean:    rm -f *.o src test1: $(OBJECTS)    $(LD) --entry=_start --dynamic-linker system/bin/linker -nostdlib -rpath system/lib -rpath $(LIBS) -L $(LIBS) -lm -lc -lui -lGLESv1_CM $^ -o ../[email protected]     acpy ../$(PROGS) .cpp.o:    $(CC) $(CFLAGS) -I $(GLES_INCLUDES) -c $*.cpp $(CLIBS)

However, I'm not very good with these things. What I want is for it to take the .cpp's that are in the src folder, compile them to .o and put them in the intermediate folder and, finally, compile the .o's to the compiled exe and put it in the bin folder.

I've managed to get clean to work like this:

cd intermediate && rm -f *.o

However, I can't get it to retrieve the .cpp's, compile them and put them in the intermediate folder.

I've looked at several other Makefiles, but none do the things I want to do.

Any help is appreciated.

Best Solution

There's more than one way to do this, but the simplest is to run in TestOne, making Intermediate/foo.o out of Src/foo.cpp and test1 out of Intermediate/foo.o, like this:

# This makefile resides in TestOne, and should be run from there.include makeinclude # Adjust the path to makeinclude, if need be.PROG = bin/test1 SOURCES = $(wildcard Src/*.cpp) # Since main.cpp and start.cpp should be in Src/ with the rest of# the source code, there's no need to single them outOBJECTS = $(patsubst Src/%.cpp,Intermediate/%.o,$(SOURCES))all: $(PROG)clean:     rm -f Intermediate/*.o bin/*$(PROG): $(OBJECTS)     $(LD) $(BLAH_BLAH_BLAH) $^ -o ../[email protected]  $(OBJECTS): Intermediate/%.o : Src/%.cpp    $(CC) $(CFLAGS) -I $(GLES_INCLUDES) -c $< $(CLIBS) -o [email protected]