简体   繁体   中英

Using libarchive in Qt - build errors

I've got hold of libarchive and built the examples on both Windows and Linux following the build instructions .

I now want to use this library in my project, which is Qt based, so I'm using Qt creator. I've added the include paths for libarchive in my pro file, but when I compile I get errors saying "undefined reference to ' imp _archive_read_new'" and so on.

This is my code at the moment:

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

Here is the full list or errors I get: D:\\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:16: undefined reference to _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 to imp _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: undefined reference to _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 to imp _archive_read_open_filename' D:\\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:28: undefined reference to _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 to imp _archive_entry_size' D:\\Tar-build-desktop-Qt_4_7_4_for_ Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:32: undefined reference to _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 to imp _archive_read_next_header' D:\\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:16: undefined reference to _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 to imp _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: undefined reference to _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 to imp _archive_read_open_filename' D:\\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:28: undefin ed reference to _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 to imp _archive_entry_size' D:\\Tar-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug/../Tar/TarFileManager.cpp:32: undefined reference to _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 to imp _archive_read_next_header'

As I suspected, it's a linking problem. You do not actually link with the library. I don't know how you do it in Qt Creator, but you have to add the flags -L/path/to/library/folder and -lname_of_library to the linking stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM