用户空间中的文件系统 (FUSE) 编译错误

File System in Userspace (FUSE) compilation error

/*This is a simple try to create a File System in UserSpace 
The pre_init function just initializes the filesystem */
#include<linux/fuse.h>
#include<stdio.h>
#include<stdlib.h>
#include<fuse_lowlevel.h>


static void* pre_init(struct fuse_conn_info *conn, struct fuse_config *cfg){
        printf("[init] called\n");
        (void) conn;
        return NULL;
}
static struct fuse_operations opr = {
        .init = pre_init,
};
int main(int argc, char *argv[]){
        return fuse_main(argc, argv, &opr, NULL);
}

我正在尝试使用 gcc sample.c -o sample `pkg-config fuse --cflags --libs` 编译代码,我得到了一个正如我所展示的那样,代码中存在很多错误

 sample.c:7:59: warning: ‘struct fuse_config’ declared inside parameter list will not be visible outside of this definition or declaration
 static void* pre_init(struct fuse_conn_info *conn, struct fuse_config *cfg){
                                                           ^~~~~~~~~~~
 sample.c:12:15: error: variable ‘opr’ has initializer but incomplete type
 static struct fuse_operations opr = {
               ^~~~~~~~~~~~~~~
 sample.c:13:3: error: ‘struct fuse_operations’ has no member named ‘init’
  .init = pre_init,
   ^~~~
 sample.c:13:10: warning: excess elements in struct initializer
  .init = pre_init,
          ^~~~~~~~
 sample.c:13:10: note: (near initialization for ‘opr’)
 sample.c: In function ‘main’:
 sample.c:16:9: warning: implicit declaration of function ‘fuse_main’; did you mean ‘fuse_mount’? [-Wimplicit-function-declaration]
 return fuse_main(argc, argv, &opr, NULL);
         ^~~~~~~~~
         fuse_mount
 sample.c: At top level:
 sample.c:12:31: error: storage size of ‘opr’ isn’t known
 static struct fuse_operations opr = {
                               ^~~

我还检查了 fuse 是否正确安装,因为头文件已包含在其中,没有任何问题。但是为什么我不能编译这个简单的代码呢?

有两个 "fuse" 版本,有时会相互共存:fuse2 和 fuse3。他们不同。在我的 Archlinux 中有两个 fuse 包:fuse2 和 fuse3。在我的系统上,文件 /usr/include/fuse.h 只包含 fuse/fuse.hfuse/fuse.h 来自 fuse2 包。 Header fuse3/fuse.h 来自 fuse3.
无论如何,你想在这里使用 fuse3 api 就像你使用 struct fuse_config 一样。 fuse3 定义 struct fuse_config.
但是,最重要的是,在包含任何保险丝 header 文件之前定义 FUSE_USE_VERSION 宏,如 fuse.h from fuse2 and in the fuse.h from fuse3:

开头所述
IMPORTANT: you should define FUSE_USE_VERSION before including this header.

以下在我的平台上使用 gcc -Wall -pedantic -lfuse3 1.c 编译时没有 warnings/errors:

#define FUSE_USE_VERSION 31
#include <fuse3/fuse.h>
#include <stdio.h>
#include <stdlib.h>

static void* pre_init(struct fuse_conn_info *conn, struct fuse_config *cfg){
        printf("[init] called\n");
        (void) conn;
        return NULL;
}
static struct fuse_operations opr = {
        .init = pre_init,
};
int main(int argc, char *argv[]){
        return fuse_main(argc, argv, &opr, NULL);
}