Search code examples
clinuxlibjpeg

Undefined reference to jpeg_CreateDecompress() error when using libjpeg


I install libjpeg-dev and all files are in the include folder

/usr/include/jerror.h
/usr/include/jmorecfg.h
/usr/include/jpegint.h
/usr/include/jpeglib.h
/usr/include/turbojpeg.h
/usr/include/x86_64-linux-gnu
/usr/include/x86_64-linux-gnu/jconfig.h

And when I try this simple code to decompress a jpeg image I got the error as in title.

here is the code:

#include <stdlib.h>
#include <stdio.h>
#include <jpeglib.h>
int main(void){
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    return 0;
}

Solution

  • The same problem has buged me for about two days!

    my solution is use:

    gcc your_code.c -ljpeg
    

    instead of:

    gcc -ljpeg your_code.c
    

    to compile your code.

    here is the explanation:Why does the order in which libraries are linked sometimes cause errors in GCC?

    hope this will help.