Undefined reference error to function that is actu

2020-05-03 12:04发布

问题:

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): undefined reference to `get_input'

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("\n");

//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("\n");
}

//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("\n");
}




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.\n");
return (0);
}

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

//less than 1 cnd

if (value < 1){
printf("You have entered a number less than 1. Please try again.\n");
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;
}
}

回答1:

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.

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.

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)


回答2:

The function print_pattern is not terminated at proper place but instead at the very end of the file:

void print_pattern(int size){
 ...
... end of the loop
}

... more functions
...
... end of print_pattern
}

This results into defining nested functions instead of global level.

It's generally good habit to indent the blocks, in which case you would realized the mistake very quickly.