Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 11th, 2006, 6:42 AM   #1
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
include math.h not working for pow function

Hi guys - remember me from that pointer crisis!!!?

Got a much smaller problem this time. For some reason gcc on linux (but strangely enough not on windows/cygwin) is refusing to recognise a call to the pow function even though i have included math.h .

These are the relevant lines of code:
#include <math.h>
...
b = pow(2, (c));

b and c are declared as ints, whereas my research has told me they should strictly be doubles - but even when they are declared as doubles and I put 2.0 instead of 2, I still get the exact same error message, which is:

/tmp/ccK1ydZK.o(.text+0x91d): In function `genbitbinary':
: undefined reference to `pow'

Any help greatly appreciated - this coursework is finally due in tomorrow and can't find anyone who knows what's going on here!
deanosrs is offline   Reply With Quote
Old May 11th, 2006, 7:28 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Perhaps you could peer into your suspect copy of math.h and see if it mentions 'pow'. Seems silly, I know.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 11th, 2006, 8:47 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
You need to link with the math library:
gcc prog.c -lm
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old May 11th, 2006, 9:25 AM   #4
deanosrs
Programmer
 
deanosrs's Avatar
 
Join Date: Apr 2006
Location: Bristol, UK
Posts: 31
Rep Power: 0 deanosrs is on a distinguished road
Send a message via MSN to deanosrs
Mmmm... well I can't control how it's compiled so looks like I'm going to have to write myself a pow function. Is there anywhere i can get the one that should be in math.h?
deanosrs is offline   Reply With Quote
Old May 11th, 2006, 9:32 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
(post deleted)

Disregard, I failed to realize it was C and not C++. Thanks DaWei
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old May 11th, 2006, 9:32 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Narue is implying that it's a link error, not a compile error. I don't use gcc, so I didn't recognize that. A compile error means that it wasn't declared or defined in a place the compiler could find it. That would be your source code or your included headers. A link error means that actual code to fulfill the promise of the declaration wasn't found. That means a library or object file (or whatever they may be called your particular system) wasn't found. You have to make sure that you have the library and that it's in a place the linker knows to look. It's the partner to having to have an include file and the compiler having to know where to find it.

This is the C forum, IR. I have no idea how he's actually writing and compiling his code.

OP, you need to distinguish between declarations and code/definitions. It's confusing you. Header files say something is/will be available before the process is finished, they don't necessarily provide it to the compiler. The compiler only wants to know how to reference it. The linker needs the Real Thang.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 11th, 2006, 10:52 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
... have to write myself a pow function...
It quite simple if you're working with only ints.
int power(int base,int exponent)
{
   int i,ret=base;
   for(i=1;i<exponent;++i)
      ret *= base;
   return ret;
}
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC