0

I have a simple C program . and I'm trying to use libarchive inside it . the problem is the linker does not work probably so I can just include archive.h header without error but can't use any method inside the app as i get undefined reference error. here is my code:

#include <stdio.h>
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>


int main(){

    return 0;

}
write_archivec(const char *outname, const char *inner, const char **ls1, const char **ls2)
{
    struct archive *a;
    struct archive_entry *entry;
    struct stat st;
    char buff[8192];
    int len;
    int file;
    a = archive_write_new();
    archive_write_add_filter_gzip(a);
    archive_write_set_format_pax_restricted(a); // Note 1
    archive_write_open_filename(a, outname);
    while (*ls1)
    {
        char tem[80];
        strcpy(tem,inner);
        strcat(tem,"/");
        //tem = (char*)realloc(tem,strlen("symlinkpath"));
        strcat(tem,*ls2);
        stat(*ls1, &st);
        entry = archive_entry_new(); // Note 2
        archive_entry_set_pathname(entry, &tem);
        archive_entry_set_size(entry, st.st_size); // Note 3
        archive_entry_set_filetype(entry, AE_IFREG);
        archive_entry_set_perm(entry, 0644);
        archive_write_header(a, entry);
        file = open(*ls1, O_RDONLY);
        if(file == -1){
            void Unlock();
        }
        len = read(file, buff, sizeof(buff));
        while (len > 0)
        {
            archive_write_data(a, buff, len);
            len = read(file, buff, sizeof(buff));
        }
        close(file);
        archive_entry_free(entry);
        ls1++;
        ls2++;
    }
    archive_write_close(a);
    archive_write_free(a);
}

and the output will be :

$ gcc -I/usr/local/include -L/usr/local/lib -larchive t.c -o a
/usr/bin/ld: /tmp/cclrnMld.o: in function `write_archivec':
t.c:(.text+0x62): undefined reference to `archive_write_new'
/usr/bin/ld: t.c:(.text+0x78): undefined reference to `archive_write_add_filter_gzip'
/usr/bin/ld: t.c:(.text+0x87): undefined reference to `archive_write_set_format_pax_restricted'
/usr/bin/ld: t.c:(.text+0xa0): undefined reference to `archive_write_open_filename'
/usr/bin/ld: t.c:(.text+0x12f): undefined reference to `archive_entry_new'
/usr/bin/ld: t.c:(.text+0x14f): undefined reference to `archive_entry_set_pathname'
/usr/bin/ld: t.c:(.text+0x168): undefined reference to `archive_entry_set_size'
/usr/bin/ld: t.c:(.text+0x17c): undefined reference to `archive_entry_set_filetype'
/usr/bin/ld: t.c:(.text+0x190): undefined reference to `archive_entry_set_perm'
/usr/bin/ld: t.c:(.text+0x1a9): undefined reference to `archive_write_header'
/usr/bin/ld: t.c:(.text+0x211): undefined reference to `archive_write_data'
/usr/bin/ld: t.c:(.text+0x258): undefined reference to `archive_entry_free'
/usr/bin/ld: t.c:(.text+0x28a): undefined reference to `archive_write_close'
/usr/bin/ld: t.c:(.text+0x299): undefined reference to `archive_write_free'
collect2: error: ld returned 1 exit status.

I also followed install instruction in here

https://github.com/libarchive/libarchive/wiki/BuildInstructions#using-configure-for-building-from-the-command-line-on-linux-freebsd-solaris-cygwin-aix-interix-mac-os-x-and-other-unix-like-systems

Sina Gz
  • 31
  • 6

2 Answers2

0

Because archive_... functions are not standard C, you need to explicitly tell the compiler where it can find the library.

You typically do this with the -l compiler flag. You can use the -L flag to specify the path of the libraries. Check the documentation of your compiler.

But you can also link the libraries explicitly by specifying their full path.

The first question to answer is: Where is your library installed?

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
0

Most dummy mistake from me or bug from gcc?

I find out that I should specify link and include argument after the file name so :

$ gcc -I/usr/local/include -L/usr/local/lib -larchive t.c -o a
should replaced with:
$ gcc t.c -I/usr/local/include -L/usr/local/lib -larchive -o a
Sina Gz
  • 31
  • 6
  • @HolyBlackCat so why it does not throw an error about it? – Sina Gz Jun 17 '21 at 07:21
  • 1
    It did give you an error, otherwise you wouldn't be here. :P `-l...` only apply to source files (and other `-l...`) to the left of them. This supposedly reduces the amount of work for the linker, although some more modern linkers like LLD don't have this limitation. – HolyBlackCat Jun 17 '21 at 08:15