Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile C/C++ application statically with FreeImage library in Linux (Ubuntu)?

I am following this example. However, I want to compile a static binary for the given toy example code. Normally, I use -static during compilation but here it gives an error message.

The compile command which works fine:

g++ freeimagetest.cpp -o freeimagetest -lfreeimageplus

The compile command which does not works fine:

g++ freeimagetest.cpp -o freeimagetest -lfreeimageplus -static

The last few lines of the error message:

 In function `ZIPPreDecode':
(.text+0x6f8): undefined reference to `inflateReset'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libfreeimageplus.a(tif_zip.o): In function `ZIPSetupDecode':
(.text+0x783): undefined reference to `inflateInit_'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libfreeimageplus.a(tif_zip.o): In function `ZIPSetupDecode':
(.text+0x7b4): undefined reference to `deflateEnd'
collect2: ld returned 1 exit status

So how could this be done/fixed?

EDIT: I could not see how the attached link solves my problem. Apparently, it looks that, due to the error messages, however, there is some problem in the way I am trying to compile it statically. I could not find the right way to do so. I think the error message is missleading - It just ends with these lines (where these lines are less than one percent of the all message). Anyone who has done it can better answer. If you think your answer is more than just an educated guess, i would request you to please give it a try before answering. It will only take a couple minutes if you follow the attached link. Moreover, I have tagged C also because it is same for C language programs as well.

like image 846
tod Avatar asked Dec 15 '16 11:12

tod


1 Answers

Usually when you statically link a library (instead of linking it dynamically), you need to also link all of its dependencies manually. You need to figure out what library (dependency) it lacks and link it too.

Quick googling shows that inflateReset is from a library called zlib. Thus you need to link it using -lz. There is a good chance you already have this library in your compiler search directories, but if you don't, then you need to compile it manually.

like image 72
HolyBlackCat Avatar answered Sep 21 '22 15:09

HolyBlackCat