【问题标题】:How to fix linker error when compiling XV6?编译 XV6 时如何修复链接器错误?
【发布时间】:2018-11-26 00:32:22
【问题描述】:

我的 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() 未定义的引用错误。我知道我需要为 XV6 更改 Makefile 中的某些内容,但我不知道是什么。这就是我在 XV6 Makefile 中所做的更改:

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

【问题讨论】:

  • 向我们展示整个makefile,以及makefile生成了哪些编译和链接命令。还有,foo.c 和 testmain.c 是不是在同一个目录下?
  • @Leonard github.com/jeffallen/xv6/blob/master/Makefile 是 XV6 制作文件。但是,如上所述,我更改了 UPROGS。这是编译错误:未定义对 'foo' 的引用 - 是的,它们在同一个目录中
  • 好吧,我没有运行 XV6,所以我无法对此进行测试,但请尝试将 foo.o 添加到顶部的 OBJ 列表中。

标签: c xv6


【解决方案1】:

您需要对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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多