相关疑难解决方法(0)

未定义的引用`pow'和`floor'

我正在尝试在C中制作一个简单的斐波那契计算器,但在编译时gcc告诉我,我错过了战俘和地板功能.怎么了?

码:

#include <stdio.h>
#include <math.h>

int fibo(int n);

int main() {
        printf("Fib(4) = %d", fibo(4));
        return 0;
}

int fibo(int n) {
        double phi = 1.61803399;

        return (int)(floor((float)(pow(phi, n) / sqrt(5)) + .5f));
}
Run Code Online (Sandbox Code Playgroud)

输出:

gab@testvm:~/work/c/fibo$ gcc fib.c -o fibo
/tmp/ccNSjm4q.o: In function `fibo':
fib.c:(.text+0x4a): undefined reference to `pow'
fib.c:(.text+0x68): undefined reference to `floor'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c gcc undefined-reference

117
推荐指数
5
解决办法
18万
查看次数

C - 对"sqrt"的未定义引用,即使使用'-lm'

我尝试在C中编译一个需要"math.h"的库,这里是.c文件的开头:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h> 
#include "sparse_matrix.h"
...
Run Code Online (Sandbox Code Playgroud)

我用这个命令编译:

gcc -c ./sparse_matrix.c -o sparse_matrix.o -lm -Wall -pedantic -std=c99 -g -O
Run Code Online (Sandbox Code Playgroud)

但即使文件后面的#include完成和标志-lm(我已尝试在行尾但没有改变)我仍然得到错误: undefined reference to « sqrt » collect2: error: ld returned 1 exit status

谷歌搜索一小时后,我没有得到它.我在ubuntu 14.10(utopic unicorn)下使用gcc 4.9.提前感谢您的帮助!

c gcc math.h ld

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

在Math.h中调用函数时为什么会出现链接错误?

当试图调用函数时math.h,我收到如下链接错误

undefined reference to sqrt
Run Code Online (Sandbox Code Playgroud)

但我正在做一个#include <math.h>
我正在使用gcc并编译如下:

gcc -Wall -D_GNU_SOURCE blah.c -o blah
Run Code Online (Sandbox Code Playgroud)

为什么链接器找不到定义sqrt

math linker gcc

5
推荐指数
1
解决办法
7301
查看次数

Unable to include cmath using GCC 5.5 on Solaris 10

I am trying to run the following test program on my Solaris 10 sparc machine using gcc 5.5.0

#include <iostream>
#include <cmath>

int main()
{
    std::cout << "exp2(4) = " << std::exp2(4) << '\n'
              << "exp2(0.5) = " << std::exp2(0.5) << '\n'
              << "exp2(-4) = " << std::exp2(-4) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Here are the OS details,

~$ uname -a
SunOS sovms577 5.10 Generic_147147-26 sun4v sparc SUNW,SPARC-Enterprise-T5220
~$ cat /etc/release 
                   Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC
  Copyright (c) 1983, …
Run Code Online (Sandbox Code Playgroud)

c++ gcc solaris solaris-10

5
推荐指数
1
解决办法
659
查看次数

标签 统计

gcc ×4

c ×2

c++ ×1

ld ×1

linker ×1

math ×1

math.h ×1

solaris ×1

solaris-10 ×1

undefined-reference ×1