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
"), _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.
"), _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.
"), _T("error"), _T("setlocale"), LOCALE_STR, _T("failure"));
        iret = -1;
        return iret;
    }

    sayHello();

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

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

原文地址:https://www.cnblogs.com/lsgxeva/p/12119867.html