undefined refrence to a function

B

bobrics

Hi,

I have been trying to compile some source code that is supposed to be
working and getting some compilation errors. The errors are with
sqrt(), log() and round() functions. I have math.h included and made
sure it's in the path. I have read on the forums that some have fixed
the problem by telling GCC to link math libraries if it's not done by
default.

So, using gcc -lm helped to resolve the problems with defining the
first two functions. However, round() is still undefined: "undefined
reference to round()"

Another suggestion that I have found is to check whether the compiler
is using C99 VS C90 library. This is quite stange that I have faced
these problems while using GCC 4.0.2.

What do you think about this? Why is this a problem? As far as I
understood, round() has been defined in C99 and gcc 4.0.2 should have
come with C99 libraries.

Thank you
 
I

Ian Collins

bobrics said:
Hi,

I have been trying to compile some source code that is supposed to be
working and getting some compilation errors. The errors are with
sqrt(), log() and round() functions. I have math.h included and made
sure it's in the path. I have read on the forums that some have fixed
the problem by telling GCC to link math libraries if it's not done by
default.

So, using gcc -lm helped to resolve the problems with defining the
first two functions. However, round() is still undefined: "undefined
reference to round()"

Another suggestion that I have found is to check whether the compiler
is using C99 VS C90 library. This is quite stange that I have faced
these problems while using GCC 4.0.2.

What do you think about this? Why is this a problem? As far as I
understood, round() has been defined in C99 and gcc 4.0.2 should have
come with C99 libraries.
Try a gcc list, round should be there.
 
B

bobrics

What do you mean by gcc list?

By the way, I have tried compiling the file using just command prompt
and not Eclipse build as I was doing before and I've just got the
following errors. Otherwise it works. I think -lm actually has fixed
the problem:

$ sudo gcc -lm my_file.c -o my_file
my_file.c: In function 'my_function':
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'round'
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'roundf'
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'roundl'

How can I make this work in Eclipse now?
I was trying to change the parameters that Eclipse is using to run gcc.
In project.properties.C/C++Build window, I've changed command from gcc
to 'gcc -lt', but I am still getting undefined references. Here are the
values in ALL options text box, which is below command text box: '-O0
-g3 -Wall -c -fmessage-length=0'. It seems like I cannot modify that.

Thank you !
 
K

Keith Thompson

bobrics said:
I have been trying to compile some source code that is supposed to be
working and getting some compilation errors. The errors are with
sqrt(), log() and round() functions. I have math.h included and made
sure it's in the path. I have read on the forums that some have fixed
the problem by telling GCC to link math libraries if it's not done by
default.

So, using gcc -lm helped to resolve the problems with defining the
first two functions. However, round() is still undefined: "undefined
reference to round()"

Another suggestion that I have found is to check whether the compiler
is using C99 VS C90 library. This is quite stange that I have faced
these problems while using GCC 4.0.2.

What do you think about this? Why is this a problem? As far as I
understood, round() has been defined in C99 and gcc 4.0.2 should have
come with C99 libraries.

The round() function is new in C99; it's not in C90.

