Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having a hard time getting Ubuntu's gcc to understand the long double-manipulating functions from math.h, namely sqrtl, cabsl and cexpl. The message I get for all of them when compiling is

undefined reference to `sqrtl'

Is there any flag other than -lm that I have to put on the command line for gcc to understand these functions? Is it a missing package? Or is this a problem with the version I'm using (4.6.1 on Oneiric)?

share|improve this question
Do you have libc6-dev? All I can say is this works here: #include <math.h> \n int main() { sqrtl(1); } .. cc test.c -o test - I don't even need -lm, it figures it out. – Caesium Nov 30 '11 at 15:02
Yes, I have it. And the result is the same whether I use -lm or not. – Sir Whiteout Nov 30 '11 at 15:34
I just tested the above code and tweaked it while trying to pinpoint the problem. It indeed works in the original form, but if I declare a long double x; variable and assign a value to it, then call sqrtl (x); the problem I reported appears. – Sir Whiteout Dec 1 '11 at 14:13
The man page for sqrtl says: use cc -std=c99. – aquaherd Dec 1 '11 at 18:32
I tried that, with the same result. I have reversed to gcc 4.4 and the problem disappeared, which makes me think it's a bug. – Sir Whiteout Dec 1 '11 at 19:20
up vote 1 down vote accepted

The problem is with the order the arguments are passed to the compiler.

On Oneiric, the linked libraries have to be called after the name of the source file:

gcc test.c -lm

This change and the reasons for it are described in more detail here.

The problem does not appear for functions of constant values because gcc has built-in versions of them. So compiling with -fno-builtin gives the same error as with a variable in the argument.

Thanks for Jason Conti for this information.

share|improve this answer
That's not a new requirement, although I'm willing to believe some toolchain versions had a bug that hid it. Link order has always been critical, and has always been a source of user confusion. – ams Dec 7 '11 at 9:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.