0

我正在尝试编译使用 libzip 提取文件的源代码。 我可以毫无问题地编译代码块中的代码,但是当我尝试在终端中编译代码时,出现下面列出的错误。我需要它在终端中运行。

我曾尝试在谷歌上搜索有关如何在 linux 中链接库的解决方案,但我仍然停留在同一个地方并且没有取得任何进展。我是 linux 新手,并且正在使用 libzip/zlib。如果有人可以逐步解释如何解决此问题,我将不胜感激。

我正在尝试编译的代码由 Mobius 在 Github 上提供(链接:https ://gist.github.com/mobius/1759816 )

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <zip.h>
const char *prg;

static void safe_create_dir(const char *dir)
{
    if (mkdir(dir, 0755) < 0) {
        if (errno != EEXIST) {
            perror(dir);
            exit(1);
        }
    }
}

int main(int argc, char *argv[])
{
 const char *archive;
 struct zip *za;
 struct zip_file *zf;
 struct zip_stat sb;
 char buf[100];
 int err;
     int i, len;
     int fd;
     long long sum;
 prg = argv[0];
 if (argc != 2) {
     fprintf(stderr, "usage: %s archive/n", prg);
     return 1;
 }

 archive = argv[1];
 if ((za = zip_open(archive, 0, &err)) == NULL) {
     zip_error_to_str(buf, sizeof(buf), err, errno);
     fprintf(stderr, "%s: can't open zip archive `%s': %s/n", prg,
         archive, buf);
     return 1;
 }

 for (i = 0; i < zip_get_num_entries(za, 0); i++) {
    if (zip_stat_index(za, i, 0, &sb) == 0) {
        printf("==================/n");
        len = strlen(sb.name);
        printf("Name: [%s], ", sb.name);
        printf("Size: [%llu], ", sb.size);
        printf("mtime: [%u]/n", (unsigned int)sb.mtime);
        if (sb.name[len - 1] == '/') {
            safe_create_dir(sb.name);
        } else {
            zf = zip_fopen_index(za, i, 0);
            if (!zf) {
                fprintf(stderr, "boese, boese/n");
                exit(100);
            }

            fd = open(sb.name, O_RDWR | O_TRUNC | O_CREAT, 0644);
            if (fd < 0) {
                fprintf(stderr, "boese, boese/n");
                exit(101);
            }

            sum = 0;
            while (sum != sb.size) {
                len = zip_fread(zf, buf, 100);
                if (len < 0) {
                    fprintf(stderr, "boese, boese/n");
                    exit(102);
                }
                write(fd, buf, len);
                sum += len;
            }
            close(fd);
            zip_fclose(zf);
        }
      } else {
         printf("File[%s] Line[%d]/n", __FILE__, __LINE__);
     }
 }   

 if (zip_close(za) == -1) {
    fprintf(stderr, "%s: can't close zip archive `%s'/n", prg, archive);
     return 1;
  }

  return 0;
}

我主要专注于试图理解为什么我会得到所有未定义的参考错误。我已经安装了 libzip 和 zlib(都从 zip 文件安装)。 任何帮助或指导将不胜感激!

4

0 回答 0