C program fails to link with math library

Peter Teuben teuben at gmail.com
Tue Dec 29 01:12:52 UTC 2020


some linkers are not smart, so put it behind.

But also, please use the -o flag to give it a real name. otherwise it's 
call a.out (don't ask), so


     gcc test.c -o test -lm

would be my recommendation, but if you like less typing, the -o is 
optional (but what if you have 2 tests :-)



On 12/28/20 8:01 PM, Stuart McGraw wrote:
> It's been many years since I've done anything with C but I am trying
> to recompile an old C program on Ubuntu-18.04 and am having a problem
> with libraries.
>
> When I try to compile this program, test.c:
>
>   #include <math.h>
>   int main (int argc, char **argv) {
>       double a=1.0, b;
>       b = sin (a); }
>
> with the command:
>
>   gcc -lm test.c
>
> I get errors:
>
>   /tmp/ccdASNYu.o: In function `main':
>   test.c:(.text+0x2a): undefined reference to `sin'
>   collect2: error: ld returned 1 exit status
>
> The above program and command are (simplified versions for exposition
> of) what is used in my Makefile and worked six or so years ago on a
> Fedora system.  As far as I know I've done no messing with C or
> linker configuration and have no weird environment variables that
> would affect them.  Isn't libm a standard library that shouldn't
> require any magic beyond "-lm" to use?
>
> What am I doing wrong?
>




More information about the ubuntu-users mailing list