如果我包含<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/C++读取一个图像文件.如果有人可以为我发布代码,那将是非常好的.
我处理灰度图像,图像是JPEG.我想把图像读成2D数组,这将使我的工作变得简单.
我正在使用math.h和-lm编译的选项.我尝试了所有:
gcc -o ssf ssf_tb.c ssf.c -lm
gcc -o ssf ssf_tb.c -lm ssf.c
gcc -o -lm ssf -lm ssf_tb.c ssf.c
Run Code Online (Sandbox Code Playgroud)
但错误:
undefined reference to 'pow'
Run Code Online (Sandbox Code Playgroud)
在所有情况下都会发生
我有一个键盘.
在第15行内的for函数
for(i=2; i<=90; i+=2){
int j=0+i;
printf("%i\n",i);
power=pow(inp,j);
factor=factorial(i);
if(i%4==0)fAns += power/factor;
else fAns -= power/factor;
}
Run Code Online (Sandbox Code Playgroud)
power=pow(inp,j);我添加的行j而不是使用,i因为它给了我同样的错误.undefined reference to 'pow'.
如果我j用2 替换,那么它工作正常,但当我使用j=i它不会工作.递增此行是否有问题?
我希望这个增加,而不是给我一个错误.
对于某些编译器,在C程序中使用pow和某些其他功能需要链接到m库.但是,某些编译器不需要这样做,并且在链接到m库时会出错.对于带有std::thread和的C++,存在几乎完全相同的情况pthread,但是CMake模块FindThreads完全缓解了这种情况 - 是否有一些类似于libm的模块?
检测CMake如何处理的最佳方法是什么?这是我目前的解决方案,这不太理想,因为有更多的C编译器,而不仅仅是GCC和MSVC:
if(NOT MSVC)
target_link_libraries(my-c-target PUBLIC m)
endif()
Run Code Online (Sandbox Code Playgroud)
这适用于我的目的,但我很确定有些情况会失败并需要手动用户干预,这对于不了解这种默默无闻的人来说并不好玩.理想情况下,我不希望用户必须通过命令行指定他们的编译器是否很奇怪 ; 我想在CMake中自动检测它,因为这是CMake的全部内容.
在 ubuntu 20.04 上,当我使用 clang-8 或 clang-9(clang 版本 9.0.1-12)编译包含对 libm 的引用的简单代码时,它将失败并显示错误“未定义的引用__pow_finite”
#include <math.h>
int main()
{
double x=1, y=1;
x = pow(x,y);
}
Run Code Online (Sandbox Code Playgroud)
clang-9 -lm test.c -ffast-math
/usr/bin/ld: /tmp/test-9b1a45.o: in function `main':
test.c:(.text+0x2a): undefined reference to `__pow_finite'
readelf -Ws /lib/x86_64-linux-gnu/libm.so.6| grep pow_finite
626: 000000000002ed90 65 IFUNC GLOBAL DEFAULT 17 __pow_finite@GLIBC_2.15
Run Code Online (Sandbox Code Playgroud)
gcc 没问题。知道这里有什么问题吗?
C++ 也有同样的问题:
#include <cmath>
int main()
{
double x=1, y=1;
x = pow(x,y);
}
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上使用了-lm,我只是忘记放入文本。如果我不添加它,则是另一个错误。
$ clang-9 test.c
/usr/bin/ld: /tmp/test-3389a6.o: in function `main':
test.c:(.text+0x25): undefined …Run Code Online (Sandbox Code Playgroud) 我正在使用gcc 4.6.1.
代码段:
int main(void)
{
int x= 2;
int y = pow(3,x);
printf("%d\n",y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
终奌站:
avinash@titanic:~/Desktop/DSF$ gcc power.c -o power
/tmp/ccTJ7vAH.o: In function `main':
power.c:(.text+0x25): undefined reference to `pow'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我将x替换为2,那么它将以预期的方式执行.pow函数不接受变量作为参数吗?
注意:我在源文件中包含了stdio.h和math.h.
#include <stdio.h>
#include <math.h>
main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
double ans= pow(a,b);
printf("%f",ans);
}
Run Code Online (Sandbox Code Playgroud)
我写了这段代码并尝试在 Visual stdio (linux) 中运行。但它给出了一条错误消息。
味精- new_year_candles.c:4:1: warning: return type defaults to ‘int’ [-Wimplicit-int] 4 | main() | ^~~~ /usr/bin/ld: /tmp/ccCQbbLV.o: in function `main': new_year_candles.c:(.text+0x4a): undefined reference to `pow' collect2: error: ld returned 1 exit status
现在做什么?