1

I'm a complete newbie in C for Linux. I have this function (tested on Visual Studio, it works and it's not using any C++ or Windows-only functions) that returns the amount of digits inside an integer number (note the use of log10() from math.h), but it throws me the error "log10() undefined reference, ld returned 1 exit status". I also have this very exact problem on other function (the same program) that uses pow() (A function to convert binary to decimal, that works everywhere but Ubuntu, just the same as this one).

 int DigitsInNumber(int Num)
 {
     int Digit = Num;
     if (Digit == 0) /* Log10(0) = Math error */
        Digit = 1; 
     else
     {    
         if (Digit < 0) /* Log10(<Negative number>) = Math error */
             Digit = Digit * - 1
         else
             Digit = (int) log10((double) Digit) + 1; /* Log10(250) + 1 = 3 
             (250 has 3 digits) */
     }

     return Digit;
 }

This code was created on Eclipse (Ubuntu) and g++ (as recommended by the teacher), I know probably there are "duplicates" but the related questions I've read recommended the same thing: "Include math.h, that's what you need." (I did that the very minute the function came to my mind) and "Use ld" (Where?) that none of those answers said where to type or do with it. What do I do about "ld"? Thank you.

soulblazer
  • 1,178
  • 7
  • 20
  • 30
  • Probably [need -lm](http://stackoverflow.com/a/19372412/1708801). – Shafik Yaghmour Jun 23 '14 at 02:33
  • 1
    `ld` is the GNU linker. Usually you don't have to invoke it yourself, as GCC does so for you. Just add `-lm` to your GCC (`g++`) command line. – Jonathon Reinhart Jun 23 '14 at 02:35
  • See also: http://stackoverflow.com/questions/1351712/how-to-add-a-library-to-an-eclipse-project – Jonathon Reinhart Jun 23 '14 at 02:36
  • @JonathonReinhart The link was the one that worked, I had no idea where to use the command (good to know for future libraries). The teacher just told us to make a pure C program that simulates the fetch, install Virtual Box, download an Ubuntu iso, install Eclipse and figure it out our way through the exam. Thanks so much. – soulblazer Jun 23 '14 at 03:11
  • It's really best to understand what IDE's like Eclipse are doing under the hood. In this case, that is `-lm`. – Jonathon Reinhart Jun 23 '14 at 03:12

0 Answers0