Makefile strange variable substitution












2















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



























    2















    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

























      2












      2








      2








      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 5 hours ago









      Martin ŽdilaMartin Ždila

      155115




      155115






















          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































            3














            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 5 hours ago









                  Stephen KittStephen Kitt

                  175k24400478




                  175k24400478

























                      3














                      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




























                        3














                        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


























                          3












                          3








                          3







                          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 4 hours ago









                          Satya MishraSatya Mishra

                          35615




                          35615






























                              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

                              Loup dans la culture

                              How to solve the problem of ntp “Unable to contact time server” from KDE?

                              ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux