#include <math.h>

D

Darius Fatakia

Hi,

I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They're the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to 'pow'" error. Here is the
relevant portion of my code.

Your help would be appreciated. Thanks!

* Genetic Algorithm module
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "string.h"


/* GetSD
*
* This function returns the standard deviation of the feature vectors
* in a dataT * array.
*/
static double GetSD(dataT *dataArr[], int data_size, int features[], double
mean[]) {
int i, j;
double data_vect[GENES_PER_CHROM], data_sum, mean_sum, ang_sum, dotprod,
data_norm, mean_norm, angle;
double sd;

ang_sum = 0;

for(i = 0; i < data_size; i++) {
for(j = 0; j < GENES_PER_CHROM; j++) {
data_vect[j] = dataArr->val[(features[j])];
}

// get dot product of the two vectors
dotprod = 0;
for(j = 0; j < GENES_PER_CHROM; j++) {
dotprod += data_vect[j] * mean[j];
}

// get the norms
data_sum = 0;
mean_sum = 0;
for(j = 0; j < GENES_PER_CHROM; j++) {
data_sum += pow(data_vect[j], 2);
mean_sum += pow(mean[j], 2);
}
data_norm = sqrt(data_sum);
mean_norm = sqrt(mean_sum);

// compute the angle
angle = acos(dotprod/(data_norm * mean_norm));

ang_sum += pow(angle, 2);
}

sd = sqrt(ang_sum / (data_size - 1));

return sd;
}
 
A

Alex

Darius Fatakia said:
I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They're the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to 'pow'" error. Here is the
relevant portion of my code.

You are most likely having a linking problem. Read your system
documentation to find out how to link in the math library.

<OT> Appending -lm to your compile line might work. </OT>

Alex
 
E

E. Robert Tisdale

Darius said:
I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They're the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to 'pow'" error. Here is the
relevant portion of my code.
> cat gam.c
typedef struct dataT {
double* val;
} dataT;

#define GENES_PER_CHROM 1024

/*
* Genetic Algorithm module
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "string.h"


/* GetSD
*
* This function returns the standard deviation
* of the feature vectors in a dataT* array.
*/

static
double GetSD(dataT* dataArr[],
int data_size, int features[], double mean[]) {
double data_vect[GENES_PER_CHROM];
double ang_sum = 0.0;

for(int i = 0; i < data_size; ++i) {
for(int j = 0; j < GENES_PER_CHROM; ++j) {
data_vect[j] = dataArr->val[(features[j])];
}

// get dot product of the two vectors
double dotprod = 0.0;
for(int j = 0; j < GENES_PER_CHROM; ++j) {
dotprod += data_vect[j]*mean[j];
}

// get the norms
double data_sum = 0.0;
double mean_sum = 0.0;
for(int j = 0; j < GENES_PER_CHROM; ++j) {
data_sum += pow(data_vect[j], 2);
mean_sum += pow(mean[j], 2);
}
double data_norm = sqrt(data_sum);
double mean_norm = sqrt(mean_sum);

// compute the angle
double angle = acos(dotprod/(data_norm*mean_norm));
ang_sum += pow(angle, 2);
}
return sqrt(ang_sum/(data_size - 1));
}

int main(int argc, char* argv[]) {
dataT* dataArr[GENES_PER_CHROM];
int data_size = 1024;
int features[1024];
double mean[GENES_PER_CHROM];
GetSD(dataArr, data_size, features, mean);

return 0;
}
> gcc -Wall -std=c99 -pedantic -o gam gam.c -lm

If you include math.h, you need to link in libm.a with the -lm option.
 
K

Kelsey Bjarnason

Hi,

I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They're the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to 'pow'" error. Here is the
relevant portion of my code.

Sounds like a linker error - are you linking the math libraries? There's
a certain, popular compiler, especially widely used in the Linux world,
which has the terminally brain-dead habit of not including the math
libraries by default. Assuming you're using this compiler, try something
like this:

gcc -lm file.c
 
J

jacob navia

I wonder how many HUNDREDS of times I have seen this question
pop up.

Are there any gcc fans out here?

Wouldn't it be time to include that library as a default library????

eh? !!!

This bug is similar to the make utility bug. Tabs are used as
separator
in "make", and the makefile will not work if they are substituted by
spaces!

This makes every novice spend hours trying to find out why two
makefiles that look the
same do not work.
 
D

Dan Pop

In said:
Sounds like a linker error - are you linking the math libraries? There's
a certain, popular compiler, especially widely used in the Linux world,
which has the terminally brain-dead habit of not including the math
libraries by default. Assuming you're using this compiler, try something
like this:

gcc -lm file.c

gcc merely follows the common Unix convention, established by a certain
Dennis M. Ritchie.

Dan
 
D

Dan Pop

In said:
I wonder how many HUNDREDS of times I have seen this question
pop up.

Are there any gcc fans out here?

Why should gcc behave differently than most other Unix compilers?
Wouldn't it be time to include that library as a default library????

Nope, but it would be high time to include the contents of libm.a into
libc.a and provide an empty libm.a, for backward compatibility purposes.
The *good* reasons for keeping it separate have no longer been valid for
the last 15 years or so.

