-
Undefined Symbol error
This is my test prog n my code goes like this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
double x, y, z;
double num = 5;
x = log(num);
y = exp(num);
z = pow(num, (double)2);
printf("\n\nlog of num = %e\nexp of num = %e\npow of num = %e\n\n", x, y, z);
return 0;
}
However i got these errors while compling my prog in unix.
ld: 0711-317 ERROR: Undefined symbol: .pow
ld: 0711-317 ERROR: Undefined symbol: .log
ld: 0711-317 ERROR: Undefined symbol: .exp
I haved checked my <math.h> library and it has these functions.
Can anyone help to explain what this error means?
Thks
-
are you using gcc or g++?
make sure to link the math lib too with "-lm". for example if your source file is test.c:
gcc test.c -lm
-
thks.
That works for my single file. I am using xlc by the way.
However for my makefile, how should i go about adding that -lm?
What does that -lm represent?
My makefile code goes lik this:
Code:
##
##
## "Test" makefile
##
##
trial: test.o readinput.o configuring.o functions.o saveoutput.o
xlc -o trial test.o readinput.o configuring.o functions.o saveoutput.o
test.o: test.cpp variables.h complex.h
xlc -c test.cpp -lm
readinput.o: readinput.cpp variables.h complex.h
xlc -c readinput.cpp -lm
configuring.o: configuring.cpp variables.h complex.h
xlc -c configuring.cpp -lm
functions.o: functions.cpp variables.h complex.h
xlc -c functions.cpp -lm
saveoutput.o: saveoutput.cpp variables.h complex.h
xlc -c saveoutput.cpp -lm
What is wrong?
-
the "-l" is a flag for the compiler telling it to link something. i'm guessing the "m" is for the math library.
try putting an "-lm" in "trial". example:
trial: test.o readinput.o configuring.o functions.o saveoutput.o
xlc -o trial test.o readinput.o configuring.o functions.o saveoutput.o -lm
i think you only need to link the math lib when you are linking all those object files, not during the compilation of your source files (you could probably remove the "-lm" elsewhere).