27

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.

6
  • 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, 2013 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, 2013 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, 2013 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, 2013 at 11:07
  • 6
    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. Aug 22, 2017 at 11:28

2 Answers 2

46

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

1
  • Thanks for the tip dude! Aug 8, 2022 at 16:53
7

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.

2
  • 1
    After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
    – mousomer
    Sep 29, 2016 at 7:02
  • @mousomer how to get it linking automatically [ubuntu 20.04]
    – mr.loop
    Jun 3, 2021 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .