undefined reference to `sqrt'











up vote
2
down vote

favorite
1












Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you










share|improve this question
























  • gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
    – old_timer
    Jan 8 '12 at 19:41















up vote
2
down vote

favorite
1












Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you










share|improve this question
























  • gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
    – old_timer
    Jan 8 '12 at 19:41













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you










share|improve this question















Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you







c nan sqrt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 '12 at 19:42









Pascal Cuoq

66.5k6124227




66.5k6124227










asked Jan 8 '12 at 19:39









Yakov

3,4561478147




3,4561478147












  • gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
    – old_timer
    Jan 8 '12 at 19:41


















  • gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
    – old_timer
    Jan 8 '12 at 19:41
















gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41




gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41












2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










You have to add the -lm flag on most Unix-based systems, as in:



Compile using:



gcc -c file.c


and then link using:



gcc -o program file.o -lm


Or if you don't want to separate the two compilation steps, simply write:



gcc -o program file.c -lm





share|improve this answer




























    up vote
    4
    down vote













    Link with -lm to link with the math library






    share|improve this answer





















    • Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
      – Akash Chandwani
      Nov 27 '16 at 18:06











    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%2f8780603%2fundefined-reference-to-sqrt%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








    up vote
    5
    down vote



    accepted










    You have to add the -lm flag on most Unix-based systems, as in:



    Compile using:



    gcc -c file.c


    and then link using:



    gcc -o program file.o -lm


    Or if you don't want to separate the two compilation steps, simply write:



    gcc -o program file.c -lm





    share|improve this answer

























      up vote
      5
      down vote



      accepted










      You have to add the -lm flag on most Unix-based systems, as in:



      Compile using:



      gcc -c file.c


      and then link using:



      gcc -o program file.o -lm


      Or if you don't want to separate the two compilation steps, simply write:



      gcc -o program file.c -lm





      share|improve this answer























        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        You have to add the -lm flag on most Unix-based systems, as in:



        Compile using:



        gcc -c file.c


        and then link using:



        gcc -o program file.o -lm


        Or if you don't want to separate the two compilation steps, simply write:



        gcc -o program file.c -lm





        share|improve this answer












        You have to add the -lm flag on most Unix-based systems, as in:



        Compile using:



        gcc -c file.c


        and then link using:



        gcc -o program file.o -lm


        Or if you don't want to separate the two compilation steps, simply write:



        gcc -o program file.c -lm






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 8 '12 at 19:44







        user529758































            up vote
            4
            down vote













            Link with -lm to link with the math library






            share|improve this answer





















            • Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
              – Akash Chandwani
              Nov 27 '16 at 18:06















            up vote
            4
            down vote













            Link with -lm to link with the math library






            share|improve this answer





















            • Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
              – Akash Chandwani
              Nov 27 '16 at 18:06













            up vote
            4
            down vote










            up vote
            4
            down vote









            Link with -lm to link with the math library






            share|improve this answer












            Link with -lm to link with the math library







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 8 '12 at 19:41









            Manuel Selva

            10.3k1863117




            10.3k1863117












            • Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
              – Akash Chandwani
              Nov 27 '16 at 18:06


















            • Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
              – Akash Chandwani
              Nov 27 '16 at 18:06
















            Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
            – Akash Chandwani
            Nov 27 '16 at 18:06




            Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
            – Akash Chandwani
            Nov 27 '16 at 18:06


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8780603%2fundefined-reference-to-sqrt%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values