如何使用 Linux (Ubuntu) 中的 FreeImage 库静态编译 C/C++ 应用程序?

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

我正在遵循 this 示例。但是,我想为给定的玩具示例代码编译一个静态二进制文件。通常,我在编译期间使用 -static 但在这里它给出了错误消息。

运行正常的编译命令:

g++ freeimagetest.cpp -o freeimagetest -lfreeimageplus

的编译命令工作正常:

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

错误信息的最后几行:

 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

这怎么可能done/fixed?

编辑: 我看不出附件 link 如何解决我的问题。显然,由于错误消息,我尝试静态编译它的方式存在一些问题。我找不到这样做的正确方法。我认为错误消息具有误导性——它仅以这些行结尾(这些行不到所有消息的百分之一)。做过的人可以更好的回答。如果您认为您的答案不仅仅是有根据的猜测,我会请您在回答之前试一试。如果您按照附件 link 进行操作,只需几分钟。此外,我还标记了 C 因为它对于 C 语言程序也是一样的。

它依赖于 zip 库。可能是 zlib.

这些函数没有依赖关系,如果您想避免依赖地狱,这是更好的方法。

https://github.com/MalcolmMcLean/babyxrc

通常,当您静态 link 库(而不是动态地 link 时),您还需要手动 link 它的所有依赖项。您需要弄清楚它缺少什么库(依赖项)并且link它也是。

快速谷歌搜索显示 inflateReset 来自名为 zlib 的图书馆。因此,您需要使用 -lz link 它。很有可能您的编译器搜索目录中已经有这个库,但如果没有,则需要手动编译它。

使用 Visual C++ 6 资料来源:Use FreeImage as Static Library

如何将 FreeImage 用作静态库而不是 DLL (Visual C++ 6)?

1. Compile the FreeImage library in debug and release modes and close your projects. You won't need to use the FreeImage source code anymore.
Note: Do not compile the FreeImage DLL (project named FreeImage) but the project named FreeImageLib. This should produce a huge file named FreeImage.lib (in release mode) or FreeImaged.lib (in debug mode) in the Dist\ directory.



2. Copy FreeImage.lib/FreeImaged.lib into your lib\ directory or in a directory where the linker can find them (e.g. your project directory). You can use "Menu->Tools->Options->Directories->Library files" for this.



3. Create a new project and add your code in it.
Add a call to FreeImage_Initialise() at the beginning of you main function and a call to FreeImage_DeInitialise() at the end of this function.



4. Edit the compiler options (Menu -> Project -> Settings)

    1. tab C/C++ : Category "Preprocessor"
    * Add FREEIMAGE_LIB to the preprocessor definitions
    2. tab C/C++ : Category "Code Generation"
    * Use the Multithreaded run-time library (in release mode)
    * Use the Debug Multithreaded run-time library (in debug mode)

5. Edit linker options (Menu -> Project -> Settings)

    1. tab Link : Category Input
    * Add FreeImage.lib to the list of object/library modules (release mode)
    * Add FreeImaged.lib to the list of object/library modules (debug mode)
    2. tab Link : Category Input
    * Add LIBCMT to the Ignore library list (it helps to avoid a warning)

6. Compile and link your program.

使用MakeFile.MingW

# Uncomment this variable to make a static library. This may
# also be specified as an environment variable and can hold
# any of STATIC and SHARED and must be in uppercase letters.
# Default: SHARED
#FREEIMAGE_LIBRARY_TYPE = STATIC
 FREEIMAGE_LIBRARY_TYPE = STATIC

你不需要任何其他库参考 ReadMe.Mingw 第 2 节


2。使用 MinGW

构建 FreeImage 库

不需要任何其他第三方库(例如 libjpeg、libpng、libtiff、libmng 和 zlib 等)安装在 您的系统以便编译和使用该库。 FreeImage 使用 这些库的自己的版本。这样,您可以确定 FreeImage 将始终使用经过适当测试的最新版本 的这些第三方库。

  1. Fail to static linking FreeImage 3.15.4 on MingW (SOLVED)
  2. https://github.com/KelSolaar/FreeImage/blob/master/README.minGW

以下是在 Ubuntu 上使用 FreeImage 库静态编译 C/C++ 应用程序的步骤:

  1. 使用this link下载FreeImage的源码分发。
  2. 下载解压得到FreeImage源码目录。
  3. 在此目录中打开 README.linux 文件并阅读。它有非常有用的信息。我建议不要错过它。
  4. README.linux 文件还指导如何为 C 或 C++ 编译(静态和动态)和安装此库。下面是关于C++的:

构建 FreeImagePlus

FreeImagePlus 是 FreeImage 的 C++ 包装器。要将 FreeImage 构建为 C++ 库,请使用命令提示符进入 FreeImage 目录并使用以下命令构建分发:

make -f Makefile.fip

静态编译测试应用程序

一旦 make 完成,所需的文件就可以在 Dist 目录中找到。

- libfreeimageplus.a:这是可以使用的静态库。

- FreeImagePlus.h:这是可用于include的头文件。

只需将这两个文件复制到示例 C++ 文件(假设 freeimagetest.cpp)所在的目录中。

使用以下命令在 Ubuntu 上使用 FreeImage 库静态编译 C/C++ 应用程序:

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

如果您想了解更多有关创建和使用静态库的信息,可以参考示例here

这些步骤解决了我的问题。

请随时改进此答案。

这个问题的 link which you posted in your own answer 顶部有以下内容:

Source distribution includes source for FreeImage, C++, C#, Delphi and VB6 wrappers, examples and the internally used libraries LibTIFF, LibJPEG, LibPNG, ZLib, OpenEXR, OpenJPEG, LibRaw, LibJXR and LibWebP.

您使用的发行版很可能不会将所有这些库编译成静态版本的 freeimage 库。

编译错误表明您应该尝试将系统的 ZLib 和 LibTIFF 添加到您 link 所针对的库列表中。这样您就可以避免构建自己的 freeimage 库版本并使用系统版本。如果 ZLib 和 LibTIFF 证明不是缺失的那些,您可以尝试在该列表中逐个添加其他库的系统版本,看看哪个可以解决缺失的 link 问题。