我有以下代码(根据这个问题的基础知识):
#include<stdio.h>
#include<math.h>
double f1(double x)
{
double res = sin(x);
return 0;
}
/* The main function */
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当gcc test.c我编译它时,我得到以下错误,我无法解决原因:
/tmp/ccOF5bis.o: In function `f1':
test2.c:(.text+0x13): undefined reference to `sin'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,我编写了各种sin从main函数内部调用的测试程序,并且这些程序完美地工作.我必须在这里做一些明显错误的事 - 但它是什么?
我将Ubuntu升级(10.11, 11.04 i dont know)到11.10 后出现了一些奇怪的错误.
我正在undefined reference to 'sqrt'使用math.h 并使用-lm进行链接
我正在编译gcc -Wall -Werror -g -Iinclude/ -lm lib/matrix.c src/analyse.c -o bin/analyse.o两个源文件使用并包括math.h.
这段代码编译没有问题,自升级以来我没有太大变化,但现在它不起作用.
你有什么建议我可以做,找到错误?
对不起,如果以前问过这个问题; 关于数学链接器错误的帖子太多了,我找不到匹配的错误
我试图测试某些功能fenv.h,但是,当我编译如下功能LD失败,undefined reference to 'feclearexcept'和undefined reference to 'fetestexcept'。我正在运行针对 uclibc 编译的强化 gentoo,我怀疑这至少在某种程度上是相关的
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
#include <fenv.h>
#pragma STDC FENV_ACCESS on
int main ()
{
feclearexcept (FE_ALL_EXCEPT);
sqrt(-1);
if (fetestexcept(FE_INVALID)) printf ("sqrt(-1) raises FE_INVALID\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
fenv.h在/usr/include. 中有静态和动态库 ( libm.a, libm.so) /usr/lib。我正在编译gcc -o test test.c -lm;有没有人知道为什么链接器找不到相关函数。似乎没有fenv.h相应的库。
更新:这篇十年前的博文似乎暗示 uclibc 不支持 fenv。我无法确定是否仍然是这种情况,但如果是这样,有什么可以做的。 http://uclibc.10924.n7.nabble.com/missing-fenv-h-for-qemu-td2703.html
在 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) 我正在写一个素数发现者.在数学上,它更快,而不是for (unsigned long i = 2; i < number/2; i++)更快,并且仍然同样有效for (unsigned long i = 2; i < sqrt(number); i++)
但它不起作用.以下是我的代码.
// Stuff goes up here, including a function prototype and:
#include <math.h>
char isPrime (unsigned long number)
{
if (number <= 1) {
return 0;
}
long double sqrtOfNumber = sqrt(number); // Calculate once.
for (unsigned long i = 2; i < sqrtOfNumber; i++) {
if (number%i == 0) { // It has a divisor.
return 0; …Run Code Online (Sandbox Code Playgroud) 这是函数指针程序:demo.c
#include <stdio.h>
#include <math.h>
void tabulate(double (*f)(double), double first, double last, double incr);
int main(void) {
double final, increment, initial;
printf("Enter initial value: ");
scanf("%lf", &initial);
printf("Enter final value: ");
scanf("%lf", &final);
printf("Enter increment: ");
scanf("%lf", &increment);
printf("\n x cos(x)"
"\n ------- -------\n");
tabulate(cos, initial, final, increment);
return 0;
}
void tabulate(double (*f)(double), double first, double last, double incr) {
double x;
int i, num_intervals;
num_intervals = ceil((last - first)/incr);
for(i=0; i<=num_intervals; i++) {
x = first + i …Run Code Online (Sandbox Code Playgroud)