C Build error when getting the value of sin()
I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBe开发者_开发知识库ans using the GCC as toolset for compiling. One of the very first programs that I made was this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv) {
double rad = 1;
double result = 0;
result = sin(rad);
return (EXIT_SUCCESS);
}
This is a simple, no-brainer example that should have worked without a problem. However, I get a Build Error: Exit code 2(error in line 18, undefined reference to sin) when trying to compile. Interestingly enough, if I remove the assignment of the value of sin(rad) to result OR replace rad with a hard coded value, the program compiles just fine. What am I doing wrong here?
In C, you need to link to the math library:
Add this to the command line options:
-lm
Be sure that your are linking with the math library.
$ gcc myprog.c -lm
精彩评论