cshellmakefiletargetautomatic-variable

Unexpected execution of "all" target in my makefile


I've a makefile that is suppose to repeat an execution of a c code (BootstrapCL.c) one time for each file.csv in the directory. For each execution it should give in input to the c code (as argv) 2 string: the name, with and without extention, of the input csv file used in the current execution. This is the makefile content:

SRCS := $(wildcard *.csv)
BINS := $(SRCS:%.csv=%)

all: ${BINS}

%: BootstrapCL.c
    gcc -Wall BootstrapCL.c  -lm -o BootstrapCL
    ./BootstrapCL $@.csv $@

The problem is that, after the execution of all the group of csv file (iI'd like to execute only the target inside ${BINS} list) , it also run a last execution with "all" target. Of course I don't have any all.csv file in my folder; I think I'm using $@ in the wrong way but I don't understand why and how to fix the issue, any idea?


Solution

  • I have a slightly different take on the solution then Beta's:

    SRCS := $(wildcard *.csv)
    BINS := $(SRCS:%.csv=%)
    
    # All rule - requires binaries to be generated. This does not generate anything, therefore it is PHONY
    .PHONY: all
    all: ${BINS}
    
    # Generates the binaries - requires BootstrapCL exe to be generated first
    ${BINS}: BootstrapCL
        ./BootstrapCL $@.csv $@
    
    # Generates BootstrapCL
    BootstrapCL:
        gcc -Wall BootstrapCL.c  -lm -o BootstrapCL
    

    From what I can tell - you need to compile BootstrapCL.c once to generate BootstrapCL executable. Then using that you genetate multiple binaries - one for eacn .csv file.

    So in reverse: