-lm doesn't work for my Makefile

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm trying to create a Makefile to compile my project. However, when I use the 'math.h' library, my make fails. This is the Makefile file :



run: tema1
./tema1

build: tema1.c
gcc tema1.c -o tema1 -lm

clean:
rm *.o tema1


The part of the code where I use the pow() and sqrt() is :



float score = sqrt(k) + pow(1.25, completed_lines);


But, even compiling using '-lm', I still get this error :



> /tmp/ccSQVWNy.o: In function `easy_win_score': tema1.c:(.text+0x1518):
> undefined reference to `sqrt' tema1.c:(.text+0x1540): undefined
> reference to `pow' collect2: error: ld returned 1 exit status
> <builtin>: recipe for target 'tema1' failed make: *** [tema1] Error 1


Any idea why and how can I fix this ? If I only use this in the terminal :




gcc tema1.c -o tema1 -lm




it works, but in the Makefile, it fails.










share|improve this question






























    1















    I'm trying to create a Makefile to compile my project. However, when I use the 'math.h' library, my make fails. This is the Makefile file :



    run: tema1
    ./tema1

    build: tema1.c
    gcc tema1.c -o tema1 -lm

    clean:
    rm *.o tema1


    The part of the code where I use the pow() and sqrt() is :



    float score = sqrt(k) + pow(1.25, completed_lines);


    But, even compiling using '-lm', I still get this error :



    > /tmp/ccSQVWNy.o: In function `easy_win_score': tema1.c:(.text+0x1518):
    > undefined reference to `sqrt' tema1.c:(.text+0x1540): undefined
    > reference to `pow' collect2: error: ld returned 1 exit status
    > <builtin>: recipe for target 'tema1' failed make: *** [tema1] Error 1


    Any idea why and how can I fix this ? If I only use this in the terminal :




    gcc tema1.c -o tema1 -lm




    it works, but in the Makefile, it fails.










    share|improve this question


























      1












      1








      1








      I'm trying to create a Makefile to compile my project. However, when I use the 'math.h' library, my make fails. This is the Makefile file :



      run: tema1
      ./tema1

      build: tema1.c
      gcc tema1.c -o tema1 -lm

      clean:
      rm *.o tema1


      The part of the code where I use the pow() and sqrt() is :



      float score = sqrt(k) + pow(1.25, completed_lines);


      But, even compiling using '-lm', I still get this error :



      > /tmp/ccSQVWNy.o: In function `easy_win_score': tema1.c:(.text+0x1518):
      > undefined reference to `sqrt' tema1.c:(.text+0x1540): undefined
      > reference to `pow' collect2: error: ld returned 1 exit status
      > <builtin>: recipe for target 'tema1' failed make: *** [tema1] Error 1


      Any idea why and how can I fix this ? If I only use this in the terminal :




      gcc tema1.c -o tema1 -lm




      it works, but in the Makefile, it fails.










      share|improve this question
















      I'm trying to create a Makefile to compile my project. However, when I use the 'math.h' library, my make fails. This is the Makefile file :



      run: tema1
      ./tema1

      build: tema1.c
      gcc tema1.c -o tema1 -lm

      clean:
      rm *.o tema1


      The part of the code where I use the pow() and sqrt() is :



      float score = sqrt(k) + pow(1.25, completed_lines);


      But, even compiling using '-lm', I still get this error :



      > /tmp/ccSQVWNy.o: In function `easy_win_score': tema1.c:(.text+0x1518):
      > undefined reference to `sqrt' tema1.c:(.text+0x1540): undefined
      > reference to `pow' collect2: error: ld returned 1 exit status
      > <builtin>: recipe for target 'tema1' failed make: *** [tema1] Error 1


      Any idea why and how can I fix this ? If I only use this in the terminal :




      gcc tema1.c -o tema1 -lm




      it works, but in the Makefile, it fails.







      linux compiling make






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 18 at 0:43









      Rui F Ribeiro

      42.1k1483142




      42.1k1483142










      asked Mar 15 at 9:46









      ballowballow

      83




      83




















          1 Answer
          1






          active

          oldest

          votes


















          2














          This happens because your Makefile doesn’t explain how to build tema1 (from Make’s perspective), so it uses its built-in rules:




          • run depends on tema1;


          • tema1 doesn’t have a definition, but there’s a C file, so Make tries to compile it using its default rule, which doesn’t specify -lm.

          To fix this, say



          tema1: tema1.c
          gcc tema1.c -o tema1 -lm


          instead of build: tema1.c etc.



          You can reduce repetition by using automatic variables:



          tema1: tema1.c
          gcc $^ -o $@ -lm


          To keep “named” rules (run, build etc.), make them depend on concrete artifacts (apart from clean, since it doesn’t produce anything), add separate rules for the concrete artifacts, and mark the “named” rules as phony (so Make won’t expect a corresponding on-disk artifact):



          build: tema1

          tema1: tema1.c
          gcc $^ -o $@ -lm

          .PHONY: run build clean


          It’s also worth changing clean so it doesn’t fail when there’s nothing to clean:



          clean:
          rm -f *.o tema1





          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%2f506459%2flm-doesnt-work-for-my-makefile%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









            2














            This happens because your Makefile doesn’t explain how to build tema1 (from Make’s perspective), so it uses its built-in rules:




            • run depends on tema1;


            • tema1 doesn’t have a definition, but there’s a C file, so Make tries to compile it using its default rule, which doesn’t specify -lm.

            To fix this, say



            tema1: tema1.c
            gcc tema1.c -o tema1 -lm


            instead of build: tema1.c etc.



            You can reduce repetition by using automatic variables:



            tema1: tema1.c
            gcc $^ -o $@ -lm


            To keep “named” rules (run, build etc.), make them depend on concrete artifacts (apart from clean, since it doesn’t produce anything), add separate rules for the concrete artifacts, and mark the “named” rules as phony (so Make won’t expect a corresponding on-disk artifact):



            build: tema1

            tema1: tema1.c
            gcc $^ -o $@ -lm

            .PHONY: run build clean


            It’s also worth changing clean so it doesn’t fail when there’s nothing to clean:



            clean:
            rm -f *.o tema1





            share|improve this answer





























              2














              This happens because your Makefile doesn’t explain how to build tema1 (from Make’s perspective), so it uses its built-in rules:




              • run depends on tema1;


              • tema1 doesn’t have a definition, but there’s a C file, so Make tries to compile it using its default rule, which doesn’t specify -lm.

              To fix this, say



              tema1: tema1.c
              gcc tema1.c -o tema1 -lm


              instead of build: tema1.c etc.



              You can reduce repetition by using automatic variables:



              tema1: tema1.c
              gcc $^ -o $@ -lm


              To keep “named” rules (run, build etc.), make them depend on concrete artifacts (apart from clean, since it doesn’t produce anything), add separate rules for the concrete artifacts, and mark the “named” rules as phony (so Make won’t expect a corresponding on-disk artifact):



              build: tema1

              tema1: tema1.c
              gcc $^ -o $@ -lm

              .PHONY: run build clean


              It’s also worth changing clean so it doesn’t fail when there’s nothing to clean:



              clean:
              rm -f *.o tema1





              share|improve this answer



























                2












                2








                2







                This happens because your Makefile doesn’t explain how to build tema1 (from Make’s perspective), so it uses its built-in rules:




                • run depends on tema1;


                • tema1 doesn’t have a definition, but there’s a C file, so Make tries to compile it using its default rule, which doesn’t specify -lm.

                To fix this, say



                tema1: tema1.c
                gcc tema1.c -o tema1 -lm


                instead of build: tema1.c etc.



                You can reduce repetition by using automatic variables:



                tema1: tema1.c
                gcc $^ -o $@ -lm


                To keep “named” rules (run, build etc.), make them depend on concrete artifacts (apart from clean, since it doesn’t produce anything), add separate rules for the concrete artifacts, and mark the “named” rules as phony (so Make won’t expect a corresponding on-disk artifact):



                build: tema1

                tema1: tema1.c
                gcc $^ -o $@ -lm

                .PHONY: run build clean


                It’s also worth changing clean so it doesn’t fail when there’s nothing to clean:



                clean:
                rm -f *.o tema1





                share|improve this answer















                This happens because your Makefile doesn’t explain how to build tema1 (from Make’s perspective), so it uses its built-in rules:




                • run depends on tema1;


                • tema1 doesn’t have a definition, but there’s a C file, so Make tries to compile it using its default rule, which doesn’t specify -lm.

                To fix this, say



                tema1: tema1.c
                gcc tema1.c -o tema1 -lm


                instead of build: tema1.c etc.



                You can reduce repetition by using automatic variables:



                tema1: tema1.c
                gcc $^ -o $@ -lm


                To keep “named” rules (run, build etc.), make them depend on concrete artifacts (apart from clean, since it doesn’t produce anything), add separate rules for the concrete artifacts, and mark the “named” rules as phony (so Make won’t expect a corresponding on-disk artifact):



                build: tema1

                tema1: tema1.c
                gcc $^ -o $@ -lm

                .PHONY: run build clean


                It’s also worth changing clean so it doesn’t fail when there’s nothing to clean:



                clean:
                rm -f *.o tema1






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 15 at 10:52

























                answered Mar 15 at 9:52









                Stephen KittStephen Kitt

                181k25414493




                181k25414493



























                    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%2f506459%2flm-doesnt-work-for-my-makefile%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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?