0

I have a.csv,b.csv, ... in a my docs/csv directory, I need convert each of this file to a json file.

I follow this question to write a Makefile like this.

SRCS = $(wildcard docs/csv/*.csv)
DESTS = $(patsubst docs/csv/%.csv, scripts/data/%.lua, $(SRCS))

all: $(DESTS)

$(DESTS): $(SRCS)
    echo $@
    echo $<

but every time I ran make all, the echo $@ show every file as expected, but echo $< always show the single file, called items.csv in my csv folder.

2
  • The title of your question is inaccurate-- I almost marked this a duplicate.
    – Beta
    Commented Sep 30, 2013 at 17:22
  • make comes with a manual, in which questions such as yours are answered. Tell us what isn't clear in the manual. Commented Oct 1, 2013 at 9:22

3 Answers 3

2

The trouble is that in this rule:

$(DESTS): $(SRCS)
    ...

every lua file depends on all csv files, which is not what I think you intend. And since $< expands to the first prerequisite, you get the same one (items.csv) for every target.

Try this:

all: $(DESTS)

scripts/data/%.lua: docs/csv/%.csv 
    echo $@
    echo $<
0

$<

is the name of the FIRST dependency. Use $^ for all the dependencies

$@

is the name of the current target

1
  • I need variable match the $@, if the $@ is scripts/data/a.lua then I need docs/csv/a.csv, is there such a variable, or what's wrong with my makefile. Commented Sep 30, 2013 at 17:24
0

The GNU make man page on Automatic Variables is extremely useful. Here's what it says:

$@

The file name of the target of the rule. If the target is an archive member, then ‘$@’ is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ‘$@’ is the name of whichever target caused the rule's recipe to be run.

$<

The name of the first prerequisite. If the target got its recipe from an implicit rule, this will be the first prerequisite added by the implicit rule (see Implicit Rules).

Incidentally, you probably want to write your make rule as a pattern rule instead:

%.lua : %.csv
    <rules for making a lua from a csv>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.