You need to link with the math library:
gcc -o sphere sphere.c -lm
The error you are seeing: error: ld returned 1 exit status is from the linker ld (part of gcc that combines the object files) because it is unable to find where the function pow is defined.
Including math.h brings in the declaration of the various functions and not their definition. The def is present in the math library libm.a. You need to link your program with this library so that the calls to functions like pow() are resolved.
pow() function does not compile if either parameter not constant
link error: undefined reference to `pow'
c - Undefined reference to pow when compiled using gcc - Stack Overflow
undefined reference to `pow'
Videos
Hey y'all, as the title says, I'm a wee bit stuck trying to use the math.h library function "pow". Here's my code -
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
//OMITTED CODE
size_t i;
double j;
int k;
for(i = argLen - 1, k = 0; k < argLen; i--, k++) {
j = pow(16, i);
deciVals[k] = deciVals[k] * j;
}
//OMITTED CODE
return 0;
}Despite having math.h included, and using the -lm option for the GCC, I'm still getting this error -
>gcc -lm -o output hexConverter.c /usr/bin/ld: /tmp/ccghjuxO.o: in function `main': hexConverter.c:(.text+0x237): undefined reference to `pow' collect2: error: ld returned 1 exit status
Any help would be appreciated, thanks guys.
Hello! I have a very strange problem.
code:
#include <stdio.h>#include <math.h>int main() {long long a = 1, b = 2;
double ans = pow(1, 2);
printf("%f", ans);return 0;}
--------------------------------------
return:
1.000000
============================================
code:
#include <stdio.h>#include <math.h>int main() {long long a = 1, b = 2;
double ans = pow(a, b);
printf("%f", ans);return 0;}
--------------------------------------
return:/usr/bin/ld: CMakeFiles/untitled.dir/main.c.o: in function \main':`
/home/tarminik/CLionProjects/untitled/main.c:7: undefined reference to \pow'`
collect2: error: ld returned 1 exit status