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


Groups > comp.lang.c > #62556

Re: Need help with an odd error...

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Need help with an odd error...
Date 2015-05-24 23:39 +0100
Organization A noiseless patient Spider
Message-ID <87wpzxd2hk.fsf@bsb.me.uk> (permalink)
References <mjtj7h$nlo$1@speranza.aioe.org>

Show all headers | View raw


"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.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web