gcc linking fails: undefined reference to sqrt

Bug #1851341 reported by Tim Tei
6
This bug affects 1 person
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;
        printf("%f\n", sqrt(n));
}

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-ubuntu.txt vs ld-parameters-debian.txt).

The first "--as-needed" is causing the problems.

ProblemType: Bug
DistroRelease: Ubuntu 19.10
Package: gcc 4:9.2.1-3.1ubuntu1
ProcVersionSignature: Ubuntu 5.3.0-19.20-generic 5.3.1
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_DIR=<set>
 LANG=de_DE.UTF-8
 SHELL=/bin/bash
SourcePackage: gcc-defaults
UpgradeStatus: Upgraded to eoan on 2019-10-29 (6 days ago)

Revision history for this message
Tim Tei (timtei) wrote :
Revision history for this message
Tim Tei (timtei) wrote :
Revision history for this message
Tim Tei (timtei) wrote :
description: updated
Revision history for this message
Matthias Klose (doko) wrote :

GCC in Ubuntu passes --as-needed by default.

Changed in gcc-defaults (Ubuntu):
status: New → Incomplete
status: Incomplete → Invalid
Revision history for this message
Tim Tei (timtei) wrote :

Matthias can you elaborate on that?

Revision history for this message
Tim Tei (timtei) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.