首页 > 解决方案 > cs50 第 1 周课程中未定义的参考

问题描述

我报名参加了CS50。我正在从事第 1 周的项目。程序顶部的#include 应该已经定义了某些程序,例如get_string、get_int 等……在我进入项目之前,我运行了一个简单的测试以确保一切正常。

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

int main(void)
    {
        printf("What is your name?\n");
        string x = get_string("x");
        printf("Hello, %x!");
    }  

我的代码在上面和下面是我不断收到的错误。

undefined reference to `get_string'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

当我输入 #include 时,这些错误不是已经引用了吗?

标签: ccs50

解决方案


那是链接器错误,而不是编译器错误。问题是您包含cs50.h,它具有声明get_string,但您没有包含具有其定义的库。要修复它,请传递-lcs50命令行。


推荐阅读