简体   繁体   English

无法在MingW上静态链接FreeImage 3.15.4

[英]Fail to static linking FreeImage 3.15.4 on MingW

I can't static lingking FreeImage 3.15.4 on MingW (either using .lib nor .a). 我不能在MiningW上静态徘徊FreeImage 3.15.4(使用.lib或.a)。 I always receive error "undefined reference to" all FreeImage method. 我总是收到所有FreeImage方法“未定义的引用”错误。 While I successfully dynamic linking the library. 虽然我成功地动态链接了库。

Then I try build from the source, it's the same. 然后,我尝试从源代码进行构建,这是相同的。

I also try using 3.15.3, both static and dynamic is success. 我也尝试使用3.15.3,静态和动态都是成功的。 But there's a bug in those version (opening some JPEG). 但是这些版本存在一个错误(打开一些JPEG)。

Need help with this. 需要帮助。

My code only 1 file, purge.cpp 我的代码只有1个文件,purge.cpp

#define FREEIMAGE_LIB
#include "FreeImage.h"
#include <iostream>
#include <fstream> 
#include <vector>
#include <string>

void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
  std::cerr << "FIError: " << message << std::endl;
}

int main(int argc, char** argv) {
#ifdef FREEIMAGE_LIB
  FreeImage_Initialise();
#endif
  FreeImage_SetOutputMessage(FreeImageErrorHandler);

  std::string fnameIn (argv[1]);
  std::string fnameOut (argv[2]);
  std::vector<uint8_t> data;

  std::ifstream ifs;
  ifs.open(fnameIn.c_str(), std::ios::binary);
  uint8_t c = ifs.get();
  while (ifs.good()) {
    data.push_back(c);
    c = ifs.get();
  }
  ifs.close();

  FIMEMORY *hmem = FreeImage_OpenMemory(&data[0], data.size());
  FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
  FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, 0);

  int flag = JPEG_BASELINE | JPEG_QUALITYGOOD | JPEG_SUBSAMPLING_420 | JPEG_PROGRESSIVE | JPEG_OPTIMIZE;
  bool b = FreeImage_Save(FIF_JPEG, dib, fnameOut.c_str(), flag);
  std::cout << ((b)?"Save\n":"NoSave\n");

  FreeImage_Unload(dib);
  FreeImage_CloseMemory(hmem);

#ifdef FREEIMAGE_LIB
  FreeImage_DeInitialise();
#endif

  return 0; 
}

Command: 命令:

g++ -o purge purge.cpp -L. -lfreeimage

Result: 结果:

C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x286): undefined reference to `FreeImage_Initialise'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x292): undefined reference to `FreeImage_SetOutputMessage'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x486): undefined reference to `FreeImage_OpenMemory'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x49c): undefined reference to `FreeImage_GetFileTypeFromMemory'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x502): undefined reference to `FreeImage_LoadFromMemory'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x533): undefined reference to `FreeImage_GetWidth'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x541): undefined reference to `FreeImage_GetHeight'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x701): undefined reference to `FreeImage_Rescale'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x723): undefined reference to `FreeImage_Unload'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x72e): undefined reference to `FreeImage_CloseMemory'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x7a4): undefined reference to `FreeImage_Save'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x7d9): undefined reference to `FreeImage_Unload'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x7ea): undefined reference to `FreeImage_CloseMemory'
C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o:purge.cpp:(.text+0x7ef): undefined reference to `FreeImage_DeInitialise'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\ADIT~1.BIS\AppData\Local\Temp\cc1LYwkh.o: bad reloc address 0xf in section `.text$_ZNSt6vectorIhSaIhEEC1Ev[__ZNSt6vectorIhSaIhEEC1Ev]'
collect2.exe: error: ld returned 1 exit status

Then when I remove "#define FREEIMAGE_LIB", it's success. 然后,当我删除“ #define FREEIMAGE_LIB”时,它成功了。 But with dynamic linking :( 但是使用动态链接:(


