【问题标题】:cannot link and include libarchive to C preogram无法将 libarchive 链接并包含到 C 程序
【发布时间】:2021-09-01 15:28:19
【问题描述】:

我有一个简单的 C 程序。 我正在尝试在其中使用 libarchive 。 问题是链接器可能无法正常工作,所以我可以只包含 archive.h 标头而不会出错,但由于出现未定义的引用错误,因此无法在应用程序内使用任何方法。 这是我的代码:

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

输出将是:

$ 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.

我也按照这里的安装说明进行操作

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

【问题讨论】:

  • 在这种情况下,标志顺序很重要。 -l... 必须在 t.c 的右侧。

标签: c linux gcc libarchive


【解决方案1】:

因为archive_... 函数不是标准C,你需要明确告诉编译器在哪里可以找到库。

您通常使用-l 编译器标志来执行此操作。您可以使用-L 标志来指定库的路径。检查编译器的文档。

但您也可以通过指定它们的完整路径来显式链接这些库。

要回答的第一个问题是:您的库安装在哪里?

【讨论】:

  • /usr/local 的标准路径我之前也试过,但没有任何不同的输出
【解决方案2】:

我犯的大部分错误还是 gcc 的错误?

我发现我应该在文件名之后指定链接和包含参数 所以:

$ 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

【讨论】:

  • @HolyBlackCat 那么为什么它不会抛出错误呢?
  • 它确实给了你一个错误,否则你就不会在这里。 :P -l... 仅适用于它们左侧的源文件(和其他 -l...)。据说这会减少链接器的工作量,尽管一些更现代的链接器(如 LLD)没有此限制。
猜你喜欢
  • 1970-01-01
  • 2021-10-29
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多