Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
Undefined reference to `sin' when passing variable instead of constant

I had a code that was working perfectly in windows but when i tried to compile it in linux i got an error. I found that the issue is with the sin() function.

If i pass to it a constant number directly, it works fine:

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

int main(void) {
    float y = sin(1.57);

    printf("sin(1.57) = %f", y);

    return 0;
}

Output 1:

sin(1.57) = 1.000000

But when i pass to it a variable i get an error:

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

int main(void) {

    float x = 1.5;
    float y = sin(x);

    printf("sin(%f) = %f", x, y);

    return 0;
}

Output 2:

/tmp/ccfFXUZS.o: In function `main':
source-bcfaa9ff162b:(.text+0x1a): undefined reference to `sin'
collect2: error: ld returned 1 exit status
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

On some systems, the math functions are in a separate library, which needs to be linked explicitly. Simply pass the option -lm when compiling (linking) your program, and everything should work.

The reason the first one works is because the compiler knows about sin and optimized the whole thing to a constant - it doesn't need to actually call the sin function.

over 4 years ago · Santiago Trujillo Relatório

0

You need to link with -lm.

I don't know why the first example is working - maybe some optimizations ?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda