VC warning: C4819


VC warning: C4819

 

------問題--------------------
Qt項目使用 VC++ 編譯器出現此錯誤。

warning: C4819: 該文件包含不能在當前代碼頁(936)中表示的字符。請將該文件保存為 Unicode 格式以防止數據丟失。

 

---------- QT 解決方案 --------------------

在項目的.pro配置文件中,增加以下內容

# Disable warning C4819 for msvc
msvc:QMAKE_CXXFLAGS += -execution-charset:utf-8
msvc:QMAKE_CXXFLAGS += -source-charset:utf-8
QMAKE_CXXFLAGS_WARN_ON += -wd4819

 

------其他解決方案--------------------
忽略警告,但是問題還在
(1)找出警告的文件,然后在該文件的開始處加入下面的語句:
# pragma warning (disable:4819)

(2)如果是 VS IDE,還可以:在Project -> Properties -> Configuration Properties -> C/C++ -> Advance 的 Disable Specific Warnings 中添加相應的警告編號:4819;

去掉這個警告:
(1)轉換Code文件為: UTF-8 帶BOM 格式;

(2)如果是 Qt Creator,設置【項目編輯器】,【文件編碼】為:UTF-8,【UTF-8 BOM】:如果編碼是UTF-8則添加。最后隨便改動一下出現警告的文件保存,就會保存為:UTF-8 帶BOM 格式。

(3)如果是 VS IDE,打開有該warning的文件,點擊【文件】選【高級保存選項】,改變編碼格式為【簡體中文(GB2312)- 代碼頁936】或【Unicode(UTF-8 帶簽名)-代碼頁65001】,保存。

 

 

簡體中文 (GB2312) - 代碼頁 936 : Windows (CR LF) --- GB2312
Unicode - 代碼頁 12000 : Windows (CR LF) --- UCS-2 LE BOM
Unicode (UTF-8 帶簽名) - 代碼頁 65001 : Windows (CR LF) --- UTF-8-BOM
Unicode (UTF-8 無簽名) - 代碼頁 65001 : Windows (CR LF) --- UTF-8

 

// hello.cpp: 定義控制台應用程序的入口點。
//

//#include "targetver.h" // SDKDDKVer.h
#include "stdafx.h" // stdio.h, tchar.h, targetver.h

#if defined(UNICODE) && defined(_UNICODE)
#pragma message("UNICODE and _UNICODE macro activated.")
#else
#error UNICODE and _UNICODE must be defined. This version only supports unicode builds.
#endif // !UNICONDE

#include <cstdio> // fprintf, fwprintf
#include <cstdlib> // system, _wsystem
#include <clocale> // setlocale, _wsetlocale

#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif /* End of defined(__cplusplus) || defined(c_plusplus) */

int sayHello()
{
    int iret = 0;
    _ftprintf(stdout, _T("Say: %s\n"), _T("Hello 您好"));
    return iret;
}

#if defined(__cplusplus) || defined(c_plusplus)
}
#endif /* End of defined(__cplusplus) || defined(c_plusplus) */

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
    int iret = 0;
    TCHAR *oldLocale = nullptr;
    const TCHAR *LOCALE_STR = _T("zh-CN");
    oldLocale = _tsetlocale(LC_ALL, nullptr);
    _ftprintf(stdout, _T("%s: %s %s.\n"), _T("notice"), _T("initial locale is"), oldLocale);

    /*
    * string --- locale
    * NULL --- C
    * "" --- Chinese (Simplified)_China.936
    * "zh-CN" --- zh-CN
    * "chs" --- Chinese_China.936
    */
    if (!_tsetlocale(LC_ALL, LOCALE_STR))
    {
        _ftprintf(stderr, _T("%s: %s %s %s.\n"), _T("error"), _T("setlocale"), LOCALE_STR, _T("failure"));
        iret = -1;
        return iret;
    }

    sayHello();

    _tsystem(_T("Pause"));
    iret = 0;
    return iret;
}

 

 

================= End

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM