0

I'm trying to compile source code that uses libzip to extract files. I'm able to compile the code in Code Blocks with no issue however when I try to compile the code in the terminal I get the error listed below. I need it to run in the terminal.

I have tried googling for resolutions with regards to how to link libraries in linux but I'm still stuck at the same spot and have made no progress. I'm new to linux as well as working with libzip/zlib. I would greatly appreciate it if someone could explain how to approach this issue step-by-step.

The code that I am trying to compile was provided by Mobius on Github(link: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;
}

I'm mostly focused on trying to understand why I am getting all of the undefined reference errors. I have installed both libzip and zlib(both installed from a zip file). Any help or guidance will be greatly appreciated!

JonSnow
  • 67
  • 3
  • 14

0 Answers0