libext2fs gcc linking error

Help
gregoose
2014-03-12
2014-03-12
  • gregoose

    gregoose - 2014-03-12

    I am writing a C program which uses libext2fs:

    #include <ext2fs/ext2fs.h>
    
    int main(int argc, char *argv[]) 
    { 
        char *fsname = argv[1];
        errcode_t err;
        ext2_filsys fs;
    
        err= ext2fs_open(fsname, 0, 0, 0, unix_io_manager, &fs);
    

    When I compile the program I get following errors:

    $ gcc -lext2fs fstest.c -o fstest
    /tmp/ccXCSICx.o: In function `main':
    openfs.c:(.text+0x1e): undefined reference to `unix_io_manager'
    openfs.c:(.text+0x43): undefined reference to `ext2fs_open'
    collect2: error: ld returned 1 exit status
    

    System:

    • Ubuntu 12.10 64-bit
    • gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
    • e2fsprogs, e2fslibs, e2fslibs-dev: 1.42.5-1ubuntu2
     
  • Theodore Ts'o

    Theodore Ts'o - 2014-03-12

    The order in which libraries are placed on the gcc (or more pedantically the linker, since gcc is passing its arguments to ld). So instead of:

    $ gcc -lext2fs fstest.c -o fstest

    You need to do this:

    gcc fstest.c -o fstest -lext2fs

    And since you'll probably want to use user-readable error messages, you'll want to add to main():

    add_error_table(&et_ext2_error_table);
    

    ... and then check the error code and print the result:

    if (err) {
        com_err(argv[0], err, "while opening filesystem %s", fsname);
        exit(1);
    }
    

    And then you'll need to make sure you've linked in the com_err library.

    gcc fstest.c -o fstest -lext2fs -lcom_err

     
  • gregoose

    gregoose - 2014-03-12

    Thanks! It compiles now.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks