C语言 c 中对函数错误的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20020776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:18:21  来源:igfitidea点击:

undefined reference to function error in c

cundefined

提问by heidi boynww

#include <stdio.h>


int singleFib(int x,int a,int b);
int multiFib(int x);


void main(){
int n,i;
printf("How much?\n");
scanf("%d",&n);

for(i=0;i<n;i++){
    printf("%5d. number:   %d   -  %d \n",i+1,multiFib(i),singleFib(i,1,1));


}

getch();
return 0;

}

int multiFib(int x){

if (x<2){
    return 1;
}
else{
    return multiFib(x-2)+multiFib(x-1);
}


int singleFib(int x,int a,int b){

if (x<2){
    return 1;
}
else{

    return singleFib( x-1, b,(a+b));
}

}
}

The error is in

错误在

singleFib(i,1,1) in `printf`

Whyis that problem?how can i solve that problem? i am using codeblocks

为什么是这个问题?我该如何解决这个问题?我正在使用代码块

Codeblocks\Fiberonacci\main.c|14|undefined reference to `singleFib'| The error is that.how can i solve?

Codeblocks\Fiberonacci\main.c|14|对`singleFib'的未定义引用| 错误是那个。我该如何解决?

回答by godel9

  1. You're missing a close bracket }at the end of your multiFibfunction.

  2. You have an extra close bracket }at the end of your singleFibfunction.

  3. The function mainshould have return type int.

  1. 您在函数}末尾缺少一个右括号multiFib

  2. }singleFib函数的末尾有一个额外的右括号。

  3. 函数main应该有返回类型int

回答by heidi boynww

The {and }in multiFiband singleFibare mixed up, singleFibis declared inside multiFib:

{}multiFibsingleFib混合起来,singleFib声明内multiFib

int multiFib(int x){

    if (x<2){
        return 1;
    }
    else{
        return multiFib(x-2)+multiFib(x-1);
    }


    /*************************************/
    int singleFib(int x,int a,int b) {

        if (x<2){
            return 1;
        }
        else{

            return singleFib( x-1, b,(a+b));
        }
    }

    /*************************************/
}

it works in gcc, since it's an nonstandard nested functionsextension, but the function will not be accessible outside multiFib.

它适用于 gcc,因为它是一个非标准的嵌套函数扩展,但该函数在multiFib.

回答by aks

Seems like there is a mismatch in the braces of the function definition

函数定义的大括号似乎不匹配

回答by Eutherpy

You're missing a closing bracket for multiFib, but have one that you don't need after singleFib.

您缺少 的右括号multiFib,但有一个您在 之后不需要的括号singleFib