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

时间:2018-04-07 05:32:04

标签: c linux compiler-errors filesystems userspace

/*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 = {
                               ^~~

我还检查过保险丝安装正确,因为包含头文件没有任何问题。但为什么我无法编译这个简单的代码?

1 个答案:

答案 0 :(得分:0)

有两个&#34;保险丝&#34;版本,有时彼此共存:fuse2和fuse3。他们不同。在我的Archlinux中有两个保险丝包:fuse2和fuse3。在我的系统上,文件/usr/include/fuse.h只包含fuse/fuse.h,而fuse/fuse.h来自fuse2包。标题fuse3/fuse.h来自fuse3 无论如何,当你使用struct fuse_config时,你想在这里使用fuse3 api。 fuse3定义struct fuse_config
但是,最重要的是,在包含任何融合头文件之前定义FUSE_USE_VERSION宏,如fuse.h from fuse2fuse.h from fuse3中的开头所述:

IMPORTANT: you should define FUSE_USE_VERSION before including this header.

以下编译在我的平台上使用gcc -Wall -pedantic -lfuse3 1.c没有警告/错误:

#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);
}