2014-01-06 31 views
0

我無法在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」。

+1

請顯示您的鏈接步驟和確切的錯誤。還要確保你沒有不匹配的32位和64位庫。 – Thomas

+0

源和編譯結果已添加。我使用32位。如上所述,使用動態鏈接是成功和程序正常工作(並且沒有來自3.15.3的錯誤)。 –

+0

你甚至在哪裏找到* .a庫文件?我無法在預建的軟件包中找到它。請注意,* .lib文件很可能是MSVC導入庫,它們的大小太薄了。 – Thomas

回答

1

試試在你的應用程序中定義FREEIMAGE_LIB(在包含頭部之前)如果沒有這個定義,你的應用程序總會產生「未定義的引用」,因爲FreeImage.h仍然認爲它是動態庫並通過宏添加DLL-導出annoations,原標題中和構建的庫中的類型不同。

如果您仍然有麻煩,你也嘗試了一些更改應用到庫本身:

步驟做:

1.修改FreeImage.h:

更換此:

#ifdef __cplusplus 
extern "C" { 
#endif 

機智H:

#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)而不是不破動態生成)。

例外:沒有在*U函數代替#ifdef _WIN32在 「Plugin.cpp」:FreeImage_GetFIFFromFilenameUFreeImage_LoadUFreeImage_SaveU

3.然後嘗試完全重建庫。它應該內置好

4.在你的應用程序中,你也應該定義FREEIMAGE_LIB(我在下面告訴了爲什麼)。


如果你願意,你可以把我的工作代碼,你可以使用:

https://github.com/WohlSoft/libFreeImage

我把它的可建通過QMAKE爲了方便,而我也做出了建設能力一個精簡版(它只支持BMP,ICO,PNG和GIF格式,並且體積小)。它已被納入MinGW可建設項目,併成功鏈接靜態。