2016-03-01 41 views
-1

为什么它显示错误像ld返回1退出状态和未定义的引用'powr(int,int)'为什么它显示错误像ld返回1退出状态和未定义的引用`powr(int,int)'

#include<stdio.h> 

int powr (int m , int n); 
int main(){ 

    int i,m,n; 
    printf("print the base\n"); 
    scanf("%d",&m); 
    printf("print the expoenent\n"); 
    scanf("%d",&n); 

    int p ; 
    if (n == 1){ 
     return m; 
    } 
    else { 
     p = powr(m,n/2); 
     if (n%2 ==0){ 
      return p*p ; 

     } 
     else { 
      return p*p*m; 
     } 
    } 
} 
+0

的可能的复制[C - 未定义参照SQRT(或其他数学函数)](http://stackoverflow.com/questions/5248919/c-undefined-reference-to-sqrt - 或者其他数学函数) –

+3

C中没有标准函数'powr(int,int)'你是否指'math.h'中的'pow(double,double)'? – Bathsheba

+0

你放了一个'powr'函数的原型,但是你没有提到它在问题中的定义。没有看到定义什么都不能说。 – haccks

回答

2

我想你想使用pow函数,它返回两个数字的幂。因为powmath.h。所以,包括它:

#include <math.h> 

虽然编译它,链接math.h

gcc program.c -lm 

-lm是其链接到math.h库。

另见:C - undefined reference to sqrt (or other mathematical functions)

+0

我认为他想写一个幂函数而不是使用现有函数。 – haccks

+0

@haccks,恩,你确定吗?我不这么认为。我会尝试制作一个,然后发布。给我一些时间。 –