I have source in a bunch of subdirectories like:
src/widgets/apple.cpp src/widgets/knob.cpp src/tests/blend.cpp src/ui/flash.cpp
In the root of the project I want to generate a single Makefile using a rule like:
%.o: %.cpp $(CC) -c $< build/test.exe: build/widgets/apple.o build/widgets/knob.o build/tests/blend.o src/ui/flash.o $(LD) build/widgets/apple.o .... build/ui/flash.o -o build/test.exe
When I try this it does not find a rule for build/widgets/apple.o. Can I change something so that the %.o: %.cpp is used when it needs to make build/widgets/apple.o ?
The reason is that your rule
expects the .cpp file to reside in the same directory as the .o your building. Since test.exe in your case depends on build/widgets/apple.o (etc), make is expecting apple.cpp to be build/widgets/apple.cpp.
You can use VPATH to resolve this:
When attempting to build ‘build/widgets/apple.o’, make will search for apple.cpp in VPATH. Note that the build rule has to use special variables in order to access the actual filename make finds:
Where ‘$<‘ expands to the path where make located the first dependency.
Also note that this will build all the .o files in build/widgets. If you want to build the binaries in different directories, you can do something like
I would recommend that you use ‘canned command sequences‘ in order to avoid repeating the actual compiler build rule:
You can then have multiple rules like this: