Suppose I have a directory structure such as:
./Header
./Srcs
./makefile
The contents of the folder ./Header/are two header files:
header1.h
#ifndef HEADER1_H
#define HEADER1_H
#include <stdio.h>
void func1();
#endif
header2.h
#ifndef HEADER2_H
#define HEADER2_H
#include <stdio.h>
void func2();
#endif
In ./Srcs/I have the following srcs:
src1.c
#include <header1.h>
void func1() {
printf("func1()\n");
}
src2.c
#include <header2.h>
void func2() {
printf("func2()\n");
}
main.c
#include <header1.h>
#include <header2.h>
int main(int argc, char** argv) {
func1();
func2();
return 0;
}
Finally, the makefile looks like this:
CC=gcc
INCLUDE=-I./Header/
SRC_DIR=./Srcs/
SRC_LIST=$(addprefix $(SRC_DIR), main.c src1.c src2.c)
OBJ_LIST=$(addsuffix .o, $(basename $(SRC_LIST)))
OUTPUT=test
%.o : %.c
$(CC) -c $< -o $@ $(INCLUDE)
all : $(OBJ_LIST)
$(CC) -o $(OUTPUT) $(OBJ_LIST)
clean : $(OUTPUT) $(OBJ_LIST)
rm -r $(OUTPUT) $(OBJ_LIST)
Executing the make file:
gcc -c Srcs/main.c -o Srcs/main.o -I./Header/
gcc -c Srcs/src1.c -o Srcs/src1.o -I./Header/
gcc -c Srcs/src2.c -o Srcs/src2.o -I./Header/
gcc -o test ./Srcs/main.o ./Srcs/src1.o ./Srcs/src2.o
In particular, due to the template rule that I used, all the object files are generated in the folder ./Srcs/, what I would like to do is put all the object files in the directory where the final output will be, in my specific example the final output will be in the same directory where the makefile is located.
How can I write my makefile to achieve this?