C program fails to link with math library
Peter Teuben
teuben at gmail.com
Tue Dec 29 02:51:36 UTC 2020
Well, my own bug of the day was the source command in bash. It doesn't
exist in sh. And in a makefile the default shell is sh. This was a deeply
nested installation procedure that took me two hours to uncover. The
solution was to add a line
SHELL = /bin/bash
To the Makefile ;-(
On Mon, Dec 28, 2020, 21:10 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20201228/1086c4d7/attachment.html>
More information about the ubuntu-users
mailing list