问题描述
我最近开始学习 C 作为一个副项目.我正在 OpenSuse 下使用最新的 NetBeans,使用 GCC 作为编译工具集.我最早制作的程序之一是这样的:
I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBeans 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);
}
这是一个简单明了的示例,应该可以正常运行.但是,我在尝试编译时遇到构建错误:退出代码 2(第 18 行中的错误,未定义对 sin 的引用).有趣的是,如果我删除了 sin(rad) 的值分配给结果或用硬编码值替换 rad,程序编译得很好.我在这里做错了什么?
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?
推荐答案
在C中,需要链接到数学库:
In C, you need to link to the math library:
将此添加到命令行选项:
Add this to the command line options:
-lm
这篇关于C 获取 sin() 的值时构建错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!