[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: gcc and math.h



On Mon, Mar 22, 2004 at 09:28:24AM +0800, Didier Casse wrote:
> On 21/03/04, at 18:36 -0600, stucklenp <stucklenp charter net> wrote:
> > 
> > I routinely program using C and compile with gcc.  My problem is that I need
> > some math log functions--ln, log10, and log2--and these functions are not
> > included in math.h from what I can determine.  Can anyone suggest how I can
> > get these functions?
> 
> All the functions that you're saying are included. Have a look here for
> math.h fucntions:
> 
> http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html
> 
> Do a "man function" to find out more about the individual functions. e.g
> "man log".
> 
> My guess is that you haven't linked your source code to the math
> library. (-lm option)
> 
> i.e 
> 
> gcc -lm myfile.c -o myfile


Yes... -lm  is needed by the link editor (ld).
    $ cc t.c
    /tmp/ccISAzDN.o(.text+0x23): In function `main':
    : undefined reference to `log'
    collect2: ld returned 1 exit status
 and 
    $ cc t.c -lm  
returns success and a.out does the expected.
    $ a.out
    log(10.0) is 2.302585
    $ grep 2.302585 /usr/include/math.h
    # define M_LN10         2.30258509299404568402  /* log_e 10 */

I wondered how one could discover this if it was unknown...

The "man log" command found the man page for log()/ EXP(3)
The 3 tells me it is in section three of the man pages.

Someone once told me that the "intro" man pages were worth reading and
full of nifty stuff.  I tried "man 3 intro" and found a clue.

As I scanned down

       (3M)   These  functions  are  contained in the arithmetic library libm.
              They are used by the f77(1) FORTRAN compiler by default, but not
              by the cc(1) C compiler, which needs the option -lm.

So for a "C" program -lm is the magic compile line addition.

And that is good to know because,
I really do not want to resort to the silly and brutal:

   (for i in /usr/lib/*.so*    # might need to do a find...
   do 
   echo ================================ 
   echo $i
   nm $i | grep log$
   echo ----------------------
   done)| less

to find the symbol the hard way in a nasty long list of junk.

    ================================
    /usr/lib/libm.so
    0000b020 W clog
    0000b020 t __clog
    00007850 t __ieee754_log
    00009fa0 W log                             <-----found it.
    00009fa0 t __log
    ----------------------


I guess the intro man pages are worth a second/third  look.

  ;-)

Summary:
  Good question, the answer is not obvious if you don't know it.


-- 
	T o m  M i t c h e l l 
	/dev/null the ultimate in secure storage.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]