错误消息:未定义对 'print' 函数的引用

Error message: Undefined reference to 'print' function

我在尝试用 C 编译时遇到了这个问题。当我向 help50 寻求帮助时,它给了我这样的消息“通过“未定义的引用”,clang 意味着你已经调用了一个函数,打印,似乎没有实现。如果那个功能有, 事实上,已经实现了,你很可能忘记告诉 clang 对实现打印的文件“link”。你忘了吗 compile with -lfoo, where foo is the library that defines print?" 因此,我决定实施 #include <foo.h>,但是在尝试编译后,我收到了一条致命错误消息。这是我的代码

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

`

C 中没有标准库 print() 函数。您实际上也没有在任何地方定义它。

你已经声明了print...但是实现在哪里?

声明只是对编译器的承诺,即您拥有一个函数,该函数采用给定类型的一些参数和 return 给定类型的某些参数。

当编译器发现您调用函数时,如果类型不匹配,它将如何报告错误...

实现是编译器知道如何处理参数以便return从该函数中得到某些东西的地方。

这是一个示例实现:

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