problem with round

M

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/manual/html_node/Rounding-Functions.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
 
B

Ben Pfaff

m said:
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/manual/html_node/Rounding-Functions.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.
 
M

m

Ben said:
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
 
B

Ben Pfaff

m said:
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'.
 
M

m

Ben said:
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.
 
J

jacob navia

Ben said:
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
 
M

m

Lawrence said:
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
 
M

m

Ben said:
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.
 
M

m

m said:
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.
 
B

Ben Pfaff

m said:
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.
 
H

HP

Ben Pfaff said:
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
 
K

Keith Thompson

m said:
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.
 
C

CBFalconer

m said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top