Thread: Including math.h doesn't seem to work?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Athens, Greece
    Posts
    3

    Including math.h doesn't seem to work?

    I have trouble including a built in library.


    I have this among others on top of a custom library I'm making:
    Code:
    #include <math.h>

    and try to use a couple functions from there to break a multi-digit integer into its digit components:

    Code:
    for ( pwr=(int)log10(intgr); pwr>=0; pwr-- ) 
               {   int tmp = (int)pow((double)10,(double)pwr);
                   add_tail_int(i,(int)(intgr/tmp));
                   intgr = intgr &#37; tmp;
               }
    This is a snip of the code. The rest of the code is irrelevant, including the custom functions I call in this loop. (the custom function add_tail_int() adds the single digit I get from this process into a list).
    (Maybe there is a smarter way to do what I'm trying to get done there, but I'm a beginner).

    I get this error when compiling with gcc:

    Code:
    kimon@CENTURION $ gcc BigIntTest.c Integer_standalone.c
    /tmp/ccAVnqA0.o: In function `kataxorisi':
    Integer_standalone.c:(.text+0x785): undefined reference to `log10'
    Integer_standalone.c:(.text+0x7af): undefined reference to `pow'
    collect2: ld returned 1 exit status
    As if including math.h never happened...
    This is the only error I get.



    I've tried gcc in DevC++ IDE under Vista (in compatibility mode), gcc in SUA (unix emulator) under Win Vista, and also a recent gcc under linux, and they all produce that error.
    The funny thing is that I had been able to compile and run the project in DevC++ under Win XP a while ago, except I no longer have XP, and I don't see why I can't make it work even under the native linux system...


    This is my version of gcc:
    Code:
    kimon@CENTURION $ gcc -v
    Using built-in specs.
    Target: x86_64-suse-linux
    Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.1.2 --enable-ssp --disable-libssp --disable-libgcj --with-slibdir=/lib64 --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --program-suffix=-4.1 --enable-version-specific-runtime-libs --without-system-libunwind --with-cpu=generic --host=x86_64-suse-linux
    Thread model: posix
    gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)
    Last edited by scorpion_gr; 08-07-2007 at 04:27 AM. Reason: adding info

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    link with the math library,
    Code:
    -lm

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    compile with -lm

    it reminds the compiler to link to the maths library

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Athens, Greece
    Posts
    3
    Many thanks guys! You saved me!

    I didn't know the precompiler could "ignore" commands like that...

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the include directive doesn't tell the compiler anything about the library. It's just for that header file.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Athens, Greece
    Posts
    3
    Oh, I see... Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM