在 C 中从头文件调用函数

问题描述 投票:0回答:1

在 xv6 中,我在内核文件夹中有

hello.h
,在用户文件夹中有
main.c
。 错误在
main.c

内核/hello.h:

void hello();

内核/hello.c:

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

现在我想调用函数

hello()
。但出现错误。

用户/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);
}
c operating-system riscv xv6
1个回答
1
投票

您需要编译

hello.c
main.c
文件 将它们链接在一起。

gcc -o myprog main.c hello.c

./myprog
© www.soinside.com 2019 - 2024. All rights reserved.