math.h functions not recognized when taken with variables as arguments

Bug #898759 reported by Daniel Carrasco Guariento
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
eglibc (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

When using gcc 4.6.1 to compile a program with functions from math.h, the compiler does not recognize the functions and issues the following error message:

undefined reference to `sqrt'

(with sqrt replaced by other functions when applicable) but only if the functions are called with variables as their arguments.

The code below:

#include <stdio.h>
#include <math.h>
int main (){
     long double x, y, z;
     z = 2;
     x = sqrt ((double) z);
     y = sqrtl ((long double) z);
     printf ("%.30Lg\n%.30Lg\n", (long double) x, (long double) y);
     return 0;
}

triggers the bug when compiled with the command-line:

gcc -lm test.c

If I change the argument of the functions, so they read sqrt(2) or sqrtl(2) the compiler does not complain and the program runs correctly with the expected precision for each of the functions.

The bug also happens if the compiler is run with either -std=c99, -std=gnu99 or -ansi in addition to the command-line above.

ProblemType: Bug
DistroRelease: Ubuntu 11.10
Package: libc6-dev 2.13-20ubuntu5
ProcVersionSignature: Ubuntu 3.0.0-13.22-generic 3.0.6
Uname: Linux 3.0.0-13-generic x86_64
ApportVersion: 1.23-0ubuntu4
Architecture: amd64
Date: Thu Dec 1 15:44:53 2011
InstallationMedia: Ubuntu 11.10 "Oneiric Ocelot" - Release amd64 (20111012)
ProcEnviron:
 LANGUAGE=pt_BR:pt:en
 PATH=(custom, no user)
 LANG=pt_BR.UTF-8
 SHELL=/bin/bash
SourcePackage: eglibc
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Daniel Carrasco Guariento (sirwhiteout) wrote :
Revision history for this message
Jason Conti (jconti) wrote :

In oneiric you need to specify the libraries after the code due to changes in http://wiki.debian.org/ToolChain/DSOLinking

gcc test.c -lm

GCC also has builtin versions of sqrt which are folded in when the value of sqrt is a constant, so this is why you notice the differing behavior between variable and constant values. With the constant version compiling with:

gcc -fno-builtin -lm test.c

will give the same error.

Changed in eglibc (Ubuntu):
status: New → Invalid
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.