错误:未定义的数学函数参考 [英] Error: undefined reference to math functions

查看:176
本文介绍了错误:未定义的数学函数参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是函数指针程序:demo.c

This is function pointer program: 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 * incr;
        printf("%10.5f %10.5f\n", x, (*f)(x));
    }
}

当我尝试运行此代码时, :

When I try to run this code, got this message:

/tmp/ccQ4IZeD.o: In function `main':
demo.c:(.text+0x92): undefined reference to `cos'
/tmp/ccQ4IZeD.o: In function `tabulate':
demo.c:(.text+0xe3): undefined reference to `ceil'
collect2: error: ld returned 1 exit status

这意味着什么,因为cos和ceil是< math.h> 文件功能。
这是因为操作系统(ubuntu 13.10 32位),因为这个程序是在其他系统上工作。

What does it means, because cos and ceil is <math.h> file function. Is this is because of OS (ubuntu 13.10 32-bits), because this program is work on else system.

如果是,那么什么解决方案是在我的系统上运行。

If it yes, then what solution is to run on my system.

推荐答案

您必须使用 -lm 在链接时。

这篇关于错误:未定义的数学函数参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