gcc 和 math.h 的 C 链接器错误

标签 c gcc linker math.h trigonometry

我收到一个错误提示 pythagorean.so: undefined symbol: cos。我正在使用 math.hstdlib.hstdio.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/

相关文章:

c++ - GNU const/纯属性与 constexpr

c++ - 编译 testOnDemandRTSPServer.cpp 时出错(来自 Live555 库)

linker - glibc.so 中的 pthread 是否由弱符号实现以提供 pthread stub 函数?

c - fatal error #1035 : Can't find include file "stdafx.h"

c++ - 在 GCC 中链接库

c - C中的结构内的结构

gcc - cygwin 64 位上的简单 gcc 编译

c++ - boost 功能并绑定(bind)外部符号

c - 不定数量结构数组的动态分配

c - 从 C 中的终端获取所有输出