macOS Ventura下CS50 get_string链接错误:linker exit code 1
解决CS50 C语言课程中
make命令的链接错误 问题场景
在macOS Ventura 13.1系统下学习哈佛CS50的C语言课程,执行make hello时触发以下链接错误:
Undefined symbols for architecture arm64: "_get_string", referenced from: _main in hello-c42e93.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [hello] Error 1
对应的hello.c代码:
#include <cs50.h> #include <stdio.h> int main(void) { string answer = get_string("What's your name? "); printf("hello, answer\n"); }
已按CS50官方库的步骤完成cs50.h库的安装。
解决方案
原因分析
错误提示_get_string符号未找到,本质是编译时没有正确链接CS50库——make默认的编译规则不会自动添加CS50库的链接参数,导致编译器无法找到该函数的实现。
解决方法
方法1:直接用clang手动编译
执行以下命令,显式指定链接CS50库:
clang hello.c -o hello -lcs50
-lcs50参数用于告诉编译器链接CS50库,这样就能找到get_string的实现。
方法2:自定义Makefile适配链接需求
如果习惯用make命令,可创建/修改Makefile文件,添加链接参数:
hello: hello.c clang hello.c -o hello -lcs50
保存后执行make hello即可正常编译。
额外代码修正
你的printf语句存在问题,当前会固定输出hello, answer,若要打印输入的名字,需修改为:
printf("hello, %s\n", answer);
内容的提问来源于stack exchange,提问作者Hakor
相关产品推荐
相关产品推荐

