Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #62553 > unrolled thread

Need help with an odd error...

Started by"Chris M. Thomasson" <cristom@charter.net>
First post2015-05-24 15:26 -0700
Last post2015-05-27 19:48 +0200
Articles 8 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  Need help with an odd error... "Chris M. Thomasson" <cristom@charter.net> - 2015-05-24 15:26 -0700
    Re: Need help with an odd error... Keith Thompson <kst-u@mib.org> - 2015-05-24 15:38 -0700
    Re: Need help with an odd error... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-24 23:39 +0100
    Re: Need help with an odd error... James Kuyper <jameskuyper@verizon.net> - 2015-05-24 18:53 -0400
      Re: Need help with an odd error... Stephen Sprunk <stephen@sprunk.org> - 2015-05-24 19:15 -0500
    Re: Need help with an odd error... "Chris M. Thomasson" <cristom@charter.net> - 2015-05-24 16:18 -0700
      Re: Need help with an odd error... "Chris M. Thomasson" <cristom@charter.net> - 2015-05-24 16:54 -0700
      Re: Need help with an odd error... Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-27 19:48 +0200

#62553 — Need help with an odd error...

From"Chris M. Thomasson" <cristom@charter.net>
Date2015-05-24 15:26 -0700
SubjectNeed help with an odd error...
Message-ID<mjtj7h$nlo$1@speranza.aioe.org>
Can anybody help me find out why the following program
fails to compile on both:

http://www.tutorialspoint.com/compile_c_online.php

and:

http://codepad.org/AFW3DPDL
____________________________________________________
#include <math.h>
#include <stdio.h>

double
my_cos(double v)
{
    return cos(v);
}

int main()
{
    printf("my_cos(3.14) = %f\r\n", my_cos(3.14));

    return 0;
}
____________________________________________________


I do not know exactly why I am getting the following output:
____________________________________________________
sh-4.3# gcc -o main *.c

/tmp/ccXNUoFK.o: In function `my_cos':

main.c:(.text+0x1b): undefined reference to `cos'

collect2: error: ld returned 1 exit status
____________________________________________________

This compiles fine for me on my gcc 4.8 on Windows.

Humm...

It seems as if the math functions disappear!

Am I doing something totally wrong here?

;^/                                       

[toc] | [next] | [standalone]


#62555

FromKeith Thompson <kst-u@mib.org>
Date2015-05-24 15:38 -0700
Message-ID<87wpzxob2h.fsf@kst-u.example.com>
In reply to#62553
"Chris M. Thomasson" <cristom@charter.net> writes:
[...]
> #include <math.h>
> #include <stdio.h>
>
> double
> my_cos(double v)
> {
>    return cos(v);
> }
>
> int main()
> {
>    printf("my_cos(3.14) = %f\r\n", my_cos(3.14));
>
>    return 0;
> }
[...]
> sh-4.3# gcc -o main *.c
>
> /tmp/ccXNUoFK.o: In function `my_cos':
>
> main.c:(.text+0x1b): undefined reference to `cos'

Compile with:

    gcc -o main *.c -lm

