Makefile strange variable substitution2019 Community Moderator ElectionPlease explain what happens when I run...

What are some noteworthy "mic-drop" moments in math?

Why does liquid water form when we exhale on a mirror?

Could you please stop shuffling the deck and play already?

Does the nature of the Apocalypse in The Umbrella Academy change from the first to the last episode?

How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?

How is the wildcard * interpreted as a command?

NASA's RS-25 Engines shut down time

They call me Inspector Morse

Linux Ubuntu 18.04 Full Backup

Can I pump my MTB tire to max (55 psi / 380 kPa) without the tube inside bursting?

How to detect if C code (which needs 'extern C') is compiled in C++

Signed and unsigned numbers

Reverse string, can I make it faster?

When stopping and starting a tile job, what to do with the extra thinset from previous row's cleanup?

Is "conspicuously missing" or "conspicuously" the subject of this sentence?

Is it necessary to separate DC power cables and data cables?

How did Alan Turing break the enigma code using the hint given by the lady in the bar?

How strictly should I take "Candidates must be local"?

Latex does not go to next line

PTIJ: Should I kill my computer after installing software?

Single word request: Harming the benefactor

Plausibility of Mushroom Buildings

Recommendation letter by significant other if you worked with them professionally?

Do items de-spawn in Diablo?



Makefile strange variable substitution



2019 Community Moderator ElectionPlease explain what happens when I run this Makefileuse bash to pass 2 variables to a MakefileHow can i make use of makefile exported values while running script fileMakefile include env fileOutput to multiple files with MakefileIs there away to tell make to apply a rule to every file that matches a pattern?Folder exclusion formatting issueHow can I refactor this Makefile to not use fake .out outputs?Makefile fails when trying to run a C programCan't access files from within makefile












1















My Makefile looks like this:



%.foo: %.bar
cp $< $@

test: *.foo
echo *.foo


I have 2 files in the directory: a.bar and b.bar. When I run make test it outputs:



cp b.bar *.foo
echo *.foo
*.foo


It also creates a file *.foo in the current directory.



I am actually expecting to see this:



cp a.bar a.foo
cp b.bar b.foo
echo *.foo
a.foo b.foo


And also creating a.foo and b.foo. How to achieve that?










