Search code examples
c++macoscompiler-errorsziplibzip

Compiling libzip on Mac: Undefined symbols for architecture x86_64


I've been learning C++ and have decided to try to create a simple file reader using libzip on archive files (e.g. Word).

I’ve recently installed libzip on my Macbook using brew but I seem to keep on getting the following issue whenever I try to compile a program that uses libzip:

Undefined symbols for architecture x86_64:
  "_zip_fopen", referenced from:
      _main in main-918bfa.o
  "_zip_open", referenced from:
      _main in main-918bfa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [a.exe] Error 1

The command I use to compile:

g++ -g main.cpp -std=c++11 -I/usr/local/Cellar/libzip/0.11.2/include -I/usr/local/Cellar/libzip/0.11.2/lib/libzip/include -L/usr/local/Cellar/libzip/0.11.2/lib -o ../a.exe

main.cpp:

 #include <iostream>
 #include <fstream>
 #include <zip.h>
 #include <zlib.h>

 using namespace std;
 int numArgs = 2;

 int main(int argc, char** argv){
     // Parse command line arguments
     if(argc != numArgs){
         std::cout << "Incorrect number of arguments provided.\n";
         std::cout << "Command line syntax: fileReader.exe inputFile" << endl;
         exit(0);
     }

     // Try out libzip functionality
     std::string inputDocument(argv[1]);
     int err = 0;
     zip* z = zip_open(inputDocument.c_str(), 0, &err);
     if(z == NULL) {
         printf("Could not read docx file. Error code: %d", err);
         exit(-1);
     }
     zip_file* contentTypes = zip_fopen(z, "[Content_Types].xml", ZIP_FL_UNCHANGED);
     exit(0);
 }

Solution

  • Doesn't look like your including the libzip library in the compilation command. Try adding -lzip to your g++ command