0

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!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Math Rules
  • 21
  • 7
  • 2
    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. – kaylum Aug 02 '21 at 22:07
  • Doesn't the error message list the symbol or symbols that are not found? – Jonathan Leffler Aug 02 '21 at 22:36
  • @kaylum - I tried compilation again adding -lbz2, -l bz2, and -L /usr/bin -l bz2 to my gcc command. Now I get a bunch of lines saying "warning: null character ignored [-Wnull-character]", then "fatal error: too many errors emitted, stopping now [-ferror-limit=]." Why is causing that? My code compile fines without the bzip2 decompression part, so I don't think it is an encoding error on my end. – Math Rules Aug 02 '21 at 23:27
  • Can't tell without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Anyway. that's a different question and you should post a new one. – kaylum Aug 02 '21 at 23:30

1 Answers1

0

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.

kaylum
  • 13,833
  • 2
  • 22
  • 31
  • Looks like I accidentally overwrote my C file with my run.sh script, causing the null warning. For some reason I also have to use sudo to get the linker to work but it is all working now. Thank you for your help, I have accepted the answer! – Math Rules Aug 03 '21 at 00:18
  • I would upvote also but don't have enough reputation yet – Math Rules Aug 03 '21 at 00:19