You are not logged in.

#1 2013-02-23 01:54:05

Registered: 2012-02-27
Posts: 37

[SOLVED] Issues with Include in C

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)


#2 2013-02-23 02:13:04

Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 15,491

Re: [SOLVED] Issues with Include in C

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)


#3 2013-02-23 16:28:52

From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

Re: [SOLVED] Issues with Include in C

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.


#4 2013-02-23 16:47:52

From: Pasadena, CA
Registered: 2009-07-13
Posts: 14,193

Re: [SOLVED] Issues with Include in C

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:~[1] 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

Edit:  BTW:

man pow wrote:

POW(3)                                               Linux Programmer's Manual                                              POW(3)

       pow, powf, powl - power functions

       #include <math.h>

       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


#5 2013-02-23 18:23:35

Registered: 2012-02-27
Posts: 37

Re: [SOLVED] Issues with Include in C

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)


Board footer

Powered by FluxBB