0

I have the following structure in my project.

  • /
    • src/
    • bin/
    • Makefile

In src directory there will be multiple src files (each has a main function). I need to write makefile such that when I run make program1 It should search for program1.c in src folder and compile the executable as program1* in bin folder.

I have came across this question How can Makefile use separate directories for source code and binaries? But, it seems that I need to manually enter all program names into PROG variable. I just need to supply binary name with make and it should do the compilation for that respective src file?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

Okay, after a bit of experimentation with my Makefile. I finally got the solution for my problem.

Current Build System

   CC = gcc
   CFLAGS = -g -Wall
   SRC = ./src/
   BIN = ./bin/

   %: $(SRC)%.c
       $(CC) $(CFLAGS) $< -o $(BIN)$@
   
   .PHONY: clean
   
   clean: 
       rm $(BIN)*