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


Groups > comp.lang.c > #62558

Re: Need help with an odd error...

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Need help with an odd error...
Date 2015-05-24 18:53 -0400
Organization A noiseless patient Spider
Message-ID <mjtkn3$opd$1@dont-email.me> (permalink)
References <mjtj7h$nlo$1@speranza.aioe.org>

Show all headers | View raw


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

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