coperating-systemriscvxv6

call a function from header file in C


In xv6, I have hello.h inside kernel folder, main.c inside user folder. The error is in main.c.

kernel/hello.h:

void hello();

kernel/hello.c:

void hello(){
    printf("hello\n);
}

Now I want to call function hello(). But get error.

user/main.c:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/hello.h"

int main(int argc, char *argv[]) {
  hello(); // error here (undefined reference to `hello')
  exit(0);
}

Solution

  • You need to compile hello.c and main.c files and link them together.

    gcc -o myprog main.c hello.c
    
    ./myprog