我無法在MingW上靜態化FreeImage 3.15.4(使用.lib或.a)。我總是收到錯誤「未定義引用」的所有FreeImage方法。雖然我成功動態鏈接圖書館。未能在MingW上靜態鏈接FreeImage 3.15.4(已解決)
然後我嘗試從源代碼構建,它是一樣的。
我也嘗試使用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文件應該從源代碼中編譯爲「FREEIMAGE_LIBRARY_TYPE = STATIC」。不要忘了編輯「makefile」,因爲操作系統已經被硬編碼爲「gnu」。
請顯示您的鏈接步驟和確切的錯誤。還要確保你沒有不匹配的32位和64位庫。 – Thomas
源和編譯結果已添加。我使用32位。如上所述,使用動態鏈接是成功和程序正常工作(並且沒有來自3.15.3的錯誤)。 –
你甚至在哪裏找到* .a庫文件?我無法在預建的軟件包中找到它。請注意,* .lib文件很可能是MSVC導入庫,它們的大小太薄了。 – Thomas