当前位置:网站首页>VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
2022-07-02 09:32:00 【System_sleep】
前言
在VS2019中,如果代码中包含中文字符串定义或注释,会导致各种莫名其妙的编译错误,就算是运气好,编译没有报错,也会遇到控制台打印中文乱码的问题,让人倍感烦躁。
编译问题
1. 代码中定义了中文字符串常量导致编译错误:“常量中有换行符“ ;
2. 代码中包含中文注释导致编译错误:找不到XXX (宏、变量)、XXX不是常量/变量及由于找不到声明导致的其他问题;
打印乱码问题
代码打印中文字符串,在命令提示符输出是乱码;
解决方法,三种方法任选其一:
一. 修改代码文件格式为utf-8带签名,让MSVC编译器能够自动识别并以utf-8 BOM格式加载文件:
- 使用外部编辑器将代码文件保存为utf8-BOM格式;
- 或 打开代码文件,在VS2019 “高级保存选项” 中将代码文件保存格式选择为“Unicode-带签名“;
二. 修改代码格式:
修改中文字符串、中文注释的格式,在中文结尾处添加英文空格,让MSVC编译器能够以GBK编码方式加载编译中文utf-8编码字符串,可以解决编译报错问题,打印还是乱码;
三. 强制指定MSVC编译器加载代码文件的编码格式:
- 在VS2019“项目”-“工程属性”对话框-“C/C++”-“所有选项"子页,在“附加选项”内添加 “/utf-8” (等同于”/source-charset:utf-8 /execution-charset:utf-8");
- 在VS2019“项目”-“工程属性”对话框-“C/C++”-"所有选项"子页,在“附加选项”内添加 “/source-charset:utf-8 /execution-charset:gbk” (前者表示文件本身的编码,后者表示编译以后的字符数组内的字节是什么编码);
总结:
1. 如果是新建工程,推荐方法一,让所有新建代码文件都统一为utf-8 BOM 格式;
2. 如果是跨平台代码,且文件包含少量中文注释,可以尝试方法二;
3. 如果是已有的历史复杂工程(非使用工具导出的解决方案),可以尝试方法三, 但需要注意设置execution-charset时,要同时考虑到UI显示和打印二者对字符串编码的要求(可能不一致);
参考信息:
边栏推荐
- MTK full dump抓取
- What are the software product management systems? Inventory of 12 best product management tools
- 一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
- 洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
- OpenMLDB Meetup No.4 会议纪要
- Filtering of PCL
- Array splitting (regular thinking
- [quick application] win7 system cannot run and debug projects using Huawei ide
- 二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
- [play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
猜你喜欢

QT learning diary 8 - resource file addition

Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
![[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?](/img/66/674a06d8e45a31ae879b81554ef373.png)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?

V2x SIM dataset (Shanghai Jiaotong University & New York University)
![[applinking practical case] share in app pictures through applinking](/img/12/5616f1fa55387b81e25e55a98022b9.png)
[applinking practical case] share in app pictures through applinking

ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘

I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)

tidb-dm报警DM_sync_process_exists_with_error排查

TIPC Cluster5
![[AI application] Hikvision ivms-4200 software installation](/img/4e/1640e3cafac13ce4d39520195c3217.png)
[AI application] Hikvision ivms-4200 software installation
随机推荐
TIPC protocol
Verilog 和VHDL有符号数和无符号数相关运算
TIPC messaging3
Appgallery connect scenario development practice - image storage and sharing
ros gazebo相关包的安装
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
QT learning diary 7 - qmainwindow
flink二開,實現了個 batch lookup join(附源碼)
函数式接口和方法引用
spritejs
K-d tree and octree of PCL
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Approximate sum count (approximate
Iii. Système de démarrage et d'horloge à puce
PCL extracts a subset from a point cloud
Is the account above changtou school safe?
Use Huawei performance management service to configure the sampling rate on demand
Jenkins安装
Internship report skywalking distributed link tracking?
Is bond fund safe? Does the bond buying foundation lose principal?

