简体   繁体   中英

How to fix linker error when compiling XV6?

I have three files in my XV6: testmain.c, foo.h, and 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 ; }

I am getting undefined reference error for myfunction() in test_main . I know I need to change something in Makefile for XV6, but I don't know what. that's what I have changed in XV6 Makefile:

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

You need several changes in Makefile :

  • Indicate that you want to create _testmain program,
  • Tell what _testmain dependencies are (if apply).

add _testmain in programs list:

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

_testmain dependencies:

Since your _testmain program depends on two files, you must create a special rule telling that to builder (I make this rule from _%: %.o $(ULIB) rule):

_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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM