相关疑难解决方法(0)

为什么必须在C中链接数学库?

如果我包含<stdlib.h><stdio.h>在C程序中,我不必在编译时链接这些,但我必须链接到<math.h>,使用-lmgcc,例如:

gcc test.c -o test -lm
Run Code Online (Sandbox Code Playgroud)

这是什么原因?为什么我必须显式链接数学库而不是其他库?

c compilation math.h

241
推荐指数
7
解决办法
11万
查看次数

对sqrt(或其他数学函数)的未定义引用

我有这个简单的代码:

max = (int) sqrt (number);
Run Code Online (Sandbox Code Playgroud)

在标题中我有:

#include <math.h>
Run Code Online (Sandbox Code Playgroud)

但应用程序仍然表示未定义的引用sqrt.你觉得这里有什么问题吗?看起来一切都应该没问题.

c linker-errors undefined-reference

57
推荐指数
2
解决办法
9万
查看次数

-lm选项在g ++中做什么?

-lm选项在g ++中做什么以及何时需要?

是否有完整的g ++选项说明?

gcc g++

16
推荐指数
2
解决办法
1万
查看次数

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()不应该使用变量或其他东西吗?

谢谢您的帮助

c compiler-errors math.h sqrt

9
推荐指数
1
解决办法
1万
查看次数

仅当参数不是常量时,math.h中的sqrt才会导致链接器错误"未定义对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)

c compiler-errors compilation

6
推荐指数
1
解决办法
8557
查看次数

为什么 sqrt 函数对通过用户输入获取的值不起作用?

以下代码:

#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, …

c sqrt

2
推荐指数
1
解决办法
559
查看次数

编译错误用c

我正在尝试编译一个名为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)

这些错误可能是什么原因?

c makefile

0
推荐指数
1
解决办法
350
查看次数

计算C中复数的abs值

部分源代码:

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

c linker-errors clion

0
推荐指数
1
解决办法
142
查看次数