0
/*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);
}

I am trying to compile the code using gcc sample.c -o sample `pkg-config fuse --cflags --libs` And I'm getting a whole lot of errors in the code as i have shown

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

I have also checked that fuse is installed properly as the header files have been included without any issues. But why am I not able to compile this simple code?

vignesh_md
  • 15
  • 12

1 Answers1

1

There are two "fuse" versions, sometimes coexistent with each other: fuse2 and fuse3. And they differ. In my Archlinux there are two fuse packages: fuse2 and fuse3. On my system, file /usr/include/fuse.h just includes fuse/fuse.h and fuse/fuse.h comes from fuse2 packages. Header fuse3/fuse.h comes from fuse3.
Anyway, you want to use fuse3 api here as you use struct fuse_config. fuse3 defines struct fuse_config.
But, thirst of all, define FUSE_USE_VERSION macro before including any of the fuse header files, as specified in the beginning in fuse.h from fuse2 and in the fuse.h from fuse3:

IMPORTANT: you should define FUSE_USE_VERSION before including this header.

The following compiles with no warnings/errors using gcc -Wall -pedantic -lfuse3 1.c on my platform:

#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);
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thank you! But can you tell me significance of the macro FUSE_USE VERSION ? – vignesh_md Apr 11 '18 at 17:41
  • Well, it is used to export different fuse api versions depending on the application. Depending on it's value, single header may export different api. I don't know the exact differences between apis, you would need to compare fuse api documentations or look at [libfuse](https://github.com/libfuse/libfuse) changes. The best would be to use the newest version. Look at similar solitions, posix has _POSIX_C_SOURCE or and C11 standard has __STDC_WANT_LIB_EXT2__ . – KamilCuk Apr 14 '18 at 14:38