mozilla / mozjpeg

Improved JPEG encoder.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example compilation works with clang but not with gcc

zak905 opened this issue · comments

I have slightly modified the example in example.txt and added a main function to test how compression works.

When I compile using gcc it does not work:

gcc -o testcompress -I /opt/mozjpeg/include/ -lm compress.c libjpeg.a

/usr/bin/ld: libjpeg.a(jcdctmgr.c.o): in function `quantize_trellis':
jcdctmgr.c:(.text+0x31ab): undefined reference to `pow'
/usr/bin/ld: jcdctmgr.c:(.text+0x31dd): undefined reference to `pow'
/usr/bin/ld: jcdctmgr.c:(.text+0x42f0): undefined reference to `pow'
collect2: error: ld returned 1 exit status

Surprisingly, when I compile using clang, everything works fine. Not sure why.

clang -o testcompress -I /opt/mozjpeg/include/ -lm compress.c libjpeg.a

The -lm here makes the difference. It seems to work on clang but not on gcc for some reason. If I remove -lm from the clang command I get the same message as gcc.

Here the versions infos:

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
clang version 10.0.0-4ubuntu1

I also tried using gcc version 11 but it gives the same result

Upadate: compilation works with Clang but it gives Segmentation fault (core dumped) when running the binary, I found a related issue #202

To summarize: gcc compilation does not work, and clang compilation works but gives Segfault.

I can also post the full example code.

it seems like the order plays a role, the -lm needs to be last. Now I got it to compile using gcc: gcc -o testcompress -I /opt/mozjpeg/include/ compress.c libjpeg.a -lm

Additional note: if you want to use the shared library instead of *.a, the LD_LIBRARY_PATH or -rpath need to be set.
Here is how I got it working:

gcc -Wall -o compress -g -I/opt/mozjpeg/include/ -L/opt/libjpeg-turbo/lib64 compress.c -lturbojpeg -ljpeg -Wl,-rpath=/opt/mozjpeg/lib64/

I thought there is nothing in the example.txt mentionning this, so I hope this helps somebody.