Reputation: 1
So here is a very simple Makefile. I can't seem to specify the final location of the targets... I want the message.o and main.o to be located in the build directory. However, when I try to use the following code they end up in the Makefile directory. When the compiler tries to create the final executable it spits out an error because it is looking for the .o files in the ./build/ directory. I'm pretty sure there is some flaw in my understanding of makefiles, but other examples in the net shows similar solutions. Thanks in advance.
SHELL := /bin/bash
SOURCE = ./src/
BUILD = ./build/
output: $(BUILD)main.o $(BUILD)message.o
gcc $^ -o output
$(BUILD)main.o: $(SOURCE)main.c
gcc -c $^
$(BUILD)message.o: $(SOURCE)message.c
gcc -c $^
Upvotes: 0
Views: 2002
Reputation: 3076
You need to speficy the output path of the object files or else your files main.o
and message.o
will not be generated in the build
folder but in the current directory.
Please see below example :
SHELL := /bin/bash
SOURCE = ./src/
BUILD = ./build/
output: $(BUILD)main.o $(BUILD)message.o
gcc $^ -o output
$(BUILD)main.o: $(SOURCE)main.c
gcc -c $^ -o $@
$(BUILD)message.o: $(SOURCE)message.c
gcc -c $^ -o $@
Upvotes: 1