Page 1 of 1

undefined reference to `pow'

Posted: Tue Apr 03, 2012 2:48 am
by SteveOll
I'm sure that you've seen this error before, and I'm using codelite V3.5.5375 on Debian Squeeze, and for some reason my program will not sucessfully compile. I have all of the header files in the appropriate place and if the program only needs stdio.h then it will compile without problems, but for some reason the math.h file is not being linked/referenced or whatever the technical term is. I have not used codelite before but have used older IDE's where the header file path could be specifically set (e.g. Lattice C)

This is output from Output view panel:

Code: Select all

----------Build Started--------
/bin/sh -c '"make"  -j 4 -f "compoundInterest_wsp.mk"'
----------Building project:[ compoundInterest - Debug ]----------
make[1]: Entering directory `/home/steveoll/Documents/C/Codelite/compoundInterest'
gcc -o ./Debug/compoundInterest @"/home/steveoll/Documents/C/Codelite/compoundInterest/compoundInterest.txt" -L/usr/local/include  -L.   
./Debug/main.o: In function `main':
/home/steveoll/Documents/C/Codelite/compoundInterest/main.c:14: undefined reference to `pow'
collect2: ld returned 1 exit status
make[1]: *** [Debug/compoundInterest] Error 1
make[1]: Leaving directory `/home/steveoll/Documents/C/Codelite/compoundInterest'
make: *** [All] Error 2
----------Build Ended----------
1 errors, 0 warnings
Any help would be appreciated, thank you.

-Steve

Re: undefined reference to `pow'

Posted: Tue Apr 03, 2012 10:19 am
by eranif
SteveOll wrote:the math.h file is not being linked/referenced or whatever the technical term is
The technical term is important. To clarify, from looking at your compile output I can tell you that your code compiled fine, but it failed to LINK (Please use code tags next time when posting compiler output)

Simple

Code: Select all

man pow
From the command line will tell you this:
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.
The last line:
Link with -lm.
is what you are missing:

From within codelite, right click on your project and open the settings -> Linker page under the 'Libraries' add (yes, just 'm'):

Code: Select all

m
if you have other libraries to add, use semi-colon to separate them

Eran

Re: undefined reference to `pow'

Posted: Tue Apr 03, 2012 1:02 pm
by SteveOll
Eran,

Thank you for your reply. I was aware that I could use

Code: Select all

-lm
as part of the command line instruction to compile the code, e.g.

Code: Select all

gcc -o compoundInterest compoundInterest.c -lm
And that would work, but I didn't know how to achieve this in your IDE, thank you for your help, its been a long time since I last used C and I was trying to brush up on it again.

-Steve