如果我包含<stdlib.h>或<stdio.h>在C程序中,我不必在编译时链接这些,但我必须链接到<math.h>,使用-lmgcc,例如:
gcc test.c -o test -lm
Run Code Online (Sandbox Code Playgroud)
这是什么原因?为什么我必须显式链接数学库而不是其他库?
我有这个简单的代码:
max = (int) sqrt (number);
Run Code Online (Sandbox Code Playgroud)
在标题中我有:
#include <math.h>
Run Code Online (Sandbox Code Playgroud)
但应用程序仍然表示未定义的引用sqrt.你觉得这里有什么问题吗?看起来一切都应该没问题.
我不知道我是否遗漏了一些明显的东西,但似乎我无法计算C中变量的平方根; sqrt()函数似乎只适用于常量.这是我的代码:
#include <math.h>
#include <stdio.h>
int main()
{
double a = 2.0;
double b = sqrt(a);
printf("%f", b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此程序时,我收到以下错误:
gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler)
/tmp/ccVfxkNh.o: In function `main':
test2.c:(.text+0x30): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Compilation failed.
Run Code Online (Sandbox Code Playgroud)
但是,如果我将sqrt()中的参数替换为常量(例如2.0,(b = sqrt(2.0))),那么它可以正常工作.sqrt()不应该使用变量或其他东西吗?
谢谢您的帮助
我创建了一个小程序,如下所示:
#include <math.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int i;
double tmp;
double xx;
for(i = 1; i <= 30; i++) {
xx = (double) i + 0.01;
tmp = sqrt(xx);
printf("the square root of %0.4f is %0.4f\n", xx,tmp);
sleep(1);
xx = 0;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译它时,我收到编译器错误.
gcc -Wall calc.c -o calc
Run Code Online (Sandbox Code Playgroud)
收益:
/tmp/ccavWTUB.o: In function `main':
calc.c:(.text+0x4f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我用sqrt(10.2)之类的常量替换对sqrt(xx)的调用中的变量,它编译得很好.或者,如果我明确链接如下:
gcc -Wall -lm calc.c -o …Run Code Online (Sandbox Code Playgroud) 以下代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
long long int a;
scanf("%lld", &a);
printf("%lf", sqrt(a));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
source_file.c: In function ‘main’:
source_file.c:9:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a);
^
/tmp/ccWNm2Vs.o: In function `main':
source.c:(.text.startup+0x65): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我执行long long int a = 25;并删除scanf语句,或者只是执行sqrt(25),它们都可以工作(正确给出 output 5.000000)。
我检查了这个问题,但它是针对 C++ 并使用函数重载的,而 afaict C 没有函数重载(这就是为什么我们有sqrtf, …
我正在尝试编译一个名为randfold的程序来处理RNA折叠.但是在此期间sudo make,我收到了以下有关c程序的消息.
params.o: In function `scale_parameters':
params.c:(.text+0x160): undefined reference to `log'
params.c:(.text+0x1bc): undefined reference to `log'
fold.o: In function `HairpinE':
fold.c:(.text+0x3981): undefined reference to `log'
fold.o: In function `LoopEnergy':
fold.c:(.text+0x3bd3): undefined reference to `log'
fold.c:(.text+0x3dfa): undefined reference to `log'
fold.o:fold.c:(.text+0x4f81): more undefined references to `log' follow
collect2: ld returned 1 exit status
make: *** [randfold] Error 1
Run Code Online (Sandbox Code Playgroud)
这些错误可能是什么原因?
部分源代码:
double _Complex z = 1.0 + 1.0*I;
printf("%f\n", cabs(z));
Run Code Online (Sandbox Code Playgroud)
我的开发环境:Ubuntu16.04LTS,Clion IDE,GCC版本5.4.0,C11标准.
当我运行代码时,消息发生错误
undefined reference to `cabs'
Run Code Online (Sandbox Code Playgroud)
IDE告诉我该函数cabs是在头文件中声明的cmathcalls.h,所以我尝试:
#include<cmathcalls.h>
Run Code Online (Sandbox Code Playgroud)
但IDE警告我无法找到该文件,所以我再次尝试:
#include<bits/cmathcalls>
Run Code Online (Sandbox Code Playgroud)
我运行代码,但它仍然无法运行.
我想知道如何通过z函数得到复数的abs值cabs?