Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda