对实际在 C 中定义的函数的未定义引用错误 [英] Undefined reference error to function that is actually defined in C

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

问题描述

我编写了这个程序来构建一个数字菱形.问题是当我编译程序时,它抛出错误

I wrote this program to build a number diamond. The issue is that when I compile the program, it throws the error

build2.c:(.text+0x5): 对`get_input'的未定义引用

build2.c:(.text+0x5): undefined reference to `get_input'

collect2:错误:ld 返回 1 个退出状态

collect2: error: ld returned 1 exit status

我已经尝试了几个小时来弄清楚问题到底是什么(例如,是否存在拼写错误或类似的问题),但函数调用看起来完全相同.我试图重命名它,将其编写为原型和实现,但似乎没有任何效果.有什么我没有看到的问题吗?

I've tried for hours to figure out what exactly the problem is (e.g. if there is a spelling mistake or something similar), but the function call looks identical. I have attempted to rename it, write it as both a prototype and as an implementation, and nothing seems to work. Is there an issue that I'm not seeing?

//Define prior to main

int is_valid(int);
int get_input(void);
void print_pattern(int);

//Main
int main(void){
        int diamond_size;
        //diamond_size = get_input();

//value from get imput method used for diamond size

        print_pattern(get_input());

        return 0;
}

void print_pattern(int size){
int length, num, i, j;

//beginning of new diamond

printf("
");

//Define each integer to work in layout of diamond
//First for loop fans out

for(i=1; i  <= size; i += 2){
        length = size-i+1;
        num =  1;
        printf("%*s", length," ");
        for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("
");
}

//second for loop fans in

for(i=size-2; i >= 1; i -= 2){
        length = size-i+1;
        num =   1;
        printf("%*s", length," ");
          for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("
");
}




int is_valid(int value){
int rem;

//uses remainder to determine if it is odd or even; an even number will not have a reaminder in this case

rem = value % 2;

if (rem == 0){
printf("You've entered a even number. Please try again.
");
return (0);
}

//greater than 9 cnd
if (value > 9){
printf("You have entered a number greater than 9. Please try again.
");
return (0);
}

//less than 1 cnd

if (value < 1){
printf("You have entered a number less than 1. Please try again.
");
return (0);
}

return (1);

}

int get_input()
{
int cont, number, valid;
cont = 1;
while (cont = 1)
{
        printf("Enter an odd number less than 9 and greater than 0 < ");
        scanf("%d", &number);
        valid = is_valid(number);
        if (valid == 1)
        {
        cont = 0;
        }

}
return number;
}
}

推荐答案

你好像有嵌套函数;这是 (a) 非标准 GCC 扩展,并且(b) 我假设嵌套的 get_input() 函数的范围是封闭函数,而不是文件范围.解决方案是将 get_input() 移动到文件范围.在print_pattern()的末尾添加一个额外的},并删除文件末尾的最后一个}.

You seem to have nested functions; this is (a) a non-standard GCC extension, and (b) I presume the scope of the nested get_input() function is the enclosing function, not the file scope. The solution is to move get_input() to file scope. At the end of print_pattern() add an extra }, and delete the final } at the end of the file.

另外,请格式化您的代码 - 现在大多数 IDE 都有整理代码的选项,并且使用正确的缩进,您可能更早看到了您的问题.

Also, please format your code - most IDEs these days have options to tidy it up, and with correct indentation you may have seen your problem earlier.

哦,作为额外的错误修复,你还有 get_input():

Oh, and as a bonus bug fix, you also have in get_input():

while (cont = 1)

这将永远是正确的 - 改用这个:

This will always be true - use this instead:

while (cont == 1)

这篇关于对实际在 C 中定义的函数的未定义引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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