Undefined reference error to function that is actually defined in C

后端 未结 2 2010
心在旅途
心在旅途 2020-12-22 08:21

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 refe

相关标签:
2条回答
  • 2020-12-22 08:55

    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.

    0 讨论(0)
  • 2020-12-22 08:57

    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)
    
    0 讨论(0)
提交回复
热议问题