Which has precisely zilch to do with gcc or any other Unix compiler.

Dan
 
J

Joe Wright

jacob said:
[ snip ]
This bug is similar to the make utility bug. Tabs are used as
separator
in "make", and the makefile will not work if they are substituted by
spaces!

This makes every novice spend hours trying to find out why two
makefiles that look the
same do not work.

I'm not sure I agree that it's a bug but it is annoying. make simply
uses TAB as a separator between the Rule and the Command portions of a
statement. As I use edit.com with tabs set to 3 this annoyed the hell
out of me too. Reading the info file on make one day (I'm surprised how
little I do this and how much I should) I read that ';' semicolon is
also a Rule / Command separator. How 'bout this..

# Use GNU make to build an executable for testing GE.

cc=gcc
obj=ge.o main.o
exe=main.exe
opt=-W -Wall -ansi -pedantic -O2 -c

# Note that semicolon (not TAB) separates Rule from Command on a line.

$(exe) : $(obj) ; $(cc) -s $(obj) -o $(exe)
ge.o : ge.c ; $(cc) $(opt) ge.c
main.o : main.c ge.h ; $(cc) $(opt) main.c

Live and learn.
 
D

Dan Pop

In said:
out of me too. Reading the info file on make one day (I'm surprised how
little I do this and how much I should) I read that ';' semicolon is
also a Rule / Command separator.

But is this a make feature or a GNU make feature?

Dan
 
R

Randy Howard

But is this a make feature or a GNU make feature?

Dan

Is there an ANSI standard on Make and makefiles? Or, are we free to
assume that GNU make is the "one true make" and proceed thusly?
 
P

Peter Shaggy Haywood

Groovy hepcat Darius Fatakia was jivin' on Mon, 24 Nov 2003 16:52:07
-0800 in comp.lang.c.
#include said:
I seem to be having trouble with some of my math functions (pow, sqrt,
acos). They're the only ones I use in my code and they prevent the program
from compiling. I get a "undefined reference to 'pow'" error. Here is the
relevant portion of my code.

It is the height of rudeness to post to a newsgroup without doing
both of the following:

1) read all posts in the newsgroup for a month or two, and
2) read the newsgroup's FAQ.

Please do these two things before posting again.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
M

Mark McIntyre

Is there an ANSI standard on Make and makefiles? Or, are we free to
assume that GNU make is the "one true make" and proceed thusly?

Its a great assumption. It will work perfectly.

Right up to the point at which you port your makefile to some other
compiler environment. Heck, ISTR that makefiles from the C compiler
shipped with SunOS 4.1.3 didn't work with the Sparc C++ compiler.
 
J

Joe Wright

Dan said:
But is this a make feature or a GNU make feature?
Certainly GNU make because I read it there and tried it and it worked. I
cannot know and don't care much about all other make programs. By the
same token I don't know that TAB is the separator other make programs.
To the extent that TAB was chosen by make's original author (Who?) it
seems arbitrary and badly chosen.
 
R

Randy Howard

Its a great assumption. It will work perfectly.

Right up to the point at which you port your makefile to some other
compiler environment. Heck, ISTR that makefiles from the C compiler
shipped with SunOS 4.1.3 didn't work with the Sparc C++ compiler.

Seems that would be easy enough to solve. Open Source, shouldn't
be hard to port to just about any modern platform that can support
a make of its own.
 
G

gswork

Kelsey Bjarnason said:
Sounds like a linker error - are you linking the math libraries? There's
a certain, popular compiler, especially widely used in the Linux world,
which has the terminally brain-dead habit of not including the math
libraries by default. Assuming you're using this compiler, try something
like this:

gcc -lm file.c

Out of interest and for the benefit of the OP, does the standard have
anything to say about linkage?
 
I

Irrwahn Grausewitz

Out of interest and for the benefit of the OP, does the standard have
anything to say about linkage?

About /linking/ refer to ISO/IEC 9899:1999 5.1.1.2#8.

About /linkage/ refer to 6.2.2., 6.7, 6.7.4, 6.7.5.2, 6.9,
6.9.2, 6.11.2, ...

HTH
Regards
 
D

Dan Pop

In said:
Is there an ANSI standard on Make and makefiles? Or, are we free to
assume that GNU make is the "one true make" and proceed thusly?

Why does it have to be an ANSI standard? C99 is an ANSI standard, yet
C99 code is extremely non-portable, due to lack of implementations.

According to the HP-UX man page:

STANDARDS CONFORMANCE
make: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2

Dan
 
D

Dan Pop

In said:
Seems that would be easy enough to solve. Open Source, shouldn't
be hard to port to just about any modern platform that can support
a make of its own.

"Shouldn't be hard to port" and "isn't hard to port" are two completely
different things.

Between merely having to compile and install gnu make and writing portable
makefiles I'll choose the latter without a second's hesitation. I had
no problem using well written Unix makefiles with Microsoft's NMAKE when
I had to port the project to Windows + the VC++ toolchain.

Dan
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top