gcc linking fails: undefined reference to sqrt
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-defaults (Ubuntu) |
Invalid
|
Undecided
|
Unassigned |
Bug Description
The code I try to compile and link:
$ cat s.c
#include <stdio.h>
#include <math.h>
int main()
{
double n = 5.0;
}
Linking fails with gcc, gcc-7, gcc-8, gcc-9:
$ gcc -lm s.c
/usr/bin/ld: /tmp/ccBhhS0F.o: in function `main':
s.c:(.text+0x23): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Linking succeeds with g++, clang:
$ g++ -lm s.c
$ clang -lm s.c
Tested with Ubuntu 18.04 and 19.10.
On Debian linking with gcc works fine.
The parameters passed to ld differ in Debian and Ubuntu
(see ld-parameters-
The first "--as-needed" is causing the problems.
ProblemType: Bug
DistroRelease: Ubuntu 19.10
Package: gcc 4:9.2.1-3.1ubuntu1
ProcVersionSign
Uname: Linux 5.3.0-19-generic x86_64
ApportVersion: 2.20.11-0ubuntu8.2
Architecture: amd64
CurrentDesktop: ubuntu:GNOME
Date: Tue Nov 5 08:46:14 2019
ProcEnviron:
LANGUAGE=de_DE:
PATH=(custom, no user)
XDG_RUNTIME_
LANG=de_DE.UTF-8
SHELL=/bin/bash
SourcePackage: gcc-defaults
UpgradeStatus: Upgraded to eoan on 2019-10-29 (6 days ago)
GCC in Ubuntu passes --as-needed by default.