This is somewhat OT, but gcc typically doesn't come with *any*
library. It uses whatever library is provided by the underlying
system. (On my Solaris 9 box, gcc finds round() but Sun's cc doesn't,
so I'm probably missing something here myself.)
 
K

Keith Thompson

bobrics said:
What do you mean by gcc list?

By the way, I have tried compiling the file using just command prompt
and not Eclipse build as I was doing before and I've just got the
following errors. Otherwise it works. I think -lm actually has fixed
the problem:

$ sudo gcc -lm my_file.c -o my_file
my_file.c: In function 'my_function':
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'round'
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'roundf'
my_file.c:314: warning: incompatible implicit declaration of built-in
function 'roundl'

How can I make this work in Eclipse now?

I have no idea what Eclipse is.

Here's what I *think* is happening.

gcc uses your operating system's libraries and header files. Your
system's <math.h> header doesn't support C99, so it doesn't declare
round(). Your library presumably doesn't provide an implementation of
the function either. But gcc recognizes the round() function (though
it's not required to do so); it might even generate inline code for
the call rather than a function call. Since there is no declaration
of round() in <math.h>, you have a call with no visible prototype; the
compiler is required to assume that it returns int.

Since this invokes undefined behavior, I suppose the compile *could*
generate correct code anyway, but I wouldn't count on it.

Consider using trunc() rather than round().
 
K

Keith Thompson

bobrics said:
What do you mean by gcc list?

By the way, I have tried compiling the file using just command prompt
and not Eclipse build as I was doing before and I've just got the
following errors. Otherwise it works. I think -lm actually has fixed
the problem:
[snip]

I forgot to mention: please don't top-post.

See <http://www.caliburn.nl/topposting.html>.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

bobrics said:
Hi,

I have been trying to compile some source code that is supposed to be
working and getting some compilation errors. The errors are with
sqrt(), log() and round() functions. I have math.h included and made
sure it's in the path. I have read on the forums that some have fixed
the problem by telling GCC to link math libraries if it's not done by
default.

So, using gcc -lm helped to resolve the problems with defining the
first two functions. However, round() is still undefined: "undefined
reference to round()"
Perhaps you havn't included the needed header files. Show some source code.
Another suggestion that I have found is to check whether the compiler
is using C99 VS C90 library. This is quite stange that I have faced
these problems while using GCC 4.0.2.

What do you think about this? Why is this a problem? As far as I
understood, round() has been defined in C99 and gcc 4.0.2 should have
come with C99 libraries.

gcc is just the compiler, the libraries are provided elsewhere.

(OT: to throw gcc in c99 mode, use the -std=c99 flag)
 
B

bobrics

Nils said:
Perhaps you havn't included the needed header files. Show some source code.


gcc is just the compiler, the libraries are provided elsewhere.

(OT: to throw gcc in c99 mode, use the -std=c99 flag)

//#include <iostream.h> // originally made with cpp template
#include <stdio.h>
#include <assert.h>
#include <tgmath.h>
#include <math.h>

#include <stdlib.h>
#include <time.h>
....

double somefunction(double k, double r, ...){
....
j = (int) (round ((double) k / r));
// in for loop:
mu[j - 1] += r * log (r / sig) / (double) k;
... etc..
}


So, when I ran directly from a command line, it works now, when I am
using -lm.
$: sudo gcc -lm mycodefile.c -o mycodefile

However, I don't know where exactly to add -lm option within Eclipse
development environment. I've tried adding it to the command line, so
before it was 'gcc', and now 'gcc -lm', but that haven't solved it.

Thank you
 
K

Keith Thompson

bobrics said:
Nils said:
Perhaps you havn't included the needed header files. Show some source code.


gcc is just the compiler, the libraries are provided elsewhere.

(OT: to throw gcc in c99 mode, use the -std=c99 flag)

//#include <iostream.h> // originally made with cpp template
#include <stdio.h>
#include <assert.h>
#include <tgmath.h>
#include <math.h>

#include <stdlib.h>
#include <time.h>
...

double somefunction(double k, double r, ...){
...
j = (int) (round ((double) k / r));
// in for loop:
mu[j - 1] += r * log (r / sig) / (double) k;
.. etc..
}


So, when I ran directly from a command line, it works now, when I am
using -lm.
$: sudo gcc -lm mycodefile.c -o mycodefile

However, I don't know where exactly to add -lm option within Eclipse
development environment. I've tried adding it to the command line, so
before it was 'gcc', and now 'gcc -lm', but that haven't solved it.

You're casting the result of round() to suppress the compiler's
warning. That's a really bad idea. It's likely that your <math.h>
header doesn't declare the round() function, and that your compiler is
generating code that assumes round() returns an int. It's also
possible that the compiler does the right thing, but you shouldn't
count on it.

If you want a solution that's likely to work for your specific
environment, you can try declaring round() yourself:

double round(double x);

On implementations that provide a round() function and a proper
declaration of it in <math.h>, this isn't necessary. On
implementations that don't provide round(), it won't do any good.

You're dealing with a broken implementation; parts of it support the
new C99 round() function, and parts of it don't. Consider avoiding
the problem altogether by modifying the code to use ceil() and/or
floor(), perhaps wrapping them in your own my_round() function.
 
B

Barry Schwarz

Perhaps you havn't included the needed header files. Show some source code.

The error message is from the linker, not the compiler. Header files
won't solve the problem.


Remove del for email
 
R

Richard Heathfield

bobrics said:
What del are you referring to?

He is referring to three spurious characters inserted into his email
address, confounding spammers, for the purpose of.

Ldiekle this, you see.
 

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
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top