简体   繁体   中英

Why doesn't my gcc compiler recognize the bzip2 functions, yet allows me to include the library they belong to?

I'm writing a file parser in C using gcc on Windows using MSYS2.

Using pacman I've downloaded the required libraries for using libbzip2 .

For some reason, I can include the bzlib.h file and use the structures it contains, but not its functions.

Here's my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <bzlib.h>

void init_decompress_stream(bz_stream *stream, char *next_in, unsigned int avail_in, char *next_out, unsigned int avail_out);

int main(){
  // Open FILE
  FILE *ptr;
  ptr = fopen("example.bin", "rb");

  // Read Metadata
  int metadata[10];
  fread(metadata, sizeof(int), 10, ptr);

  // Init stream
  bz_stream *stream;
  unsigned int avail_in = metadata[0] * sizeof(char);
  unsigned int avail_out = metadata[1] * sizeof(int);
  char *compressed_data = malloc(avail_in);
  char *data = malloc(avail_out);

  // Read data
  fread(compressed_data, 1, avail_in, ptr);
  init_decompress_stream(stream, compressed_data, avail_in, data, avail_out);

  // Decompress data -- COMPILES FINE UNTIL HERE --
  int bz_result = BZ2_bzDecompressInit(stream, 0, 0);
}

void init_decompress_stream(bz_stream *stream, char *next_in, unsigned int avail_in, char *next_out, unsigned int avail_out){
  stream->next_in = next_in;
  stream->avail_in = avail_in;
  stream->next_out = next_out;
  stream->avail_out = avail_out;
}

This all compiles fine until the last line of main: int bz_result = BZ2_bzDecompressInit(stream, 0, 0);

when I compile using at which point I get the error:

C:/msys64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\{User Name}\AppData\Local\Temp\cct30tb5.o:test.c(.text+0xc4): undefined reference to 'BZ2_bzDecompressInit'

collect2.exe: error: ld returned 1 exit status

While troubleshooting I've used three different compile commands:

gcc test.c This compiles #include <bzlib.h> and bz_stream *stream; just fine until the last line.

gcc test.c -L/usr/lib/libbz2.a this performs same as above.

gcc test.c -libbz2 this one cannot find -libbz2

I'm totally perplexed as to what I'm doing wrong.

In gcc, -L add a directory to the library search path, and -l specifies a library to load (using the search path to find it).

Hence you probably need something like:

gcc test.c -L /usr/lib -l bz2

I'd be surprised if /usr/lib wasn't already on the library path but you may want to specify it just in case. And the toolchain is smart enough to prefix your library names with lib and suffix them with extensions as needed.

You should be aware that gcc test.c -libbz2 is probably equivalent to gcc test.c -l ibbz2 which is going to be looking for ibbz2 , not what you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM