Makefile Syntax unclear











up vote
-1
down vote

favorite












This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.



$(BUILD_DIR)/%.o: %.c  $(BUILD_DIR) 
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@



  1. What is the usage of "$(BUILD_DIR)" in the dependency?

  2. What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?










share|improve this question


























    up vote
    -1
    down vote

    favorite












    This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.



    $(BUILD_DIR)/%.o: %.c  $(BUILD_DIR) 
    $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@



    1. What is the usage of "$(BUILD_DIR)" in the dependency?

    2. What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.



      $(BUILD_DIR)/%.o: %.c  $(BUILD_DIR) 
      $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@



      1. What is the usage of "$(BUILD_DIR)" in the dependency?

      2. What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?










      share|improve this question













      This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.



      $(BUILD_DIR)/%.o: %.c  $(BUILD_DIR) 
      $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@



      1. What is the usage of "$(BUILD_DIR)" in the dependency?

      2. What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?







      makefile






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 8:31









      Yaron

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build and that one of the source files is bar/foo.c.





          1. $(BUILD_DIR) in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:



            $(BUILD_DIR):
            mkdir -p $@


            But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:



            $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)


            that tells make to consider only the existence of $(BUILD_DIR), not its last modification time, when deciding to re-build or not.




          2. $(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ is just a combination of make automatic variables and functions.





            • $< and $@ expand as the first prerequisite (bar/foo.c) and the target (build/bar/foo.o) respectively.


            • $(<:.c=.lst) replaces .c by .lst in $<: bar/foo.lst.


            • $(notdir $(<:.c=.lst)) removes the directory part: foo.lst.




          All in all, for a bar/foo.c source file, and with BUILD_DIR = build, the pattern rule would be equivalent to:



              build/bar/foo.o: bar/foo.c | build
          $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o


          Note that there are two different situations to consider:





          1. All your source files are in the same directory as the Makefile (no bar/foo.c, just foo.c). Then you can simplify your recipe:



            $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@


            because the $(notdir...) is useless.




          2. Your source files can be in sub-directories (bar/foo.c). Then you need the $(notdir...) in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c and baz/foo.c) you will have a name conflict for $(BUILD_DIR)/foo.lst and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent to build/bar (or build/baz), not just build. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:



            $(BUILD_DIR)/%.o: %.c
            mkdir -p $(dir $@)
            $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


            There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53407961%2fmakefile-syntax-unclear%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








            up vote
            0
            down vote



            accepted










            As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build and that one of the source files is bar/foo.c.





            1. $(BUILD_DIR) in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:



              $(BUILD_DIR):
              mkdir -p $@


              But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:



              $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)


              that tells make to consider only the existence of $(BUILD_DIR), not its last modification time, when deciding to re-build or not.




            2. $(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ is just a combination of make automatic variables and functions.





              • $< and $@ expand as the first prerequisite (bar/foo.c) and the target (build/bar/foo.o) respectively.


              • $(<:.c=.lst) replaces .c by .lst in $<: bar/foo.lst.


              • $(notdir $(<:.c=.lst)) removes the directory part: foo.lst.




            All in all, for a bar/foo.c source file, and with BUILD_DIR = build, the pattern rule would be equivalent to:



                build/bar/foo.o: bar/foo.c | build
            $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o


            Note that there are two different situations to consider:





            1. All your source files are in the same directory as the Makefile (no bar/foo.c, just foo.c). Then you can simplify your recipe:



              $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@


              because the $(notdir...) is useless.




            2. Your source files can be in sub-directories (bar/foo.c). Then you need the $(notdir...) in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c and baz/foo.c) you will have a name conflict for $(BUILD_DIR)/foo.lst and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent to build/bar (or build/baz), not just build. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:



              $(BUILD_DIR)/%.o: %.c
              mkdir -p $(dir $@)
              $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


              There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.








            share|improve this answer



























              up vote
              0
              down vote



              accepted










              As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build and that one of the source files is bar/foo.c.





              1. $(BUILD_DIR) in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:



                $(BUILD_DIR):
                mkdir -p $@


                But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:



                $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)


                that tells make to consider only the existence of $(BUILD_DIR), not its last modification time, when deciding to re-build or not.




              2. $(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ is just a combination of make automatic variables and functions.





                • $< and $@ expand as the first prerequisite (bar/foo.c) and the target (build/bar/foo.o) respectively.


                • $(<:.c=.lst) replaces .c by .lst in $<: bar/foo.lst.


                • $(notdir $(<:.c=.lst)) removes the directory part: foo.lst.




              All in all, for a bar/foo.c source file, and with BUILD_DIR = build, the pattern rule would be equivalent to:



                  build/bar/foo.o: bar/foo.c | build
              $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o


              Note that there are two different situations to consider:





              1. All your source files are in the same directory as the Makefile (no bar/foo.c, just foo.c). Then you can simplify your recipe:



                $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@


                because the $(notdir...) is useless.




              2. Your source files can be in sub-directories (bar/foo.c). Then you need the $(notdir...) in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c and baz/foo.c) you will have a name conflict for $(BUILD_DIR)/foo.lst and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent to build/bar (or build/baz), not just build. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:



                $(BUILD_DIR)/%.o: %.c
                mkdir -p $(dir $@)
                $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


                There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.








              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build and that one of the source files is bar/foo.c.





                1. $(BUILD_DIR) in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:



                  $(BUILD_DIR):
                  mkdir -p $@


                  But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:



                  $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)


                  that tells make to consider only the existence of $(BUILD_DIR), not its last modification time, when deciding to re-build or not.




                2. $(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ is just a combination of make automatic variables and functions.





                  • $< and $@ expand as the first prerequisite (bar/foo.c) and the target (build/bar/foo.o) respectively.


                  • $(<:.c=.lst) replaces .c by .lst in $<: bar/foo.lst.


                  • $(notdir $(<:.c=.lst)) removes the directory part: foo.lst.




                All in all, for a bar/foo.c source file, and with BUILD_DIR = build, the pattern rule would be equivalent to:



                    build/bar/foo.o: bar/foo.c | build
                $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o


                Note that there are two different situations to consider:





                1. All your source files are in the same directory as the Makefile (no bar/foo.c, just foo.c). Then you can simplify your recipe:



                  $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@


                  because the $(notdir...) is useless.




                2. Your source files can be in sub-directories (bar/foo.c). Then you need the $(notdir...) in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c and baz/foo.c) you will have a name conflict for $(BUILD_DIR)/foo.lst and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent to build/bar (or build/baz), not just build. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:



                  $(BUILD_DIR)/%.o: %.c
                  mkdir -p $(dir $@)
                  $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


                  There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.








                share|improve this answer














                As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build and that one of the source files is bar/foo.c.





                1. $(BUILD_DIR) in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:



                  $(BUILD_DIR):
                  mkdir -p $@


                  But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:



                  $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)


                  that tells make to consider only the existence of $(BUILD_DIR), not its last modification time, when deciding to re-build or not.




                2. $(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ is just a combination of make automatic variables and functions.





                  • $< and $@ expand as the first prerequisite (bar/foo.c) and the target (build/bar/foo.o) respectively.


                  • $(<:.c=.lst) replaces .c by .lst in $<: bar/foo.lst.


                  • $(notdir $(<:.c=.lst)) removes the directory part: foo.lst.




                All in all, for a bar/foo.c source file, and with BUILD_DIR = build, the pattern rule would be equivalent to:



                    build/bar/foo.o: bar/foo.c | build
                $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o


                Note that there are two different situations to consider:





                1. All your source files are in the same directory as the Makefile (no bar/foo.c, just foo.c). Then you can simplify your recipe:



                  $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@


                  because the $(notdir...) is useless.




                2. Your source files can be in sub-directories (bar/foo.c). Then you need the $(notdir...) in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c and baz/foo.c) you will have a name conflict for $(BUILD_DIR)/foo.lst and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent to build/bar (or build/baz), not just build. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:



                  $(BUILD_DIR)/%.o: %.c
                  mkdir -p $(dir $@)
                  $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@


                  There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.









                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 10:16

























                answered Nov 21 at 9:36









                Renaud Pacalet

                8,15421629




                8,15421629






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407961%2fmakefile-syntax-unclear%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...