You are not logged in.

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

jemofthewest
Member
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:

http://sprunge.us/CSGH

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)

Offline

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

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

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)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

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

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

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.

Offline

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

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

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)

NAME
       pow, powf, powl - power functions

SYNOPSIS
       #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
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

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

jemofthewest
Member
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)

Offline

Board footer

Powered by FluxBB