How to compile a C program that uses math.h?












11















I am having problems in math.h header file and when I use square root function as in sqrt(d). But my compiler is not supporting this. Please advise me about this problem.



My Ubuntu version is 2012.










share|improve this question




















  • 2





    Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

    – fossfreedom
    Aug 15 '13 at 10:37











  • it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

    – shreya
    Aug 15 '13 at 10:43






  • 1





    please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

    – fossfreedom
    Aug 15 '13 at 10:45






  • 1





    @shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

    – Braiam
    Aug 15 '13 at 11:07






  • 3





    I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

    – Eliah Kagan
    Aug 22 '17 at 11:28
















11















I am having problems in math.h header file and when I use square root function as in sqrt(d). But my compiler is not supporting this. Please advise me about this problem.



My Ubuntu version is 2012.










share|improve this question




















  • 2





    Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

    – fossfreedom
    Aug 15 '13 at 10:37











  • it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

    – shreya
    Aug 15 '13 at 10:43






  • 1





    please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

    – fossfreedom
    Aug 15 '13 at 10:45






  • 1





    @shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

    – Braiam
    Aug 15 '13 at 11:07






  • 3





    I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

    – Eliah Kagan
    Aug 22 '17 at 11:28














11












11








11


6






I am having problems in math.h header file and when I use square root function as in sqrt(d). But my compiler is not supporting this. Please advise me about this problem.



My Ubuntu version is 2012.










share|improve this question
















I am having problems in math.h header file and when I use square root function as in sqrt(d). But my compiler is not supporting this. Please advise me about this problem.



My Ubuntu version is 2012.







compiling gcc c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 15 '13 at 11:19









Eliah Kagan

82.6k22227369




82.6k22227369










asked Aug 15 '13 at 10:34









shreyashreya

78114




78114








  • 2





    Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

    – fossfreedom
    Aug 15 '13 at 10:37











  • it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

    – shreya
    Aug 15 '13 at 10:43






  • 1





    please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

    – fossfreedom
    Aug 15 '13 at 10:45






  • 1





    @shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

    – Braiam
    Aug 15 '13 at 11:07






  • 3





    I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

    – Eliah Kagan
    Aug 22 '17 at 11:28














  • 2





    Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

    – fossfreedom
    Aug 15 '13 at 10:37











  • it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

    – shreya
    Aug 15 '13 at 10:43






  • 1





    please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

    – fossfreedom
    Aug 15 '13 at 10:45






  • 1





    @shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

    – Braiam
    Aug 15 '13 at 11:07






  • 3





    I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

    – Eliah Kagan
    Aug 22 '17 at 11:28








2




2





Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

– fossfreedom
Aug 15 '13 at 10:37





Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?

– fossfreedom
Aug 15 '13 at 10:37













it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

– shreya
Aug 15 '13 at 10:43





it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command

– shreya
Aug 15 '13 at 10:43




1




1





please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

– fossfreedom
Aug 15 '13 at 10:45





please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?

– fossfreedom
Aug 15 '13 at 10:45




1




1





@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

– Braiam
Aug 15 '13 at 11:07





@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.

– Braiam
Aug 15 '13 at 11:07




3




3





I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

– Eliah Kagan
Aug 22 '17 at 11:28





I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use gcc on Ubuntu.

– Eliah Kagan
Aug 22 '17 at 11:28










2 Answers
2






active

oldest

votes


















19














Append -lm to the end of your gcc command.



With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.



If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.



For example: gcc -o foo foo.c -lm






share|improve this answer

































    1














    If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.



    gcc xyz.c -o xyz -lm


    Here,



    gcc is compiler command (compiler name)
    xyz.c is a source file name.
    -o is an option to specify the output file.
    xyz is the name of the output file.
    -lm is an option to link againt the math library (libm).



    for more details here is the link containing complete article on it.
    Compiling C program with math.h in Linux.






    share|improve this answer


























    • After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

      – mousomer
      Sep 29 '16 at 7:02










    protected by Sergiy Kolodyazhnyy Jan 23 at 11:28



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    19














    Append -lm to the end of your gcc command.



    With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.



    If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.



    For example: gcc -o foo foo.c -lm






    share|improve this answer






























      19














      Append -lm to the end of your gcc command.



      With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.



      If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.



      For example: gcc -o foo foo.c -lm






      share|improve this answer




























        19












        19








        19







        Append -lm to the end of your gcc command.



        With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.



        If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.



        For example: gcc -o foo foo.c -lm






        share|improve this answer















        Append -lm to the end of your gcc command.



        With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.



        If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.



        For example: gcc -o foo foo.c -lm







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 22 '17 at 11:50

























        answered Aug 15 '13 at 12:47









        Eliah KaganEliah Kagan

        82.6k22227369




        82.6k22227369

























            1














            If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.



            gcc xyz.c -o xyz -lm


            Here,



            gcc is compiler command (compiler name)
            xyz.c is a source file name.
            -o is an option to specify the output file.
            xyz is the name of the output file.
            -lm is an option to link againt the math library (libm).



            for more details here is the link containing complete article on it.
            Compiling C program with math.h in Linux.






            share|improve this answer


























            • After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

              – mousomer
              Sep 29 '16 at 7:02
















            1














            If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.



            gcc xyz.c -o xyz -lm


            Here,



            gcc is compiler command (compiler name)
            xyz.c is a source file name.
            -o is an option to specify the output file.
            xyz is the name of the output file.
            -lm is an option to link againt the math library (libm).



            for more details here is the link containing complete article on it.
            Compiling C program with math.h in Linux.






            share|improve this answer


























            • After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

              – mousomer
              Sep 29 '16 at 7:02














            1












            1








            1







            If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.



            gcc xyz.c -o xyz -lm


            Here,



            gcc is compiler command (compiler name)
            xyz.c is a source file name.
            -o is an option to specify the output file.
            xyz is the name of the output file.
            -lm is an option to link againt the math library (libm).



            for more details here is the link containing complete article on it.
            Compiling C program with math.h in Linux.






            share|improve this answer















            If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.



            gcc xyz.c -o xyz -lm


            Here,



            gcc is compiler command (compiler name)
            xyz.c is a source file name.
            -o is an option to specify the output file.
            xyz is the name of the output file.
            -lm is an option to link againt the math library (libm).



            for more details here is the link containing complete article on it.
            Compiling C program with math.h in Linux.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 21 '17 at 19:49









            Community

            1




            1










            answered Mar 12 '16 at 18:57









            MikeMike

            372




            372













            • After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

              – mousomer
              Sep 29 '16 at 7:02



















            • After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

              – mousomer
              Sep 29 '16 at 7:02

















            After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

            – mousomer
            Sep 29 '16 at 7:02





            After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).

            – mousomer
            Sep 29 '16 at 7:02





            protected by Sergiy Kolodyazhnyy Jan 23 at 11:28



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents