Velocity Reviews > ceil() and log10() - undefined?

# ceil() and log10() - undefined?

ptn
Guest
Posts: n/a

 10-03-2007
Hi everyone,

I was messing around with math.h and I got this error:

"""
/tmp/ccefZYYN.o: In function `digcount':
itos.c.text+0x103): undefined reference to `log10'
itos.c.text+0x111): undefined reference to `ceil'
collect2: ld returned 1 exit status

shell returned 1
"""

It can't be that log10() and ceil() aren't defined, because math.h
takes care of that. I admit that I have no idea what .text+0x111/.text
+0x103 means

The complete code is here:

# include <stdio.h>
# include <malloc.h>
# include <math.h>

int digcount(int num); // counts the digits of <num>

main()
// transform a number to a string
{
int num, fact, dig, digits, i;
char *s;

scanf("%d", &num);
digits = digcount(num);
s = (char *) malloc((digits + 1) * sizeof(char)); // assign just
enough space
s += (digits - 1); // go to the last address of the memory space
assigned
*s-- = '\0'; // mark the end of the string
for (i = 0; i < digits; i++) {
dig = num % 10;
num /= 10;
*s-- = '0' + dig; // store digit by digit
}
printf("\"%s\"\n", ++s); // after the for, s is pointing to the
addres before the start of the string
}

int digcount(int num)
/* N = trunc(log(x), 0) + 1, where N is the number of digits of x */
{
double ans;

ans = log10(num);
ans = ceil(ans);
return (int) ans;
}

