26

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.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
shreya
  • 313
  • 1
  • 3
  • 4
  • 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](http://sscce.org/) for guidance. – Braiam Aug 15 '13 at 11:07
  • http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-cu – Ciro Santilli OurBigBook.com May 15 '15 at 20:37
  • 5
    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 Answers2

45

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

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
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.

Mike
  • 105
  • 1
  • 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 '16 at 07:02
  • @mousomer how to get it linking automatically [ubuntu 20.04] – mr.loop Jun 03 '21 at 09:26