Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

c - Error message: Undefined reference to 'print' function

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

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

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...