gpt4 book ai didi

c - 编译 XV6 时如何修复链接器错误?

转载 作者:行者123 更新时间:2023-11-30 16:25:13 25 4
gpt4 key购买 nike

我的 XV6 中有三个文件:testmain.c、foo.h 和 foo.c:

foo.h:

extern void myfunction(void)

foo.c:

#include "foo.h"
void myfunction(void){
printf(1, "HelloWorld"); }

testmain.c:

 #include "foo.h" 
int main(void){
myfunction();
return 0 ; }

我在 test_main 中收到 myfunction() 的 undefined reference 错误。我知道我需要在 XV6 的 Makefile 中更改一些内容,但我不知道是什么。这就是我在 XV6 Makefile 中所做的更改:

UPROGS=\
_cat\
_echo\
_forktest\
_grep\
_init\
_kill\
_ln\
_ls\
_mkdir\
_rm\
_sh\
_stressfs\
_usertests\
_wc\
_zombie\
_foo\
_testmain\

最佳答案

您需要对 Makefile 进行几处更改:

  • 表明您要创建_testmain程序,
  • 说明 _testmain 依赖项是什么(如果适用)。

在程序列表中添加_testmain:

UPROGS=\
_testmain\
_cat\
_crash\
_echo\
_factor\
....

_testmain 依赖项:

由于您的 _testmain 程序依赖于两个文件,因此您必须创建一个特殊的规则,告诉构建器(我从 _%: %.o $(ULIB)规则):

_testmain: testmain.o foo.o $(ULIB)

$(LD) $(LDFLAGS) -N -e main -Ttext 0x1000 -o $@ $^
$(OBJDUMP) -S $@ > $*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym

关于c - 编译 XV6 时如何修复链接器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com