Solved. 解决了。

After reading README.minGW carefully. 仔细阅读README.minGW之后。 The mistake I made is: I create *.a with 'pexports' & 'dlltool'. 我犯的错误是:我用'pexports'和'dlltool'创建* .a。 While those *.a file is for dynamic link. 而那些* .a文件用于动态链接。 The *.a file for static link should compile from the source with setting "FREEIMAGE_LIBRARY_TYPE=STATIC". 静态链接的* .a文件应使用设置为“ FREEIMAGE_LIBRARY_TYPE = STATIC”的源代码进行编译。 Don't forget to edit "makefile", since the OS has been hard-coded to "gnu". 不要忘记编辑“ makefile”,因为操作系统已被硬编码为“ gnu”。

Try to define FREEIMAGE_LIB in your application too (before including of the header). 也尝试在您的应用程序中定义FREEIMAGE_LIB (在包含标题之前)。 Without that define your app will always spawn "undefined reference", because FreeImage.h still think that it's dynamic library and adds via macroses a DLL-Export annoations, so, prototypes in the header and in the built library are different. 没有该定义,您的应用程序将始终生成“未定义的引用”,因为FreeImage.h仍然认为它是动态库,并通过宏添加了DLL导出注释,因此,标头和内置库中的原型是不同的。

If you still have troubles, you would also try to apply some changes into library itself: 如果仍然有麻烦,还可以尝试对库本身进行一些更改:

Steps to do: 步骤:

1. Modify FreeImage.h: 1.修改FreeImage.h:

Replace this: 替换为:

#ifdef __cplusplus
extern "C" {
#endif

with: 有:

#if defined(__cplusplus) && !defined(__MINGW32__)
extern "C" {
#endif

then this: 然后这个:

#ifdef __cplusplus
}
#endif

with: 有:

#if defined(__cplusplus) && !defined(__MINGW32__)
}
#endif

2. In those files: 2.在这些文件中:

Source/DeprecationManager/DeprecationMgr.cpp
Source/FreeImage/FreeImage.cpp
Source/FreeImage/Plugin.cpp

replace all #ifdef _WIN32 with #if defined(_WIN32) && !defined(__MINGW32__) (or #if defined(_WIN32) && !defined(FREEIMAGE_LIB) instead to don't break dynamic build). 将所有#ifdef _WIN32替换为#if defined(_WIN32) && !defined(__MINGW32__) (或#if defined(_WIN32) && !defined(FREEIMAGE_LIB) #if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && !defined(FREEIMAGE_LIB)而不破坏动态构建)。

Exception: don't replace #ifdef _WIN32 in the "Plugin.cpp" in the *U functions: FreeImage_GetFIFFromFilenameU , FreeImage_LoadU and FreeImage_SaveU . 例外:不要在*U函数的“ Plugin.cpp”中替换#ifdef _WIN32FreeImage_GetFIFFromFilenameUFreeImage_LoadUFreeImage_SaveU

3. Then try to completely rebuild library. 3.然后尝试完全重建库。 It should be built fine 它应该建造良好

4. In your application you should define FREEIMAGE_LIB too (I told below why). 4.在您的应用程序中,您也应该定义FREEIMAGE_LIB (我在下面告诉了为什么)。


If you want, you can take my working code which you would use: 如果需要,可以使用我将要使用的工作代码:

https://github.com/WohlSoft/libFreeImage https://github.com/WohlSoft/libFreeImage

I made it buildable via QMake for convenience, and I also made ability to build a lite version (which supports BMP, ICO, PNG and GIF formats only and which has small weigth). 为了方便起见,我使它可以通过QMake进行构建,并且还具有构建lite版本的能力(仅支持BMP,ICO,PNG和GIF格式,并且尺寸较小)。 It was been included into MinGW-buildable project and it is successfully linking statically. 它已包含在MinGW可构建项目中,并且已成功静态链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM