math.h functions not recognized when taken with variables as arguments
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\
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
ProcVersionSign
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=
PATH=(custom, no user)
LANG=pt_BR.UTF-8
SHELL=/bin/bash
SourcePackage: eglibc
UpgradeStatus: No upgrade log present (probably fresh install)
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.