share|improve this question



























    1















    My Makefile looks like this:



    %.foo: %.bar
    cp $< $@

    test: *.foo
    echo *.foo


    I have 2 files in the directory: a.bar and b.bar. When I run make test it outputs:



    cp b.bar *.foo
    echo *.foo
    *.foo


    It also creates a file *.foo in the current directory.



    I am actually expecting to see this:



    cp a.bar a.foo
    cp b.bar b.foo
    echo *.foo
    a.foo b.foo


    And also creating a.foo and b.foo. How to achieve that?










    share|improve this question

























      1












      1








      1








      My Makefile looks like this:



      %.foo: %.bar
      cp $< $@

      test: *.foo
      echo *.foo


      I have 2 files in the directory: a.bar and b.bar. When I run make test it outputs:



      cp b.bar *.foo
      echo *.foo
      *.foo


      It also creates a file *.foo in the current directory.



      I am actually expecting to see this:



      cp a.bar a.foo
      cp b.bar b.foo
      echo *.foo
      a.foo b.foo


      And also creating a.foo and b.foo. How to achieve that?










      share|improve this question














      My Makefile looks like this:



      %.foo: %.bar
      cp $< $@

      test: *.foo
      echo *.foo


      I have 2 files in the directory: a.bar and b.bar. When I run make test it outputs:



      cp b.bar *.foo
      echo *.foo
      *.foo


      It also creates a file *.foo in the current directory.



      I am actually expecting to see this:



      cp a.bar a.foo
      cp b.bar b.foo
      echo *.foo
      a.foo b.foo


      And also creating a.foo and b.foo. How to achieve that?







      make






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 52 mins ago









      Martin ŽdilaMartin Ždila

      150115




      150115






















          2 Answers
          2






          active

          oldest

          votes


















          3














          In this case you need to handle wildcards explicitly, with the wildcard function (at least in GNU Make):



          %.foo: %.bar
          cp $< $@

          foos = $(patsubst %.bar,%.foo,$(wildcard *.bar))

          test: $(foos)
          echo $(foos)


          $(wildcard *.bar) expands to all the files ending in .bar, the patsubst call replaces .bar with .foo, and all the targets are then processed as you’d expect.






          share|improve this answer































            2














            There is no *.foo file to begin with. So what make does is look for how to make *.foo literaly and the first rule does this. Make expands $< to the first pre-requisite (*.bar, which happens to be b.bar in this case). Make then runs the shell command cp b.bar *.foo. Since there is no *.foo, shell expands it to cp b.bar *.foo literally. That's how you get a *.foo file.



            You can verify this by running make -d test.



            You can get the effect you want by generating the list of targets based on list of prerequisites.



            TARGETS = $(patsubst %.bar,%.foo,$(wildcard *.bar))
            %.foo: %.bar
            @cp $< $@
            test: $(TARGETS)
            @echo $(TARGETS)
            echo *.foo





            share|improve this answer























              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%2f505735%2fmakefile-strange-variable-substitution%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              In this case you need to handle wildcards explicitly, with the wildcard function (at least in GNU Make):



              %.foo: %.bar
              cp $< $@

              foos = $(patsubst %.bar,%.foo,$(wildcard *.bar))

              test: $(foos)
              echo $(foos)


              $(wildcard *.bar) expands to all the files ending in .bar, the patsubst call replaces .bar with .foo, and all the targets are then processed as you’d expect.






              share|improve this answer




























                3














                In this case you need to handle wildcards explicitly, with the wildcard function (at least in GNU Make):



                %.foo: %.bar
                cp $< $@

                foos = $(patsubst %.bar,%.foo,$(wildcard *.bar))

                test: $(foos)
                echo $(foos)


                $(wildcard *.bar) expands to all the files ending in .bar, the patsubst call replaces .bar with .foo, and all the targets are then processed as you’d expect.






                share|improve this answer


























                  3












                  3








                  3







                  In this case you need to handle wildcards explicitly, with the wildcard function (at least in GNU Make):



                  %.foo: %.bar
                  cp $< $@

                  foos = $(patsubst %.bar,%.foo,$(wildcard *.bar))

                  test: $(foos)
                  echo $(foos)


                  $(wildcard *.bar) expands to all the files ending in .bar, the patsubst call replaces .bar with .foo, and all the targets are then processed as you’d expect.






                  share|improve this answer













                  In this case you need to handle wildcards explicitly, with the wildcard function (at least in GNU Make):



                  %.foo: %.bar
                  cp $< $@

                  foos = $(patsubst %.bar,%.foo,$(wildcard *.bar))

                  test: $(foos)
                  echo $(foos)


                  $(wildcard *.bar) expands to all the files ending in .bar, the patsubst call replaces .bar with .foo, and all the targets are then processed as you’d expect.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 42 mins ago









                  Stephen KittStephen Kitt

                  175k24400478




                  175k24400478

























                      2














                      There is no *.foo file to begin with. So what make does is look for how to make *.foo literaly and the first rule does this. Make expands $< to the first pre-requisite (*.bar, which happens to be b.bar in this case). Make then runs the shell command cp b.bar *.foo. Since there is no *.foo, shell expands it to cp b.bar *.foo literally. That's how you get a *.foo file.



                      You can verify this by running make -d test.



                      You can get the effect you want by generating the list of targets based on list of prerequisites.



                      TARGETS = $(patsubst %.bar,%.foo,$(wildcard *.bar))
                      %.foo: %.bar
                      @cp $< $@
                      test: $(TARGETS)
                      @echo $(TARGETS)
                      echo *.foo





                      share|improve this answer




























                        2














                        There is no *.foo file to begin with. So what make does is look for how to make *.foo literaly and the first rule does this. Make expands $< to the first pre-requisite (*.bar, which happens to be b.bar in this case). Make then runs the shell command cp b.bar *.foo. Since there is no *.foo, shell expands it to cp b.bar *.foo literally. That's how you get a *.foo file.



                        You can verify this by running make -d test.



                        You can get the effect you want by generating the list of targets based on list of prerequisites.



                        TARGETS = $(patsubst %.bar,%.foo,$(wildcard *.bar))
                        %.foo: %.bar
                        @cp $< $@
                        test: $(TARGETS)
                        @echo $(TARGETS)
                        echo *.foo





                        share|improve this answer


























                          2












                          2








                          2







                          There is no *.foo file to begin with. So what make does is look for how to make *.foo literaly and the first rule does this. Make expands $< to the first pre-requisite (*.bar, which happens to be b.bar in this case). Make then runs the shell command cp b.bar *.foo. Since there is no *.foo, shell expands it to cp b.bar *.foo literally. That's how you get a *.foo file.



                          You can verify this by running make -d test.



                          You can get the effect you want by generating the list of targets based on list of prerequisites.



                          TARGETS = $(patsubst %.bar,%.foo,$(wildcard *.bar))
                          %.foo: %.bar
                          @cp $< $@
                          test: $(TARGETS)
                          @echo $(TARGETS)
                          echo *.foo





                          share|improve this answer













                          There is no *.foo file to begin with. So what make does is look for how to make *.foo literaly and the first rule does this. Make expands $< to the first pre-requisite (*.bar, which happens to be b.bar in this case). Make then runs the shell command cp b.bar *.foo. Since there is no *.foo, shell expands it to cp b.bar *.foo literally. That's how you get a *.foo file.



                          You can verify this by running make -d test.



                          You can get the effect you want by generating the list of targets based on list of prerequisites.



                          TARGETS = $(patsubst %.bar,%.foo,$(wildcard *.bar))
                          %.foo: %.bar
                          @cp $< $@
                          test: $(TARGETS)
                          @echo $(TARGETS)
                          echo *.foo






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 28 mins ago









                          Satya MishraSatya Mishra

                          34615




                          34615






























                              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%2f505735%2fmakefile-strange-variable-substitution%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

                              迭戈·戈丁...

                              A phrase ”follow into" in a context The 2019 Stack Overflow Developer Survey Results Are...

                              1960s short story making fun of James Bond-style spy fiction The 2019 Stack Overflow Developer...