I need help building a trivial C program that uses libzip. I have a much more complex version, but here I'm asking about the simplest possible ZIP file program to make it easy to see what the problem is.
I tried downloading and building libzip and it didn't help with this problem. For purposes of this question, I'm giving the simplest possible set of steps to make it easy for you to help me.
First I installed necessary packages:
$ sudo apt install -y gcc libzip-dev
According to the Ubuntu package definition, here are the critical files included in libzip-dev:
/usr/include/zip.h
/usr/include/zipconf.h
/usr/lib/x86_64-linux-gnu/cmake/libzip/libzip-config-version.cmake
/usr/lib/x86_64-linux-gnu/cmake/libzip/libzip-config.cmake
/usr/lib/x86_64-linux-gnu/cmake/libzip/libzip-targets-none.cmake
/usr/lib/x86_64-linux-gnu/cmake/libzip/libzip-targets.cmake
/usr/lib/x86_64-linux-gnu/libzip.so
Here's my test program.
$ cat sample.c
#include <stdio.h>
#include <zip.h>
int main(int argc, char *argv[])
{
char file_name[] = "my_zip_file.zip";
int err;
int FLAGS=0;
struct zip *Zip = zip_open(file_name, FLAGS, &err);
if (Zip == NULL) {
printf("Cannot open ZIP %s archive: %d\n", argv[1], err);
return err;
}
zip_close(Zip);
printf("success\n");
return 0;
}
Now I try to compile the program and it doesn't work. No combination of options seems to help. Here is the most promising thing I've tried so far:
$ gcc -I/usr/include -L/usr/lib/x86_64-linux-gnu -lzip sample.c
/usr/bin/ld: /tmp/ccemzluk.o: in function `main':
sample.c:(.text+0x56): undefined reference to `zip_open'
/usr/bin/ld: sample.c:(.text+0x97): undefined reference to `zip_close'
collect2: error: ld returned 1 exit status
I need help figuring out this basic library/linking problem. I've tried this on Ubuntu 22.04 and also on a clean installation of 24.04, with the same result in both cases.
Can you please do it as below.
gcc -c -I/usr/include -L/usr/lib/x86_64-linux-gnu -lzip sample.c
gcc -o "sample" ./sample.o -lzip
In first step compile, in second step build the executable.