我收到一个错误提示 pythagorean.so: undefined symbol: cos
。我正在使用 math.h
、stdlib.h
和 stdio.h
库以及 -lm
进行编译转变。这是一个代码片段:
static bool
law_of_cosine_run(esh_command* cmd)
{
if (strcmp(cmd->argv[0], "lawofcosine") == 0) {
printf("The length of the third side is: %f\n",
law_of_cosine(strtol(cmd->argv[1], NULL, 10),
strtol(cmd->argv[2], NULL, 10), strtol(cmd->argv[3], NULL, 10)));
return true;
}
return false;
}
static double
law_of_cosine(double x, double y, double z)
{
return sqrt((x * x) + (y * y) - (2 * x * y * cos(z)));
}
有人知道为什么会这样吗? pythagorean.c 是我的 .c 文件。
谢谢
最佳答案
编译的时候一定要输入
gcc *.c -lm
关于gcc 和 math.h 的 C 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21924577/