Any ideas are welcome.
(I'm using Vim + gcc under Ubuntu Feisty Fawn)

Thanks,

Pablo Torres Navarrete

Peter Nilsson
Guest
Posts: n/a

 10-03-2007
ptn <(E-Mail Removed)> wrote:
> /tmp/ccefZYYN.o: In function `digcount':
> itos.c.text+0x103): undefined reference to `log10'
> itos.c.text+0x111): undefined reference to `ceil'
> collect2: ld returned 1 exit status
>
> It can't be that log10() and ceil() aren't defined, because
> math.h takes care of that.

For the compiler perhaps, but not for the linker.

See the FAQ: http://c-faq.com/fp/libm.html

--
Peter

Ben Pfaff
Guest
Posts: n/a

 10-03-2007
ptn <(E-Mail Removed)> writes:

> I was messing around with math.h and I got this error:
>
> """
> /tmp/ccefZYYN.o: In function `digcount':
> itos.c.text+0x103): undefined reference to `log10'
> itos.c.text+0x111): undefined reference to `ceil'
> collect2: ld returned 1 exit status
>
> shell returned 1
> """

This is in the C FAQ.

14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.

A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
questions 13.25, 13.26, and 14.2.

--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms

ptn
Guest
Posts: n/a

 10-03-2007
On Oct 2, 11:57 pm, Ben Pfaff <(E-Mail Removed)> wrote:
> ptn <(E-Mail Removed)> writes:
> > I was messing around with math.h and I got this error:

>
> > """
> > /tmp/ccefZYYN.o: In function `digcount':
> > itos.c.text+0x103): undefined reference to `log10'
> > itos.c.text+0x111): undefined reference to `ceil'
> > collect2: ld returned 1 exit status

>
> > shell returned 1
> > """

>
> This is in the C FAQ.
>
> 14.3: I'm trying to do some simple trig, and I am #including <math.h>,
> but I keep getting "undefined: sin" compilation errors.
>
> A: Make sure you're actually linking with the math library. For
> instance, under Unix, you usually need to use the -lm option, at
> questions 13.25, 13.26, and 14.2.
>
> --
> "Large amounts of money tend to quench any scruples I might be having."
> -- Stephan Wilms

Thank you very much. Program runs OK now

ptn

Martin Ambuhl
Guest
Posts: n/a

 10-03-2007
ptn wrote:
> Hi everyone,
>
> I was messing around with math.h and I got this error:
>
> """
> /tmp/ccefZYYN.o: In function `digcount':
> itos.c.text+0x103): undefined reference to `log10'
> itos.c.text+0x111): undefined reference to `ceil'
> collect2: ld returned 1 exit status
>
> shell returned 1
> """
>
> It can't be that log10() and ceil() aren't defined, because math.h
> takes care of that.

No, it doesn't.
You want to get the FAQ <http://c-faq.com/index.html> and check it
before posting any more questions. Most beginner's questions have been
asked many times before and answered many times before. If you check
the FAQ before posting, you will avoid the ire of people who get very
tired of this. In your case, you have asked a form of the question
found, along with the answer, at <http://c-faq.com/fp/libm.html>,
"Question 14.3, Q: I'm trying to do some simple trig, and I am
#including <math.h>, but the linker keeps complaining that functions
like sin and cos are undefined."

BTW, not all of us agree with the word "bug" given in the answer.

Martin Ambuhl
Guest
Posts: n/a

 10-03-2007
ptn wrote:
[...]
> # include <malloc.h>

I forgot to mention that there is no such standard header. Include
<stdlib.h> for malloc() and friends.

CBFalconer
Guest
Posts: n/a

 10-04-2007
ptn wrote:
>

.... snip ...
>
> The complete code is here:
>
> # include <stdio.h>
> # include <malloc.h>

No such standard include file exists. Probably should be stdlib.h.

> # include <math.h>
>
> int digcount(int num); // counts the digits of <num>
>
> main()

Main returns an int. Say so. Use "int main(void)".

> // transform a number to a string

C89 does not allow // comments, nor does Usenet, because of line
wrapping. Use /* ... */

> {
> int num, fact, dig, digits, i;
> char *s;
>
> scanf("%d", &num);

NEVER use scanf without checking its error return. This should be
"if (1 != scanf("5d", & num)) /* recover */ else /* OK */

> digits = digcount(num);
> s = (char *) malloc((digits + 1) * sizeof(char)); // assign just

Never cast the return from malloc. sizeof(char) is one.

> enough space

This is a syntax error!!

> s += (digits - 1); // go to the last address of the memory space
> assigned

So is this !!

> *s-- = '\0'; // mark the end of the string
> for (i = 0; i < digits; i++) {
> dig = num % 10;
> num /= 10;
> *s-- = '0' + dig; // store digit by digit
> }
> printf("\"%s\"\n", ++s); // after the for, s is pointing to the
> addres before the start of the string

Another syntax error here. Then you have failed to return 0,
EXIT_FAILURE, or EXIT_SUCCESS from main. The EXITs are found in
stdlib.h.

> }
>
> int digcount(int num)
> /* N = trunc(log(x), 0) + 1, where N is the number of digits of x */
> {
> double ans;
>
> ans = log10(num);
> ans = ceil(ans);
> return (int) ans;

This is a useless cast. Just eliminate it. In fact you can use:

return ceil(log10(num));

and the default conversions will handle everything. All casts are
suspicious and probably erroneous and/or unneeded, except for

> }
>
> Any ideas are welcome.

I suggest you first fix the above errors/oversights, and limit the
line length to 72 chars. The best way to handle malloc's is:

if (!(p = malloc(N * sizeof *p))) /* recovery code */;
else /* successful malloc, carry on */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

ptn
Guest
Posts: n/a

 10-04-2007
> > }
>
> > Any ideas are welcome.

>
> I suggest you first fix the above errors/oversights, and limit the
> line length to 72 chars. The best way to handle malloc's is:
>
> if (!(p = malloc(N * sizeof *p))) /* recovery code */;
> else /* successful malloc, carry on */

You have given me a lot more information than what I'd expected. I
learned a lot

ptn

santosh
Guest
Posts: n/a

 10-04-2007
ptn wrote:

[CBFalconer wrote the text below]
>>
>> > Any ideas are welcome.

>>
>> I suggest you first fix the above errors/oversights, and limit the
>> line length to 72 chars. The best way to handle malloc's is:
>>
>> if (!(p = malloc(N * sizeof *p))) /* recovery code */;
>> else /* successful malloc, carry on */

>
> You have given me a lot more information than what I'd expected. I
> learned a lot

wrote:"

Removing them makes the discussion hard to follow for those without threaded

Richard Heathfield
Guest
Posts: n/a

 10-04-2007
santosh said:

> ptn wrote:
>
> [CBFalconer wrote the text below]
>>>
>>> > Any ideas are welcome.

....but Chuck didn't write that bit.

>>>
>>> I suggest you first fix the above errors/oversights, and limit the
>>> line length to 72 chars. The best way to handle malloc's is:
>>>
>>> if (!(p = malloc(N * sizeof *p))) /* recovery code */;
>>> else /* successful malloc, carry on */

>>
>> You have given me a lot more information than what I'd expected. I
>> learned a lot

>
> "so-and-so wrote:"
>
> Removing them makes the discussion hard to follow for those without

Even so, ptn should be applauded for paying attention to Chuck's points
even though they were not directly related to the question he actually
general, rather than "enough to get me over *this* hill". I wish there
were more like ptn.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999