8

on ubuntu 20.04, when I use clang-8 or clang-9 (clang version 9.0.1-12) to compile a simple code containing reference to libm, it will fail with error "undefined reference to __pow_finite"

#include <math.h>

int main()
{
    double x=1, y=1;
    x = pow(x,y);
}
clang-9 -lm test.c -ffast-math
/usr/bin/ld: /tmp/test-9b1a45.o: in function `main':
test.c:(.text+0x2a): undefined reference to `__pow_finite'

readelf -Ws /lib/x86_64-linux-gnu/libm.so.6| grep pow_finite
   626: 000000000002ed90    65 IFUNC   GLOBAL DEFAULT   17 __pow_finite@GLIBC_2.15

gcc is fine. Any idea what is wrong here?

c++ has the same problem:

#include <cmath>

int main()
{
    double x=1, y=1;
    x = pow(x,y);
}

edit

I actually used -lm, I just forgot to put in the text. If I do not add it, it is another error.

$ clang-9 test.c
/usr/bin/ld: /tmp/test-3389a6.o: in function `main':
test.c:(.text+0x25): undefined reference to `pow'

$ gcc test.c
/usr/bin/ld: /tmp/cc21n4wb.o: in function `main':
test.c:(.text+0x39): undefined reference to `pow'

F31 does not have this problem. godbolt is also fine. It must be something wrong on the system or specific subversion.

So far order does not matter, so I think it is not gcc will not properly include math.h:

clang-9 test.c -ffast-math -lm
/usr/bin/ld: /tmp/test-6dfc29.o: in function `main':
test.c:(.text+0x2a): undefined reference to `__pow_finite'

clang-9 -ffast-math test.c -lm
/usr/bin/ld: /tmp/test-6754bc.o: in function `main':
test.c:(.text+0x2a): undefined reference to `__pow_finite'

change the ld to collect2 has the same problem, so it should not be ld's issue.

clang-9 -v -fuse-ld=/usr/lib/gcc/x86_64-linux-gnu/9/collect2 test.c -ffast-math -lm

update

It seems related to libc update. There is no math-finite.h any more, so when -ffast-math generate __*finite it will fail. clang has to change its behaviour.

MSalters
  • 167,472
  • 9
  • 150
  • 334
Wang
  • 6,129
  • 4
  • 29
  • 53
  • I tried that already. Result is the same. – Wang Jun 11 '20 at 22:20
  • Related, but not a duplicate (as this question seems to deal with GCC: https://stackoverflow.com/q/8671366/10957435 –  Jun 11 '20 at 22:23
  • @NateEldredge unfortunately it is not the case. – Wang Jun 11 '20 at 22:36
  • please noticed, if it is the order issue, it will say: `undefined reference to 'pow'` not `__pow_infinate` – Wang Jun 11 '20 at 22:38
  • Okay, I can reproduce that. – Nate Eldredge Jun 11 '20 at 22:42
  • 2
    I agree that this looks like a bug with clang or glibc or Ubuntu 20.04's combination of them. It works with clang-10 if that is an acceptable alternative. – Nate Eldredge Jun 11 '20 at 22:47
  • unfortunately I have to stick to clang-9 so far. – Wang Jun 11 '20 at 23:23
  • Try running the `gcc` command with `-v`. It should tell you what dirs it searches and what libm it used. Because you're using an explicit version (e.g. `clang-9`), you may be getting the _wrong_ libm [for an older/newer version]. You could also run under `strace`. You may need `-L` and/or `-rpath` options to force linkage to the correct [version specific] libs. You can run `nm` or `readelf/objdump` on the various libm files to see which define the missing `__pow_infinite` symbol – Craig Estey Jun 12 '20 at 00:40
  • @CraigEstey not the case. It turns out clang-9 does not work with new glibc which drops the `math-finite.h` – Wang Jun 12 '20 at 18:06
  • @Wang: Would you like to post an answer? – Nate Eldredge Jun 14 '20 at 17:16

1 Answers1

-1

Add header files when compiling, clang -L/home/xiaokuan/lib/glibc-2.32-install/lib -I/home/xiaokuan/lib/glibc-2.32-install/include -lm -ffast-math a.c

Ivan Rubinson
  • 2,719
  • 4
  • 15
  • 37
kuan
  • 11