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

C语言多文件跨函数调用Makefile构建失败问题排查

C多文件编译链接错误解决:未找到_call_print_hello符号

问题根源

  • 链接阶段遗漏必要目标文件:你的Makefile生成可执行文件main时,仅链接了main.o和script1.o,但call_print_hello()函数的定义在script2.c编译生成的script2.o中,链接器找不到该函数符号导致报错。
  • 额外说明:你描述的函数定义位置和实际代码相反——call_print_hello()实际在script2.c,print_hello()在script1.c,这不是当前报错的直接原因,但需对应清楚避免后续混乱。

修复方案

修改Makefile的链接规则

更新main目标的依赖和链接命令,加入script2.o:

CC = gcc 
CFLAGS = -g -Wall

# 新增script2.o作为依赖
main: main.o script1.o script2.o
    $(CC) -o main main.o script1.o script2.o 

main.o: main.c fdeclar_macros.h
    $(CC) $(CFLAGS) -c main.c

script2.o: script2.c fdeclar_macros.h
    $(CC) $(CFLAGS) -c script2.c

script1.o: script1.c fdeclar_macros.h
    $(CC) $(CFLAGS) -c script1.c

run: main
    ./main

clean:
    $(RM) -rf justify *.dSYM *.o

验证执行

修改后执行make run,会自动编译所有文件并完成链接,最终正常输出:

hello 3

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:15:34