我正在为c学校的实践做一些辅导练习。我有这个小方法(苍鹭的公式):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
.
.
double s = (a+b+c)/2.0;
double A = sqrt(s*(s-a)*(s-b)*(s-c)) //a,b,c = 3,3,2
.
.
我可以用gcc编译它:
gcc exercise.c -o exercise -lm
一切都运作良好。问题是,当我尝试在Netbeans中重写相同的代码时,它会抛出此错误:
/home/l2ysho/NetBeansProjects/cviko1/main.c:48: undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU- Linux/cviko1' failed
make[2]: *** [dist/Debug/GNU-Linux/cviko1] Error 1
make[2]: Leaving directory '/home/l2ysho/NetBeansProjects/cviko1'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/l2ysho/NetBeansProjects/cviko1'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 861ms)
在Netbeans中,sqrt()方法仅适用于数字(sqrt(2.5)),但不适用于常量(sqrt(s))。我认为,这是Netbeans编译器的一些问题。有没有人有类似的问题?
答案 0 :(得分:0)
我找到了一个解决方案。如果您遇到同样的问题,可以在NetBeans IDE中硬连接math.h:
File->Project Properties->Build->Linker->Libraries->add Library/add standard Library
我还尝试在NetBeans编译器配置中添加-lm参数,但这不起作用。