cmacosgccbzip2

Using bzlib in C on macOS Catalina - "ld: symbol(s) not found for architecture x86_64", "clang: error: linker command failed with exit code 1"


I am attempting to compile a C program on macOS Catalina. The program will make use of bzip2 decompression. My code includes the line

#include <bzlib.h>

and I am trying to call the function BZ2_bzBuffToBuffDecompress. However, when I run gcc myfile.c -o myfile.c.o, I get the following error:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

I am just using a plain text editor and gcc, no IDEs and no CMake files. I suspect I may need a CMake file for this but I am not really sure how to proceed. Any assistance with this is greatly appreciated!


Solution

  • You need to link in the bzip library. gcc myfile.c -o myfile -lbz2. That command assumes the lib is installed into the standard location. Also, you are compiling a final executable so (by strong convention) it should not have a .o suffix.