Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!