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.
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.
c - Undefined reference to `pow' and `floor' - Stack Overflow
c - Undefined reference to pow when compiled using gcc - Stack Overflow
link error: undefined reference to `pow'
[C Programming] Undefined reference to pow and sqrt
you have to call gcc with -lm: https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c
More on reddit.comVideos
You need to compile with the link flag -lm, like this:
gcc fib.c -lm -o fibo
This will tell gcc to link your code against the math lib. Just be sure to put the flag after the objects you want to link.
Add -lm to your link options, since pow() and floor() are part of the math library:
gcc fib.c -o fibo -lm
I'm trying to do my homework which is to make a program that calculates a hypotenuse, given two cathetus entered by the user. This is the code:
#include <stdio.h>
#include <math.h>
#include <locale.h>
int main(void) {
float primeiroCateto;
float segundoCateto;
float hipotenusa;
setlocale(LC_ALL, "Portuguese");
printf("Digite o valor do primeiro cateto:\n");
scanf("%f", &primeiroCateto);
printf("Digite o valor do segundo cateto:\n");
scanf("%f", &segundoCateto);
hipotenusa = sqrt(pow(primeiroCateto, 2) + pow(segundoCateto, 2));
printf("A hipotenusa é igual a:\n%.2f", hipotenusa);
return 0;
}
The problem is that when i try to copile the code using:
gcc -Wall -Wextra -o Atividade2_exercício5 Atividade2_exercício5.c
This error shows up:
/tmp/ccFsAgvS.o: In function `main': Atividade2_exercício5.c:(.text+0x82): undefined reference to `pow' Atividade2_exercício5.c:(.text+0x9d): undefined reference to `pow' Atividade2_exercício5.c:(.text+0xa7): undefined reference to `sqrt' collect2: error: ld returned 1 exit status
So what to do now? (i'm using vccode by the way)
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
The pow function is part of the math library. Therefore, the invokation of your compiler should be as follows :
gcc power.c -o power -lm
Where -lm is the switch instructing the linker to perform linking with the math library on your system.