Problem with library Math. h (indefinite reference to `sqrt')

Asked

Viewed 4,669 times

2

I’m having trouble compiling the code with the function sqrt() in the C language.

Error:

gcc exercicio_03.c  /tmp/ccGVE8ez.o: na função `distancia':
exercicio_03.c:(.text+0x142): referência indefinida para `sqrt'
collect2: error: ld returned 1 exit status

Code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


#define TAM 2

typedef struct pontos{
    int x,y;
}dados;

void ler(dados *vetor);
int distancia(dados *vetor);

int main(){
  dados vetor[TAM];
  int resultado;
  ler(vetor);
  resultado=distancia(vetor);
  printf("\nA distancia entre eles e igual a %d\n\n\n",resultado);
return 0;
}

void ler(dados *vetor){
  for(int i=0;i<TAM;i++){
      printf("\nDigite as coordenadas do ponto %d",i);
      scanf("%d",&vetor[i].x);
      scanf("%d",&vetor[i].y);
  }
}

int distancia(dados *vetor){
  int x,y,retorno;
  x=vetor[0].x-vetor[1].x;
  y=vetor[0].y-vetor[1].y;
  retorno=(x*x)+(y*y);
  retorno=sqrt(retorno);
return retorno;
}

2 answers

3

You need to ensure that the library is included in the compilation, for this use -lm in the compiler command line.

  • Thanks, it worked!

  • @Rafaelteixeira see on [tour] the best way to say thank you.

-1

In Code Blocks click on: Settings -> Compiler -> Linker Settings. On the screen, on the right side, where is "Other Linker options", type -lm and click ok.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.