By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

problem with round

P: n/a
m
all,
i am trying to use the function round() which I found through google
to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html).
this function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.
is round() not found in all math.h files? if so what might be an
alternative. i work on a machine that runs a sparc-sun-solaris operating
system with gcc version 2.95.2.
thanks
Nov 14 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
m <ma**@mschumacher.com> writes:
i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html). this
function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.


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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #2

P: n/a
m
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html). this
function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.

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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.

sorry for not having read the FAQ first. thanks for the reply. i worked
around that using
#define round(x) ((x)>=0)?(int)((x)+0.5):(int)((x)-0.5)
am new to C but seems mighty strange.this kind of linking just beats
using something like a header file. i am sure i am missing something.

thanks
Nov 14 '05 #3

P: n/a
m <ma**@mschumacher.com> writes:
sorry for not having read the FAQ first. thanks for the reply. i
worked around that using
#define round(x) ((x)>=0)?(int)((x)+0.5):(int)((x)-0.5)


For what it's worth, that should work okay, but only if `x'
rounds to a value in the range of `int'.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #4

P: n/a
m
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

sorry for not having read the FAQ first. thanks for the reply. i
worked around that using
#define round(x) ((x)>=0)?(int)((x)+0.5):(int)((x)-0.5)

For what it's worth, that should work okay, but only if `x'
rounds to a value in the range of `int'.

yes its a pgm image that "should" contain values in 0 to 255.

Nov 14 '05 #5

P: n/a
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html). this
function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.

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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.


Open letter to the gcc developers:

Dear Sirs:

Subject:

Gcc should link with the math library by default.

Rationale:
In comp.lang.c we receive since many many years the
same question posed always by beginners:

Why is my "sin" not linking? (Or "cos" whatever)

Most compilers now include a default library set
that includes the C run time, to which the math
library belongs.

The math library can be left out if you disable
this, what the 0.000000001% of users writing new
replacement math libraries for gcc will appreciate.

Implementation:
When parsing command line arguments, gcc searches for
--no_math_library. If found, the math library will
be deleted from the default library list. An argument
of --no_default_libs (or similar) could be used
for self-contained programs that do not use
any external library.

The default library list has two members at least:

The C run time library
The math library.

Other libraries could be specified somehow by
the user to avoid remembering to include them,
but at least those two should belong to the
default setting.

Rationale:

Beginners do not grasp the need to add a library,
and many do not know at all what a library is.

It is better to leave learning this fact after you
have done your first program.

Dennis Ritchie proposed a # preprocessor directive
in his Plan9 system to force the inclusion of the
library when you include the header file.

Lcc-win32 implements this with
#pragma lib "Absolute path"
#pragma lib <standard lib path>

This improves the language since it is no longer
necessary to figure out which library is used
with which header file. They are documented in
the header file with those constructs.

But I disgress

This is easy to do. Just a small change in the
gcc parsing routine, and a flag that saves the
info when generating the linker command line,
that is decorated automatically with

"-lm"

BY DEFAULT :-)

This will save thousands of questions from
the beginners. Yes, you are advanced and
you wrote a compiler, but some day you were a
plain beginner and didn't even know that
you had to write that, isn't it?

Just add it up and make your system more
beginner friendly.

jacob
Nov 14 '05 #6

P: n/a
On Thu, 20 Jan 2005 12:55:52 -0600, m wrote:
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

sorry for not having read the FAQ first. thanks for the reply. i
worked around that using
#define round(x) ((x)>=0)?(int)((x)+0.5):(int)((x)-0.5)

For what it's worth, that should work okay, but only if `x'
rounds to a value in the range of `int'.

yes its a pgm image that "should" contain values in 0 to 255.


Why is it testing for and rounding negative values?

Lawrence
Nov 14 '05 #7

P: n/a
m
m wrote:
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html).
this
function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.


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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.

i did gcc test.c -lm (wrote a program gcc which included math.h with
log2 and round in it) it jst gave me the same errors. unreferenced symbol.


shudd read -wrote a program and then did gcc.
Nov 14 '05 #8

P: n/a
m
Lawrence Kirby wrote:
On Thu, 20 Jan 2005 12:55:52 -0600, m wrote:

Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

sorry for not having read the FAQ first. thanks for the reply. i
worked around that using
#define round(x) ((x)>=0)?(int)((x)+0.5):(int)((x)-0.5)
For what it's worth, that should work okay, but only if `x'
rounds to a value in the range of `int'.


yes its a pgm image that "should" contain values in 0 to 255.

Why is it testing for and rounding negative values?

Lawrence


its testing and rouding all values and it does it because the
calculations are in double. round and then clip between 0 and 255 if needed
Nov 14 '05 #9

P: n/a
m
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:

i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html). this
function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.

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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.


i did gcc test.c -lm (wrote a program gcc which included math.h with
log2 and round in it) it jst gave me the same errors. unreferenced symbol.
Nov 14 '05 #10

P: n/a
m <ma**@mschumacher.com> writes:
i did gcc test.c -lm (wrote a program gcc which included math.h with
log2 and round in it) it jst gave me the same errors. unreferenced
symbol.


round() was introduced in C99 if I recall correctly. Your C
library may not include it.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #11

P: n/a
HP
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message news:87************@benpfaff.org...
m <ma**@mschumacher.com> writes:

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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.
--

This is offtopic here - this is a group only for the discussion of the C
language.

H
Nov 14 '05 #12

P: n/a
m <ma**@mschumacher.com> writes:
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:
i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html).
this function does not work and i get an unreferenced symbol error. the
function rint() works when math.h is included.

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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.


i did gcc test.c -lm (wrote a program gcc which included math.h with
log2 and round in it) it jst gave me the same errors. unreferenced
symbol.


If it's the "-lm" problem, I'd expect the error message to refer to an
undefined symbol, not an unreferenced symbol. Then again, an
unreferenced symbol shouldn't be a fatal error. If you have a problem
like this, it's very helpful to post the *exact* error message.

But the real problem, I think, is that your C library doesn't provide
the round() function, which is new in C99. The C90 standard has no
round() function.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13

P: n/a
m wrote:
Ben Pfaff wrote:
m <ma**@mschumacher.com> writes:
i am trying to use the function round() which I found through
google to be declared in math.h (
http://www.gnu.org/software/libc/man...unctions.html).
this function does not work and i get an unreferenced symbol
error. the function rint() works when math.h is included.


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
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.


i did gcc test.c -lm (wrote a program gcc which included math.h
with log2 and round in it) it jst gave me the same errors.
unreferenced symbol.


log2 is only available in C99. Do you have a C99 library?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #14

P: n/a
m
CBFalconer wrote:
log2 is only available in C99. Do you have a C99 library?


i have determined that i dont have a c99 lib. hence am just doing a
log(x)/log(2)

Nov 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.