【问题标题】:Using libarchive in Qt - build errors在 Qt 中使用 libarchive - 构建错误
【发布时间】:2012-01-10 12:54:25
【问题描述】:

我已经掌握了libarchive,并按照build instructions 在 Windows 和 Linux 上构建了示例。

我现在想在我的项目中使用这个库,它是基于 Qt 的,所以我正在使用 Qt creator。我在我的 pro 文件中添加了 libarchive 的包含路径,但是当我编译时出现错误说“未定义对 'imp_archive_read_new' 的引用”等等。

这是我目前的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    #include "archive.h"
    #include "archive_entry.h"

    cTarFileManager::cTarFileManager()
    {
      struct archive *a;
      struct archive_entry *entry;
      int r;
      int64_t entry_size;
      a = archive_read_new();
      archive_read_support_compression_none(a);
      archive_read_support_format_tar(a);
      r = archive_read_open_filename(a, "0000.tar", 1024);
      if (r != ARCHIVE_OK)
      {
        printf("archive not found");
      }
      else
      {
        while (archive_read_next_header(a, &entry) == ARCHIVE_OK)
        {
          const char *currentFile = archive_entry_pathname(entry);
          char *fileContents;
          entry_size = archive_entry_size(entry); //get the size of the file
          fileContents = (char*)malloc(entry_size); //alloc enough for string - from my testing I see that this is how many bytes tar and ls report from command line
          archive_read_data(a, fileContents, entry_size); //read data into fileContents string for the HTML file size
          if(strcmp(currentFile, "vendar-definition.html") == 0)
          {
            printf("file name = %s, size = %ld\n", currentFile, entry_size);
            printf("%s\n\n", fileContents); //this output over-reads chars from another file in this tar file
          }
          free(fileContents); //free the C string because I malloc'd
        }
      }
      printf("exit");
    }

这是我得到的完整列表或错误: D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:16: 未定义引用_imp__archive_read_new' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:17: undefined reference toimp_archive_read_support_compression_none' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:18: 未定义引用_imp__archive_read_support_format_tar' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:19: undefined reference toimp_archive_read_open_filename' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:28: 未定义引用_imp__archive_entry_pathname' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:30: undefined reference toimp_archive_entry_size' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:32: 未定义引用_imp__archive_read_data' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:26: undefined reference toimp_archive_read_next_header' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:16: 未定义引用_imp__archive_read_new' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:17: undefined reference toimp_archive_read_support_compression_none' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:18: 未定义引用_imp__archive_read_support_format_tar' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:19: undefined reference toimp_archive_read_open_filename' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:28: 未定义引用_imp__archive_entry_pathname' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:30: undefined reference toimp_archive_entry_size' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop__-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:32: 未定义引用_imp__archive_read_data' D:\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:26: undefined reference toimp_archive_read_next_header'

【问题讨论】:

  • 您是否也更新了库路径,并与库链接?如果不发布实际错误,很难说什么。

标签: c++ qt archive tar


【解决方案1】:

正如我所怀疑的,这是一个链接问题。您实际上并没有与库链接。我不知道你在 Qt Creator 中是怎么做的,但你必须在链接阶段添加标志 -L/path/to/library/folder-lname_of_library

【讨论】:

  • 谢谢!我刚刚设法在我的 Linux 系统上正确链接它,现在只需要让它在 Windows 上运行......
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多