无法在MingW上静态链接FreeImage 3.15.4 [英] Fail to static linking FreeImage 3.15.4 on MingW

查看:181
本文介绍了无法在MingW上静态链接FreeImage 3.15.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能在MingW上静态滞留FreeImage 3.15.4(使用.lib或.a).我总是收到所有FreeImage方法未定义的引用"错误.当我成功地动态链接库时.

I can't static lingking FreeImage 3.15.4 on MingW (either using .lib nor .a). I always receive error "undefined reference to" all FreeImage method. While I successfully dynamic linking the library.

然后我尝试从源代码进行构建,这是相同的.

Then I try build from the source, it's the same.

我也尝试使用3.15.3,无论是静态还是动态都是成功的.但是这些版本存在一个错误(打开一些JPEG).

I also try using 3.15.3, both static and dynamic is success. But there's a bug in those version (opening some JPEG).

需要帮助.

我的代码只有1个文件purge.cpp

My code only 1 file, 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; 
}

命令:

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

结果:

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

然后,当我删除"#define FREEIMAGE_LIB"时,它成功了.但是通过动态链接:(

Then when I remove "#define FREEIMAGE_LIB", it's success. But with dynamic linking :(

已解决.

仔细阅读README.minGW之后.我犯的错误是:我用'pexports'&创建* .a. 'dlltool'.而那些* .a文件用于动态链接.静态链接的* .a文件应使用设置为"FREEIMAGE_LIBRARY_TYPE = STATIC"的源代码进行编译.不要忘记编辑"makefile",因为操作系统已被硬编码为"gnu".

After reading README.minGW carefully. The mistake I made is: I create *.a with 'pexports' & 'dlltool'. While those *.a file is for dynamic link. The *.a file for static link should compile from the source with setting "FREEIMAGE_LIBRARY_TYPE=STATIC". Don't forget to edit "makefile", since the OS has been hard-coded to "gnu".

推荐答案

也尝试在您的应用程序中定义FREEIMAGE_LIB(在包含标头之前).没有该定义,您的应用程序将始终生成未定义的引用",因为FreeImage.h仍然认为它是动态库,并且通过宏添加了DLL导出注释,因此,标头和内置库中的原型是不同的.

Try to define FREEIMAGE_LIB in your application too (before including of the header). 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.

如果仍然有麻烦,您还可以尝试对库本身进行一些更改:

If you still have troubles, you would also try to apply some changes into library itself:

要做的步骤:

1.修改FreeImage.h:

替换此:

#ifdef __cplusplus
extern "C" {
#endif

具有:

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

然后这样:

#ifdef __cplusplus
}
#endif

具有:

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

2.在这些文件中:

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

将所有#ifdef _WIN32替换为#if defined(_WIN32) && !defined(__MINGW32__)(或#if defined(_WIN32) && !defined(FREEIMAGE_LIB)而不破坏动态构建).

replace all #ifdef _WIN32 with #if defined(_WIN32) && !defined(__MINGW32__) (or #if defined(_WIN32) && !defined(FREEIMAGE_LIB) instead to don't break dynamic build).

例外:不要在*U函数:FreeImage_GetFIFFromFilenameUFreeImage_LoadUFreeImage_SaveU中的"Plugin.cpp"中替换#ifdef _WIN32.

Exception: don't replace #ifdef _WIN32 in the "Plugin.cpp" in the *U functions: FreeImage_GetFIFFromFilenameU, FreeImage_LoadU and FreeImage_SaveU.

3.然后尝试完全重建库.它应该构建良好

4.在您的应用程序中,您也应该定义FREEIMAGE_LIB(我在下面告诉了为什么).

4. In your application you should define FREEIMAGE_LIB too (I told below why).

如果需要,您可以获取将要使用的我的工作代码:

https://github.com/WohlSoft/libFreeImage

为方便起见,我使其可以通过QMake进行构建,并且还具有构建精简版(仅支持BMP,ICO,PNG和GIF格式且尺寸较小)的功能.它已包含在MinGW可构建的项目中,并且已成功地静态链接.

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). It was been included into MinGW-buildable project and it is successfully linking statically.

这篇关于无法在MingW上静态链接FreeImage 3.15.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