1. 程式人生 > >FreeImage 3.17.0 在VS2015下編譯及遇到問題解決

FreeImage 3.17.0 在VS2015下編譯及遇到問題解決

1、在FreeImage 3170版本中,裡面沒有帶VS2015的工程檔案。但是有VS2005/2008/2013的。雙擊原始碼目錄下面的FreeImage.2013.sln檔案,然後VS2015就會彈出升級編譯器和庫選項。


2、選擇要編譯的版本為win32還是X64,然後編輯庫檔案。編譯完成後會出現“fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration”的錯誤。

這時候需要修改原始碼中的tif_config.h檔案。該檔案在原始碼目錄中只有一個,因此找到之後直接修改即可。將原始碼中的

#ifdef _WIN32

#define snprintf _snprintf // 將此行註釋掉

#define lfind _lfind

#endif // _WIN32

修改為

#ifdef _WIN32//#define snprintf _snprintf#define lfind _lfind#endif // _WIN32

這是因為在早期的VC自帶的C標準庫中確實沒有snprintf這個函式,只有_snprintf。但是VS2015下是有的,所以這裡不註釋掉會有重定義的問題。

也可以修改為:

#ifdef _WIN32

#if defined(_MSC_VER) && _MSC_VER < 1900

#define snprintf _snprintf

#endif

#define lfind _lfind

#endif // _WIN32

3、再次進行編譯,編譯完成後可以到原始碼目錄下的Dist目錄拷貝FreeImage.h和庫檔案。

錯誤原因分析:

目前,在很多的庫和程式中使用函式通過定義函式_snprintf()的方式來使用snprintf(),因為系統中已經定義好了_snprintf()函式。

#define snprintf _snprintf

最終,Visual Studio 14 中開始定義了snprintf()!

既然在編譯器中開始支援snprintf()函式,因此,我們無需進行重定義(畫蛇添足)。定義這個函式就會造成overshadow已經在

stdio.h中定義的snprintf()。為了約束使用者的這種方式,系統在stdio.h檔案中添加了如下的提示:

#ifdef snprintf

    #error: Macro definition of snprintf conflicts with Standard Library function declaration”

#endif

因此,就會造成上面不編譯不成功的情況。

It is true that on all previous versions of Visual Studio, you must use _snprintf() function. But VS 2014 onwards you should not #define it with _snprintf().

在早期的Visual Studio中你必須定義函式_snprintf(),但在2014及其後續版本中,你無需這麼做。

Somewhere in your code or most likely in  headers, this is done and hence the error.

Check that and remove that #define.

snprintf() is part of C99 specifications.

To enable C99 support

add this in your program

#if _MSC_VER>=1900#  define STDC99#endif

In case you don't know what _MSC_VER macro values are

...
MSVC++14.0 _MSC_VER ==1900(VisualStudio2015)
MSVC++12.0 _MSC_VER ==1800(VisualStudio2013)
MSVC++11.0 _MSC_VER ==1700(VisualStudio2012)
MSVC++10.0 _MSC_VER ==1600(VisualStudio2010)
MSVC++9.0  _MSC_VER ==1500(VisualStudio2008)
MSVC++8.0  _MSC_VER ==1400(VisualStudio2005)
MSVC++7.1  _MSC_VER ==1310(VisualStudio.NET 2003)
MSVC++7.0  _MSC_VER ==1300
MSVC++6.0  _MSC_VER ==1200
MSVC++5.0  _MSC_VER ==1100
MSVC++4.0  _MSC_VER ==1000
MSVC++2.0  _MSC_VER ==900
MSVC++1.0  _MSC_VER ==800
C/C++7.0  _MSC_VER ==700
C      6.0  _MSC_VER ==600