【问题标题】:How to compile C/C++ application statically with FreeImage library in Linux (Ubuntu)?如何在 Linux (Ubuntu) 中使用 FreeImage 库静态编译 C/C++ 应用程序?
【发布时间】:2016-12-15 11:50:43
【问题描述】:

我正在关注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

那么如何做到/解决这个问题?

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

【问题讨论】:

    标签: c++ c static-libraries static-linking freeimage


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      1. 使用this 链接下载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:这是可用于包含的头文件。

      只需将这两个文件复制到已放置示例 C++ 文件(例如 freeimagetest.cpp)的目录中。

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

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

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

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

      请随时改进此答案。

      【讨论】:

        【解决方案3】:

        使用 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

        【讨论】:

        • 我使用的是 Linux/Ubuntu。我问题中的附加示例也适用于 Linux 用户。 FreeImage 网页的 QA 中提供了使用 MSVS 静态编译它的完整步骤列表。此外,还有几个关于 SO 的问题讨论了使用 MSVS 进行静态编译期间的一系列问题。
        【解决方案4】:

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

        源代码分发包括 FreeImage、C++、C#、Delphi 和 VB6 包装器、示例和内部使用的库 LibTIFF、LibJPEG、LibPNG、ZLib、OpenEXR、OpenJPEG、LibRaw、LibJXR 和 LibWebP。

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

        编译错误建议您应该尝试将系统的 ZLib 和可能的 LibTIFF 添加到您链接的库列表中。这样您就可以避免构建自己的 freeimage 库版本并使用系统的版本。如果 ZLib 和 LibTIFF 不是丢失的,您可以尝试在该列表中一一添加其他库的系统版本,看看哪个可以解决丢失链接的问题。

        【讨论】:

        • 可能值得一提的是链接的顺序很重要。
        • @Dmitry Rubanovich,是的,-static 必须位于要在命令行上静态链接的库之前。事实上,您可以在同一命令行上多次在静态链接和动态链接之间切换,并且在每组要静态链接的库之前,您可以使用-static 选项。并且库本身可能需要重新排序(如果存在循环依赖,有时甚至需要列出两次)。因此,如果 libXYZ 依赖于 libABC,则添加 -lXYZ -lABC 会起作用,而 -lABC -lXYZ 可能不会。
        【解决方案5】:

        它依赖于一个 zip 库。可能是 zlib。

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

        https://github.com/MalcolmMcLean/babyxrc

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-27
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-14
          • 2012-05-02
          相关资源
          最近更新 更多