c++cstatic-librariesstatic-linkingfreeimage

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.


Solution

  • Following are the steps to compile C/C++ application statically with FreeImage library on Ubuntu:

    1. Use this link to download the source distribution of FreeImage.
    2. Download and uncompress to get the FreeImage source directory.
    3. Inside this directory open README.linux file and read it. It has very useful information. I would recommend not to miss it.
    4. README.linux file also guides on how to compile (both static and dynamic) and install this library for C or C++. The following is about C++:

    Build FreeImagePlus

    FreeImagePlus is a C++ wrapper for FreeImage. To build FreeImage as a C++ library enter the FreeImage directory using command prompt and build the distribution using the following command:

    make -f Makefile.fip
    

    Compile Test App Statically

    Once make is complete, the desired files are available inside the Dist directory.

    - libfreeimageplus.a: this is the static library ready to use.

    - FreeImagePlus.h: this is the header file available for include.

    Just copy these two files into the directory where the example C++ file (let's say freeimagetest.cpp) is already placed.

    Use the following command to compile C/C++ application statically with FreeImage library on Ubuntu:

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

    If you want to know more about creating and using static libraries, an example can be followed here

    These steps solved my problem.

    Please feel free to improve this answer.