i need create application extract 1 file zip archive, after want compile android.
i'm using ubuntu, libzip-0.10.1 pre-installed. created c project in eclipse, added include path , found simple script extracting file. unfortunately cannot following build , use advice.
// zip.c file #include <stdio.h> #include <stdlib.h> #include <zip.h> int main(int argc, char **argv) { struct zip *zip_file; struct zip_file *file_in_zip; int err; int files_total; int file_number; int r; char buffer[10000]; if (argc < 3) { fprintf(stderr,"usage: %s <zipfile> <fileindex>\n",argv[0]); return -1; }; zip_file = zip_open(argv[1], 0, &err); if (!zip_file) { fprintf(stderr,"error: can't open file %s\n",argv[1]); return -1; }; file_number = atoi(argv[2]); files_total = zip_get_num_files(zip_file); if (file_number > files_total) { printf("error: have %d files in zip\n",files_total); return -1; }; file_in_zip = zip_fopen_index(zip_file, file_number, 0); if (file_in_zip) { while ( (r = zip_fread(file_in_zip, buffer, sizeof(buffer))) > 0) { printf("%s",buffer); }; zip_fclose(file_in_zip); } else { fprintf(stderr,"error: can't open file %d in zip\n",file_number); }; zip_close(zip_file); return 0; }; also added few .h files include directory in project , few .c files directory zip.c file. after dependences good, have error:
‘struct zip’ has no member named ‘default_password’ in file zip_fopen_index.c the file zip_fopen_index.c is:
#include <errno.h> #include <stdio.h> #include <stdlib.h> #include "zipint.h" zip_extern struct zip_file * zip_fopen_index(struct zip *za, zip_uint64_t fileno, int flags) { return zip_fopen_index_encrypted(za, fileno, flags, za->default_password); // error here }
first of allow me comments:
your program not compiled , linked eclipse.
compiling done compiler (gcc using option -c):
make building file: ../zip.c invoking: gcc c compiler gcc -o0 -g3 -wall -c -fmessage-length=0 -mmd -mp -mf"zip.d" -mt"zip.d" -o "zip.o" "../zip.c" finished building: ../zip.c linking done linker (via compiler using option -o):
invoking: gcc c linker gcc -o "unzipper" ./zip.o ./main.o: in function `zip': /home/alk/workspace/unzipper/debug/../zip.c:20: undefined reference `zip_open' /home/alk/workspace/unzipper/debug/../zip.c:27: undefined reference `zip_get_num_files' /home/alk/workspace/unzipper/debug/../zip.c:33: undefined reference `zip_fopen_index' /home/alk/workspace/unzipper/debug/../zip.c:35: undefined reference `zip_fread' /home/alk/workspace/unzipper/debug/../zip.c:38: undefined reference `zip_fclose' /home/alk/workspace/unzipper/debug/../zip.c:43: undefined reference `zip_close' collect2: ld returned 1 exit status eclipse provides framework helping in managing sources , references spawing compiler , linker tasks , setting options.
when linker told there undefined references zip_*function during build of program, cause was, missing tell linker (via compiler, via eclipse) zip_* functions found.
those zip_* functions located in library, namely libzip.
so programmer need tell linker (via compiler, via eclipse) link functions against compiler compiled sources.
as result linker able create runnable program compiled sources libraries needed. libraries know eclipse (and therfore linker) default, example 1 containing c standard functions, namely libc.
to things going:
1 remove source files pulled libzip librarie's sources project. sources had been compiled library libzip, use in project.
2 tell linker (via eclipse) use libzip project.
do following steps below:
open project's properties
click 'c/c++ general'
click 'path , symbols', on left select 'libraries' tab, there click 'add' , enter zip
finally click 'ok'
3 try build program:
building target: unzipper invoking: gcc c linker gcc -o "unzipper" ./zip.o -lzip finished building target: unzipper (please note additional option -lzip!)
if developement version of 'libzip' had been installed before, should fine.
ps: unzipper name used eclispe project produce examples.
pss: used eclipse juno sr1
Comments
Post a Comment