C program fails to link with math library

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

C program fails to link with math library

Stuart McGraw
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
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: C program fails to link with math library

Peter Teuben-2
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
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: C program fails to link with math library

Stuart McGraw
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
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: C program fails to link with math library

Peter Teuben-2
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 <[hidden email]> 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
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: C program fails to link with math library

Michael Hannon
In reply to this post by Stuart McGraw
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 <[hidden email]> 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
> [hidden email]
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users