Pow Gcc Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads undefined reference to pow in gcc with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the undefined reference to pow' ubuntu Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: undefined reference to pow' makefile Sign up Undefined Reference error for power function in C [duplicate] up vote 1 down vote favorite Possible Duplicate: Problem using pow() in C Undefined reference to pow' andfloor' I am using gcc 4.6.1. Code snippet: int main(void) { int x= 2; undefined reference to `pow' cmake int y = pow(3,x); printf("%d\n",y); return 0; } Terminal: avinash@titanic:~/Desktop/DSF$ gcc power.c -o power /tmp/ccTJ7vAH.o: In function `main': power.c:(.text+0x25): undefined reference to `pow' collect2: ld returned 1 exit status If I replace x by 2 then it gets executed in an expected way. Doesn't pow function accepts variables as it's parameters? Note: I have included stdio.h and math.h in the source file. c gcc pow share|improve this question edited Sep 15 at 13:23 metadings 2,72411429 asked Jan 26 '12 at 20:40 Avinash Sonawane
Undefined Reference To Pow' Eclipse
41125 marked as duplicate by Tim Cooper, ruakh, Bo Persson, Mac, pmg Jan 26 '12 at 20:51 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. If you have pow(3,2) the compiler can compute that directly and doesn't have to call the function. –Bo Persson Jan 26 '12 at 20:45 If you included stdio.h and math.h in the source file, then show us that in the code rather than showing us incomplete code and then telling us what's missing. –Keith Thompson Jan 26 '12 at 20:54 @KeithThompson- Sorry. But "include" term was appearing to be Bold without actually going for the bold type. So I dropped the #include lines, but i have mentioned that I have included both the libraries under note. –Avinash Sonawane Jan 26 '12 at 21:00 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote You need to add the -lm command-line switch to link in the math library (-l... means "link in the __ library"; m is the math library): gcc power.c -lm -o power share|improve this answer edited Jan 26 '12 at 20:51 answered Jan 26 '12 at 20:42 ruakh 94.6k10135187 2 Or rather gcc power.c -lm -o power. The lib has to come after the source file. –James McLaughlin Jan 26 '12 at 20:44 This is not the proper order of
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business
Undefined Reference To `__pthread_register_cancel'
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation power function in c without pow Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like how to use pow function in c you, helping each other. Join them; it only takes a minute: Sign up Undefined reference to pow( ) in C, despite including math.h [duplicate] up vote 17 down vote favorite 6 Possible Duplicate: Problem using pow() in http://stackoverflow.com/questions/9025015/undefined-reference-error-for-power-function-in-c C what is 'undefined reference to `pow'' I'm having a bit of an issue with a simple piece of coursework for uni that's really puzzling me. Essentially, I've to write a program that, amongst other things, calculates the volume of a sphere from a given radius. I thought I'd use the pow() function rather than simply using r*r*r, for extra Brownie points, but the compiler keeps giving me the following error: undefined reference to 'pow' http://stackoverflow.com/questions/12824134/undefined-reference-to-pow-in-c-despite-including-math-h collect2: error: ld returned 1 exit status My code looks like the following: #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack http://stackoverflow.com/questions/4174080/problem-using-pow-in-c Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Problem using pow() in C up vote 22 down vote favorite 4 Why does the following undefined reference bit of code work in C: int res = pow(2, 3); printf("%d\n", res); while this other doesn't? int a = 2; int b = 3; int res = pow(a, b); printf("%d\n", res); Even if I try double a = 2; double b = 3; double res = pow(a, b); printf("%f\n", res); I get an undefined reference to `pow' What am I doing wrong? c share|improve this question asked Nov 13 '10 at undefined reference to 18:35 devoured elysium 28.8k70229411 What error message do you get for each attempt? –Alex Reynolds Nov 13 '10 at 18:36 "undefined reference to 'pow' –devoured elysium Nov 13 '10 at 18:37 A complete code snippet would be helpful... –Oliver Charlesworth Nov 13 '10 at 18:37 Are you including math.h or not? –Pete Kirkham Nov 13 '10 at 18:37 I am including math.h, yes. The first code snippet works and outputs what'd be expected. The other two give the "undefined reference to 'pow'". –devoured elysium Nov 13 '10 at 18:38 | show 5 more comments 4 Answers 4 active oldest votes up vote 42 down vote accepted When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out) printf("8\n"); When it doesn't work, is because the pow function is included in the math library and the math library isn't linked with your binary by default. To get the math library to be linked, if your compiler is gcc, use gcc ... -lm ... With other compilers, should be the same :) but read the documentation share|improve this answer answered Nov 13 '10 at 18:39 pmg 69.4k783141 4 You're sp