开发者

C Build error when getting the value of sin()

开发者 https://www.devze.com 2023-04-06 09:58 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号