C program fails to link with math library

Stuart McGraw smcg4191 at mtneva.com
Tue Dec 29 01:01:28 UTC 2020


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