![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
|
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?
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
(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." |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|