计算C中复数的abs值

时间:2018-02-07 09:03:39

标签: c linker-errors clion

部分源代码:

double _Complex z = 1.0 + 1.0*I;
printf("%f\n", cabs(z));

我的开发环境: Ubuntu16.04LTS,Clion IDE,GCC版本5.4.0,C11标准

运行代码时,消息

发生错误
undefined reference to `cabs'

IDE告诉我函数cabs在头文件cmathcalls.h中声明,所以我尝试:

#include<cmathcalls.h>

但IDE警告我无法找到该文件,所以我再试一次:

#include<bits/cmathcalls>

我运行代码,但它仍然无法运行。

我想知道如何使用函数z获取复数cabs的abs值?

2 个答案:

答案 0 :(得分:5)

对于cabs,您只需要<complex.h>。它实际声明的地方只是一个实现细节。

然后与-lm链接以实际链接数学库。

答案 1 :(得分:3)

构建可执行文件时,必须链接数学库。如何做到这一点因环境而异,但在Linux / Unix中,只需在命令中添加-lm:

gcc test.c -o test -lm

数学库名为 libm.so ,-l命令选项采用lib前缀和.a或.so后缀。

来源:Why am I getting "undefined reference to sqrt" error even though I include math.h header?