Is there a way to tell make to apply a rule to every file that matches a pattern?












1















I am working on a project that has a directory structure that looks a bit like this:



inputs/
a.txt
b.txt
c.txt

outputs/
a.out
b.out
c.out

makefile


The files in the outputs folder are generated from the files in the inputs folder and I want to automatically update them whenever one of the input files changes.



I tried to do this by creating a makefile with a pattern rule. (To keep this question simple, I'm just copying the input file instead of transforming it)



outputs/%.txt: inputs/%.out
cp $< $@


However, this makefile didn't quite do what I want. If I run make outputs/a.txt it will apply the rule that I wrote and regenerate the output file if it is outdated but if I just run make then nothing happens:



make: *** No targets.  Stop.


Is there a way to tell make that by default I want to generate an output file for every file in the inputs folder?



edit: made the output files have a different file suffix than the input files.










share|improve this question





























    1















    I am working on a project that has a directory structure that looks a bit like this:



    inputs/
    a.txt
    b.txt
    c.txt

    outputs/
    a.out
    b.out
    c.out

    makefile


    The files in the outputs folder are generated from the files in the inputs folder and I want to automatically update them whenever one of the input files changes.



    I tried to do this by creating a makefile with a pattern rule. (To keep this question simple, I'm just copying the input file instead of transforming it)



    outputs/%.txt: inputs/%.out
    cp $< $@


    However, this makefile didn't quite do what I want. If I run make outputs/a.txt it will apply the rule that I wrote and regenerate the output file if it is outdated but if I just run make then nothing happens:



    make: *** No targets.  Stop.


    Is there a way to tell make that by default I want to generate an output file for every file in the inputs folder?



    edit: made the output files have a different file suffix than the input files.










    share|improve this question



























      1












      1








      1








      I am working on a project that has a directory structure that looks a bit like this:



      inputs/
      a.txt
      b.txt
      c.txt

      outputs/
      a.out
      b.out
      c.out

      makefile


      The files in the outputs folder are generated from the files in the inputs folder and I want to automatically update them whenever one of the input files changes.



      I tried to do this by creating a makefile with a pattern rule. (To keep this question simple, I'm just copying the input file instead of transforming it)



      outputs/%.txt: inputs/%.out
      cp $< $@


      However, this makefile didn't quite do what I want. If I run make outputs/a.txt it will apply the rule that I wrote and regenerate the output file if it is outdated but if I just run make then nothing happens:



      make: *** No targets.  Stop.


      Is there a way to tell make that by default I want to generate an output file for every file in the inputs folder?



      edit: made the output files have a different file suffix than the input files.










      share|improve this question
















      I am working on a project that has a directory structure that looks a bit like this:



      inputs/
      a.txt
      b.txt
      c.txt

      outputs/
      a.out
      b.out
      c.out

      makefile


      The files in the outputs folder are generated from the files in the inputs folder and I want to automatically update them whenever one of the input files changes.



      I tried to do this by creating a makefile with a pattern rule. (To keep this question simple, I'm just copying the input file instead of transforming it)



      outputs/%.txt: inputs/%.out
      cp $< $@


      However, this makefile didn't quite do what I want. If I run make outputs/a.txt it will apply the rule that I wrote and regenerate the output file if it is outdated but if I just run make then nothing happens:



      make: *** No targets.  Stop.


      Is there a way to tell make that by default I want to generate an output file for every file in the inputs folder?



      edit: made the output files have a different file suffix than the input files.







      make






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 23 mins ago









      Michael Homer

      49.8k8134174




      49.8k8134174










      asked Dec 6 '16 at 19:51









      hugomghugomg

      1,88931635




      1,88931635






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You would typically do this with a makefile something like this:



          IN = $(wildcard inputs/*.txt)
          OUT = $(subst inputs/,outputs/,$(IN))

          outputs/%.txt: inputs/%.txt
          cp $< $@

          default: $(OUT)


          This makes the default target (the first one in the file), depend on OUT which is the glob expansion of the existing files in IN with the directory string changed from inputs to outputs.





          If you want to change the suffix there are lots of other builtin functions to manipulate the targets. For example you can add an intervening operation:



          IN2 = $(addsuffix .out,$(basename $(IN)))


          The basename function will remove the trailing .txt, and the addsuffix will add a trailing .out.






          share|improve this answer


























          • What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

            – hugomg
            Dec 6 '16 at 20:49











          • There are many functions in gnu make. I updated my answer with an example.

            – meuh
            Dec 6 '16 at 21:04











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f328493%2fis-there-a-way-to-tell-make-to-apply-a-rule-to-every-file-that-matches-a-pattern%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You would typically do this with a makefile something like this:



          IN = $(wildcard inputs/*.txt)
          OUT = $(subst inputs/,outputs/,$(IN))

          outputs/%.txt: inputs/%.txt
          cp $< $@

          default: $(OUT)


          This makes the default target (the first one in the file), depend on OUT which is the glob expansion of the existing files in IN with the directory string changed from inputs to outputs.





          If you want to change the suffix there are lots of other builtin functions to manipulate the targets. For example you can add an intervening operation:



          IN2 = $(addsuffix .out,$(basename $(IN)))


          The basename function will remove the trailing .txt, and the addsuffix will add a trailing .out.






          share|improve this answer


























          • What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

            – hugomg
            Dec 6 '16 at 20:49











          • There are many functions in gnu make. I updated my answer with an example.

            – meuh
            Dec 6 '16 at 21:04
















          3














          You would typically do this with a makefile something like this:



          IN = $(wildcard inputs/*.txt)
          OUT = $(subst inputs/,outputs/,$(IN))

          outputs/%.txt: inputs/%.txt
          cp $< $@

          default: $(OUT)


          This makes the default target (the first one in the file), depend on OUT which is the glob expansion of the existing files in IN with the directory string changed from inputs to outputs.





          If you want to change the suffix there are lots of other builtin functions to manipulate the targets. For example you can add an intervening operation:



          IN2 = $(addsuffix .out,$(basename $(IN)))


          The basename function will remove the trailing .txt, and the addsuffix will add a trailing .out.






          share|improve this answer


























          • What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

            – hugomg
            Dec 6 '16 at 20:49











          • There are many functions in gnu make. I updated my answer with an example.

            – meuh
            Dec 6 '16 at 21:04














          3












          3








          3







          You would typically do this with a makefile something like this:



          IN = $(wildcard inputs/*.txt)
          OUT = $(subst inputs/,outputs/,$(IN))

          outputs/%.txt: inputs/%.txt
          cp $< $@

          default: $(OUT)


          This makes the default target (the first one in the file), depend on OUT which is the glob expansion of the existing files in IN with the directory string changed from inputs to outputs.





          If you want to change the suffix there are lots of other builtin functions to manipulate the targets. For example you can add an intervening operation:



          IN2 = $(addsuffix .out,$(basename $(IN)))


          The basename function will remove the trailing .txt, and the addsuffix will add a trailing .out.






          share|improve this answer















          You would typically do this with a makefile something like this:



          IN = $(wildcard inputs/*.txt)
          OUT = $(subst inputs/,outputs/,$(IN))

          outputs/%.txt: inputs/%.txt
          cp $< $@

          default: $(OUT)


          This makes the default target (the first one in the file), depend on OUT which is the glob expansion of the existing files in IN with the directory string changed from inputs to outputs.





          If you want to change the suffix there are lots of other builtin functions to manipulate the targets. For example you can add an intervening operation:



          IN2 = $(addsuffix .out,$(basename $(IN)))


          The basename function will remove the trailing .txt, and the addsuffix will add a trailing .out.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 6 '16 at 21:02

























          answered Dec 6 '16 at 20:33









          meuhmeuh

          32.4k12054




          32.4k12054













          • What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

            – hugomg
            Dec 6 '16 at 20:49











          • There are many functions in gnu make. I updated my answer with an example.

            – meuh
            Dec 6 '16 at 21:04



















          • What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

            – hugomg
            Dec 6 '16 at 20:49











          • There are many functions in gnu make. I updated my answer with an example.

            – meuh
            Dec 6 '16 at 21:04

















          What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

          – hugomg
          Dec 6 '16 at 20:49





          What if I want to change the file suffix for the files in the outputs folder to something other than txt? I can't think of a clean way to do that with subst...

          – hugomg
          Dec 6 '16 at 20:49













          There are many functions in gnu make. I updated my answer with an example.

          – meuh
          Dec 6 '16 at 21:04





          There are many functions in gnu make. I updated my answer with an example.

          – meuh
          Dec 6 '16 at 21:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f328493%2fis-there-a-way-to-tell-make-to-apply-a-rule-to-every-file-that-matches-a-pattern%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt