You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook运行CS50课程C代码时出现_get_int未定义符号错误

解决Mac环境下CS50代码编译的get_int链接错误

你的代码:

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

int main(void)
{
    int x = get_int("whats x? ");
    int y = get_int("whats y? ");

    if (x < y)
    {
        printf("x is less than y\n");
    }
}

编译时出现的错误:

moutarrjallow@Moutarrs-MBP cclang % make ccompare
cc     ccompare.c   -o ccompare
Undefined symbols for architecture x86_64:
  "_get_int", referenced from:
      _main in ccompare-91f0e5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [ccompare] Error 1

问题原因

get_int是CS50专属库提供的函数,默认编译命令没有链接CS50库,导致链接器找不到该函数的实现代码。

解决办法

  1. 直接修改编译命令
    不用make,改用clang编译并指定链接CS50库:

    clang ccompare.c -o ccompare -lcs50
    
  2. 让make自动链接库
    执行make前先设置环境变量,让编译时自动带上链接参数:

    LDFLAGS=-lcs50 make ccompare
    

    或者创建一个Makefile文件,内容如下,之后直接执行make即可:

    ccompare: ccompare.c
        cc ccompare.c -o ccompare -lcs50
    
  3. 确认CS50库已安装
    如果执行上述命令仍报错,说明你的Mac上还没安装CS50的C库,用Homebrew安装:

    brew install cs50
    

内容的提问来源于stack exchange,提问作者Moutarr J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:35:18