我不能在 MingW 上静态 lingking FreeImage 3.15.4(使用 .lib 或 .a)。我总是收到错误“未定义的引用”所有 FreeImage 方法。虽然我成功地动态链接了库。
然后我尝试从源代码构建,它是一样的。
我也尝试使用3.15.3,静态和动态都是成功的。但是这些版本中有一个错误(打开一些 JPEG)。
需要帮助。
我的代码只有 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;
}
命令:
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”时,它就成功了。但是使用动态链接:(
解决了。
仔细阅读 README.minGW 后。我犯的错误是:我用“pexports”和“dlltool”创建*.a。而那些 *.a 文件用于动态链接。静态链接的 *.a 文件应该从源代码编译并设置“FREEIMAGE_LIBRARY_TYPE=STATIC”。不要忘记编辑“makefile”,因为操作系统已被硬编码为“gnu”。