Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been encountering this problem when I try to compile in C. When I asked help50 for help, it gave me this message "By "undefined reference," clang means that you've called a function, print, that doesn't seem to be implemented. If that function has, in fact, been implemented, odds are you've forgotten to tell clang to "link" against the file that implements print. Did you forget to compile with -lfoo, where foo is the library that defines print?" Because of this, I decided to implement #include <foo.h>, however after I tried to compile, I received a fatal error message. This is my code

#include <cs50.h>
#include <stdio.h>

void print(char c, int n);


//Code
int main(void) 
{
     int n;
     do
     {
         n = get_int("Height:");
     } while(n < 1 || n > 8);
     
     for(int i = 0; i < n; i++)
     {
         print(' ', n - 1 - i);
         print('#', i + 1);
         print(' ', 2);
         print('#', i + 1);
         printf("
");
     }
}

`
question from:https://stackoverflow.com/questions/65928654/error-message-undefined-reference-to-print-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have declared print... But where is the implementation?

The declaration is just a promise to the compiler that you have a function that take some parameters of a given type and return something a given type.

When the compiler sees you calling the function it will how to report errors if the types don't match...

The implementation is where the compiler knows what to do with the parameters in order to return something from that function.

Here is a sample implementation:

void print(char c, int n)
{
   printf("My char is %c and my int is %d
", c, n);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...