1

First of all, I am not at all sure whether this is the correct place to ask a question like this but...

I am currently working on a directory and trying to create a file which would contain all of the contents of the files of the directory. The requirement is that I have to do this by using a rule in a Makefile. Currently, my makefile has a list of the files at the top, then a few other rules and then my sad attempt of a rule. The issue I'm running into is that I constantly get the "no rule to make target x needed by y". I think the issue is with either my recipe or my prerequisites. So:

FILES= file1 file2 file3 file4 file5...

directory_name/all.txt: **HERE IS THE PART IDK ABOUT**
   cat $^ > $@

I'm very new to the make command and to coding in general for that matter, so it may be that this is a silly problem to have but still, I couldn't find any satisfying answers online elsewhere. I have been trying to either refer to the files via their directory (like directory_name/%.txt or something) or by just listing the files as the prerequisites but nothing has worked.

2 Answers 2

2

Your rule could look like

directory_name/all.txt: $(FILES)
        cat $(FILES) > $@

This would provide maximum portability so that the rule would work with both GNU make and BSD make (which may not understand $^; you don't say which implementation you're using). Note that the line with the cat command needs to be indented with a tab character.

5
  • It still keeps returning the error message "No rule to make target 'file 1', needed by directory_name/all.txt" Commented Dec 15, 2019 at 10:31
  • also, I'm using GNU Commented Dec 15, 2019 at 10:47
  • 1
    @hesse You have said nothing about a file 1. In your question, you have file1. You also don't say anything about whether the files you list in your FILES variable exists or not. If they don't exist, make would obviously have to have a rule describing how to create them. If there is no such rule, it complains, as you have shown. Commented Dec 15, 2019 at 11:04
  • The space character was a spelling mistake, my apologies. And the files already exist in a sub-directory of the directory where I have the Makefile, could that have an effect on the process? Commented Dec 15, 2019 at 13:53
  • @hesse You would have to use the path to each file in the assignment to FILES. Commented Dec 15, 2019 at 14:25
0

The dependency list is just $(FILES).

Basically, that just expands the list named. So the target is remade if any of the files that contribute to its validity are newer that the target itself.

There is a shorthand that will wildcard files, if you have a pattern. For your example, probably just:

FILES= file[1-9]

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.