A while ago I was working through Zed Shaw's Learn C the Hard Way. When I got to building the devpkg part, i kept getting errors that the apr library functions weren't defined, even though I was sure the makefile and CFLAGS were correct. I checked to be sure that the file exists in /usr/include, copied .h file to the local folder, no good. I eventually gave up assuming I did something stupid.
Now I tried to make a simple recursive C script using math.h functions. I'm now getting similar errors. stdio.h seems to work fine. So I copied the file to a windows box with cygwin and it compiled just fine. Here is the script:
Let me know what more information you'd need. I have no idea where to look, and I'd rather not reinstall. Thanks.
Last edited by jemofthewest (2013-02-23 18:24:01)
The actual errors would help - and the compile command you are using.
That code compiles and runs fine here.
Last edited by Trilby (2013-02-23 02:17:14)
My crystal ball says you're not linking in the necessary libraries, which would result in a message like "undefined reference to `pow' " (or whatever function you're trying to use). The functions declared in <stdio.h> are in libc.a, which is linked in by default; most of what's in <math.h> is in libm.a, which is not. In this case you'd add -lm to the gcc command to tell the linker where the math-related stuff can be found.
waller@odin:~ 1038 %gcc foo.c /tmp/cc27Zfpc.o: In function `f': foo.c:(.text+0x32): undefined reference to `pow' foo.c:(.text+0x49): undefined reference to `atan' foo.c:(.text+0x60): undefined reference to `sin' /tmp/cc27Zfpc.o: In function `Df': foo.c:(.text+0x9d): undefined reference to `sin' foo.c:(.text+0xe9): undefined reference to `pow' foo.c:(.text+0x122): undefined reference to `cos' collect2: error: ld returned 1 exit status ewaller@odin:~ 1039 %gcc foo.c -lm ewaller@odin:~ 1040 %./a.out Alpha is: 0.500000% ewaller@odin:~ 1041 %
P.S. You forgot the newline in the printf at line 45
POW(3) Linux Programmer's Manual POW(3)
pow, powf, powl - power functions
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Link with -lm.
Last edited by ewaller (2013-02-23 16:49:04)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence. You must not work with the public much. -- Trilby
How to Ask Questions the Smart Way
Ah, thanks so much :-) That was it. I read the man page, but apparently not well enough.
EDIT: I read the math.h man page. It seems weird that that would be on the individual functions, but not for the header file... I see it now with pow and sin. Thanks again.
Last edited by jemofthewest (2013-02-23 18:29:07)