C program fails to link with math library
Michael Hannon
jmhannon.ucdavis at gmail.com
Tue Dec 29 04:11:49 UTC 2020
I think the linker is doing just what it's supposed to do. IIRC, the
linker is looking "downstream", i.e., to the right on the command
line. in order to resolve links. This gives you some control over
which version of a function "foo" is being linked into your
executable, i.e., in case a function by that name appears in multiple
source files. I haven't done much with this kind of thing lately, so
if my interpretation is outdated, I'd appreciate hearing about it.
-- Mike
On Mon, Dec 28, 2020 at 6:10 PM Stuart McGraw <smcg4191 at mtneva.com> wrote:
>
> Well, that was simple! :-) Thanks so much, I'm embarrassed to say how long it probably would have taken to figure that out given a life long habit of (almost) always putting options in front of arguments. And, yup, I do use -o in the Makefile, I just tried to maximally simplify for posting. Thanks.
>
> On 12/28/20 6:12 PM, Peter Teuben wrote:
> > 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?
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
More information about the ubuntu-users
mailing list