0

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\
mohammed
  • 21
  • 2
  • Show us the whole makefile, and what compile and link commands are generated by the makefile. Also, are foo.c and testmain.c in the same directory? – Leonard Nov 26 '18 at 00:44
  • @Leonard https://github.com/jeffallen/xv6/blob/master/Makefile is the XV6 make file. However I changed the UPROGS as I mentioned above. This is the compile error : undefined reference to 'foo' - yes they are in the same directory – mohammed Nov 26 '18 at 00:59
  • 1
    Well I'm not running XV6 so I can't test this, but try adding foo.o to the list of OBJs at the top. – Leonard Nov 26 '18 at 06:09

1 Answers1

0

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
Mathieu
  • 8,840
  • 7
  • 32
  • 45