Home > Back-end >  use make to recompile my program by .c files
use make to recompile my program by .c files

Time:01-29

I'm trying to make a makefile that doesn't recompile my program if I delete my object files...because my .c files are up to date, how can I do this?

I try to do this but it doesn't work

SRC = main.c file.c file1.c

OBJ = $(SRC:c=o)

%.o: %.c 
    gcc -c $< -o $@

name = program

all:    $(NAME)

$(NAME): $(SRC) ; $(OBJ)
    gcc $(OBJ) -o $(NAME)

CodePudding user response:

Thank! To solve my problem i'm must add special target .INTERMEDIATE or .SECONDARY like this: More details on this issue can be found at the link Chains of Implicit Rules provided by Henry.

SRC = main.c file.c file1.c

OBJ = $(SRC:c=o)

%.o: %.c
    gcc -c $< -o $@

NAME = program

all:    $(NAME)

$(NAME): $(SRC) ; $(OBJ)
    gcc $(OBJ) -o $(NAME)

.SECONDARY: $(OBJ)

CodePudding user response:

Well, that is basically impossible.

To create the binary, you need the object files. To create the object files you need the source files.

The binary is up to date if it is newer than the up to date object files. And the object files are up to date if they are newer than the source files.

There is no direct route from the binary to the source files. Deleting the object files will always rebuild your source code.

Update: It is possible (as pointed below), but not easy. And IMHO the makefile complexity added is not worth it. Just keep the object files. Instead change the makefile to put you object files in a separate directory away from the source code.

  •  Tags:  
  • Related