(Prepare for a flame war over whether the need to specify "-lm" when
using the math library is a bug or not.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#62556

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-24 23:39 +0100
Message-ID<87wpzxd2hk.fsf@bsb.me.uk>
In reply to#62553
"Chris M. Thomasson" <cristom@charter.net> writes:

> Can anybody help me find out why the following program
> fails to compile on both:
>
> http://www.tutorialspoint.com/compile_c_online.php
>
> and:
>
> http://codepad.org/AFW3DPDL
> ____________________________________________________
> #include <math.h>
> #include <stdio.h>
>
> double
> my_cos(double v)
> {
>    return cos(v);
> }
>
> int main()
> {
>    printf("my_cos(3.14) = %f\r\n", my_cos(3.14));
>
>    return 0;
> }
> ____________________________________________________
>
>
> I do not know exactly why I am getting the following output:
> ____________________________________________________
> sh-4.3# gcc -o main *.c
>
> /tmp/ccXNUoFK.o: In function `my_cos':
>
> main.c:(.text+0x1b): undefined reference to `cos'
>
> collect2: error: ld returned 1 exit status
> ____________________________________________________

Add -lm to the gcc command line.  The fact that the maths library is not
linked by default is a strange quirk of history.  I'm not sure why it's
stuck around for long, but there it is.

It's a FAQ: http://c-faq.com/fp/libm.html

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#62558

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-05-24 18:53 -0400
Message-ID<mjtkn3$opd$1@dont-email.me>
In reply to#62553
On 05/24/2015 06:26 PM, Chris M. Thomasson wrote:
> Can anybody help me find out why the following program
> fails to compile on both:
> 
> http://www.tutorialspoint.com/compile_c_online.php
> 
> and:
> 
> http://codepad.org/AFW3DPDL
> ____________________________________________________
> #include <math.h>
> #include <stdio.h>
> 
> double
> my_cos(double v)
> {
>     return cos(v);
> }
> 
> int main()
> {
>     printf("my_cos(3.14) = %f\r\n", my_cos(3.14));
> 
>     return 0;
> }
> ____________________________________________________
> 
> 
> I do not know exactly why I am getting the following output:
> ____________________________________________________
> sh-4.3# gcc -o main *.c
> 
> /tmp/ccXNUoFK.o: In function `my_cos':
> 
> main.c:(.text+0x1b): undefined reference to `cos'
> 
> collect2: error: ld returned 1 exit status
> ____________________________________________________
> 
> This compiles fine for me on my gcc 4.8 on Windows.
> 
> Humm...
> 
> It seems as if the math functions disappear!
> 
> Am I doing something totally wrong here?

Most compilers are not fully conforming when invoked in their default
mode - they turn on certain extensions to C, and don't fully support all
of it's features. gcc is one of those, and one of the command line
options needed to make it a fully conforming implementation of C is -lm,
which tells it to link in the C standard math library.
The reason for this is largely historical. When C was first developed,
the time to link a program could be quite large, depending upon the
libraries linked in. The math library was fairly large, and while some
programs make heavy use of it, many other programs make no use of it at
all. To reduce the link times for programs in the second category,
linking in the math library was made optional, and the option was off by
default.
This decision was not made by the developers of gcc;, it predates gcc by
quite a bit. I remember having to do the same thing when I first learned
C, in 1979. gcc just duplicated behavior that was common on many (most?)
C compilers of the time.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#62568

FromStephen Sprunk <stephen@sprunk.org>
Date2015-05-24 19:15 -0500
Message-ID<mjtphn$5rt$1@dont-email.me>
In reply to#62558
On 24-May-15 17:53, James Kuyper wrote:
> When C was first developed, the time to link a program could be quite
> large, depending upon the libraries linked in. The math library was
> fairly large, and while some programs make heavy use of it, many other
> programs make no use of it at all. To reduce the link times for
> programs in the second category, linking in the math library was made
> optional, and the option was off by default.

Linking was typically static back then, so linking in a bunch of unused
math functions might make a binary larger than necessary, on top of
being slower.

A _lot_ of code went to great lengths to avoid anything requiring
floating point since most CPUs of the day didn't have hardware support
for it; linking the math library might also link in a software emulation
layer that was never used, further adding to the bloat.

Also, while _you_ may not call anything in the math library, you almost
certainly call printf(), which does need (parts of) the math library for
handling certain argument types--and that results in dragging in a bunch
of other functions.

The solution was a stripped-down version of printf() in libc and the
full version of printf() in libm, with the latter taking precedence
during the linking process, e.g. via "weak" and "strong" symbols.

It's tempting to stop all this nonsense, but there are _still_ some
embedded systems out there with no FPU and scarce RAM, and most folks
have learned to live with this quirk, so it's not worth changing.

S

-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


#62562

From"Chris M. Thomasson" <cristom@charter.net>
Date2015-05-24 16:18 -0700
Message-ID<mjtm9f$tcv$1@speranza.aioe.org>
In reply to#62553
>"Stefan Ram"  wrote in message 
>news:lm-20150525003454@ram.dialup.fu-berlin.de...

>>"Chris M. Thomasson" <cristom@charter.net> writes:
>>It seems as if the math functions disappear!

>  Your source code seems to be fine,
>  but you need a C implementation!
> [...]

Okay, thank you all wrt the `-lm' flag!   :^D

I  have one more question... Why do you think the following program 
compiles,
links and runs fine, without using `-lm' on:

http://www.tutorialspoint.com/compile_c_online.php
____________________________________________________
#include <math.h>
#include <stdio.h>

int main()
{
    double v = cos(3.14);

    printf("v = %f\r\n", v);

    return 0;
}
____________________________________________________


I found this to be very strange because it works with:

gcc -o main *.c


However, this does not work on codepad:

http://codepad.org/wXFiMoAV


Humm... I must be missing something again!

;^o 

[toc] | [prev] | [next] | [standalone]


#62566

From"Chris M. Thomasson" <cristom@charter.net>
Date2015-05-24 16:54 -0700
Message-ID<mjtobd$1c1$1@speranza.aioe.org>
In reply to#62562
> "Stefan Ram"  wrote in message 
> news:library-20150525012932@ram.dialup.fu-berlin.de...

> > "Chris M. Thomasson" <cristom@charter.net> writes:
> >I  have one more question... Why do you think the following program
> > compiles,
> >links and runs fine, without using `-lm' on:

>   It is possible that in this case the expression
>   »cos(3.14)« is evaluated during translation by the
>   compiler, so that the run-time library is not needed.

This makes perfect sense to me Stefan.

Thanks again!


FWIW, I have not compiled with GCC for
a while now. My work has been on MSVC
for around the past year, so, unfortunately,
I totally forgot about linking with the
math library via `lm'. Now its all coming
back to me. Also, when I noticed this problem,
I failed to think that the IDE I am using
just might be already adding `-lm' to the
command line.

Yikes!

;^|

[toc] | [prev] | [next] | [standalone]


#62686

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2015-05-27 19:48 +0200
Message-ID<mk501i$hrd$1@news.albasani.net>
In reply to#62562
On 25.05.2015 01:29, Stefan Ram wrote:
> "Chris M. Thomasson" <cristom@charter.net> writes:
>> I  have one more question... Why do you think the following program 
>> compiles,
>> links and runs fine, without using `-lm' on:
> 
>   It is possible that in this case the expression
>   »cos(3.14)« is evaluated during translation by the
>   compiler, so that the run-time library is not needed.

Yup, nail/head!

I was actually a bit surprised that this is done even without
optimization enabled:

  400535:       48 b8 d7 f5 19 57 fd    movabs $0xbfeffffd5719f5d7,%rax
  40053c:       ff ef bf
  40053f:       48 89 45 f8             mov    %rax,-0x8(%rbp)
  400543:       48 8b 45 f8             mov    -0x8(%rbp),%rax
  400547:       48 89 45 e8             mov    %rax,-0x18(%rbp)
  40054b:       f2 0f 10 45 e8          movsd  -0x18(%rbp),%xmm0
  400550:       bf f4 05 40 00          mov    $0x4005f4,%edi
  400555:       b8 01 00 00 00          mov    $0x1,%eax
  40055a:       e8 b1 fe ff ff          callq  400410 <printf@plt>

Interesting!

Cheers,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web