3

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)?

  • 1
    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 01 '11 at 14:13
  • The man page for sqrtl says: use cc -std=c99. – aquaherd Dec 01 '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 01 '11 at 19:20

1 Answers1

1

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.

  • 1
    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 07 '11 at 09:21