?轉載請注明原文鏈接:https://www.cnblogs.com/mechanicoder/p/16894144.html
1. 問題由來
背景:團隊使用 Visual Studio 進行跨平臺專案開發,
遇到的問題:
- 編碼:Windows平臺下源代碼檔案格式可能是 GBK、IBM866、UTF16LE、UTF-8等等,中文字串、注釋等跨平臺編譯時出現亂碼;
- 轉碼:Visual Studio 根據源代碼內容、系統區域設定(即本地化)自動確定源代碼編碼格式,含中文字符時存在不確定性;代碼中英字串處理時需要格式來回轉換,例如 ANSI->UTF8,無法統一;
- 規范:由于團隊成員個人 Visual Studio 編碼格式配置可能不同,例如 switch case 陳述句中的 case 是否縮進以及縮進量,經常遇到對源代碼反復以不同風格進行格式化的情況;(檔案格式化快捷鍵 Ctrl+K,D;選中內容格式化快捷鍵 Ctrl+K,F)Visual Studio 雖然可以使用團隊統一配置,但修改不方便、修改內容無法高效的同步,
2. 解決方法
2.1. 檔案編碼問題
首先,Visual Studio 代碼編輯器支持多種編碼格式,這從 Visual Studio 帶格式保存檔案選項可以看出來,

圖1. 原始碼高級保存選項
關鍵在于如何讓 Visual Studio 始終以 UTF-8 為默認編碼格式,而非由 IDE 本地化自動判斷,避免手動更改編碼格式,該問題在一個回答中找到了靠譜的解決方法(如何配置可以讓 Visual Studio 默認以 UTF-8 格式編碼且全域有效),即通過一個可移植、可定制的文本編輯器組態檔 .editorconfig 對編輯器進行配置,以下參考回答內容:
Visual Studio supports EditorConfig files (EditorConfig)
Visual Studio (VS2017 and later) searches for a file named '.editorconfig' in the directory containing your source files, or anywhere above this directory in the hierarchy. This file can be used to direct the editor to use utf-8. I use the following:
[*] end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true indent_style = space indent_size = 4![]()
The
[*]line is a mask and means for all files - the scope of the commands can be reduced using e.g.[*.{h,cpp}]There are further possibilities, particularly for C# files. Full details can be found at EditorConfig settings - Visual Studio (Windows) | Microsoft Learn
根據 Visual Studio 官方介紹,Visual Studio 啟動時將會自動從原始碼檔案所在檔案夾開始搜索該檔案,直至找到位于頂層目錄的檔案或沒有找到,因此使用時將其放在代碼倉庫的根目錄即可,

圖2. 代碼目錄結構--圖片來自 Visual Studio 官網
那么,是否可以自動生成一個 .editorconfig 檔案呢?
Visual Studio 支持根據本地設定生成一個 .editorconfig 檔案,操作路徑為:
Tools / Options / Text Editor / C/C++ / Code Stype / General: Generate .editorconfig file from settings.

圖3. 生成 .editorconfig 檔案,截圖來自 Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.2.1
得到 .editorconfig 之后就可以進行配置了,例如上述回答中的含義分別時(以下內容是 .editorconfig 的一部分,組態檔支持以 # 開始的注釋):
[*]
end_of_line = lf # 行尾 UNIX 格式 LF
charset = utf-8 # 檔案編碼字符集為 UTF-8
trim_trailing_whitespace = true # 洗掉檔案末尾空格
insert_final_newline = true # 末尾插入新行
indent_style = space # 以空格代替 tab
indent_size = 4 # 代替 tab 的空格數量
Visual Studio 支持的特性 VS官網鏈接,或 editorconfig官網鏈接,
如何將已有檔案轉碼:實作Python腳本,按原編碼讀入資料并按 UTF-8 格式寫出即可,
注意:組態檔修改后需要重啟 Visual Studio,通過檔案格式化(Ctrl+K,D)判斷配置是否生效,如查看格式化前后的空格數量,
2.2. 編譯問題
自動轉碼后編譯時可能遇到各種例外編譯的錯誤或警告(如4819),這些錯誤是由于 Visual Studio 未按 UTF-8 格式進行編譯導致的,既然文本編輯器支持以 UTF-8 編碼的源檔案,那么編譯器也必然支持以 UTF-8 編碼格式決議源檔案,
這里需要指定編譯選項 /utf-8 告訴編譯器以 UTF-8 對源檔案進行解碼,可參考官方檔案,
直接通過 Visual Studio 配置專案時,進行如下設定:

圖4. 指定 /utf-8 編譯選項,截圖來自 Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.2.1
通過 CMake 配置專案時,cmake 腳本指令:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
指定 /utf-8 編譯選項后,當編譯檔案或由源檔案所包含的頭檔案非 UTF-8 編碼時,將會出現大量如下警告資訊,他們可以通過 Visual Studio 禁用警告編號選項關閉,
warning C4828: The file contains a character starting at offset 0x453 that is illegal in the current source character set (codepage 65001).
或通過 cmake 腳本指令:
add_compile_options(/wd4828)
2.3. 其他方法[不建議]
關于編譯警告4819的問題,有網友回答(原回答鏈接)可通過修改系統本地化設定解決,經測驗確實可修復,設定路徑如下:
控制面板 / 時鐘 / 區域 / 區域 / 管理 / 更改系統區域設定,使用Unicode UTF8提供全球語言支持

圖5. 更改系統區域設定
這種解決方法存在副作用,本地化是系統全域設定,可能將影響其他應用程式,當然微軟自家的應用程式相信都已經做了很好的適配,作者電腦上一款軟體中文版就在執行上述設定后出現了亂碼,英文版正常,
此外,2.1 中所述的指定編譯選項 /utf-8 同樣可以解決該警告問題,因此不建議更改系統區域設定,
參考資料
1. How to config visual studio to use UTF-8 as the default encoding for all projects? - Stack Overflow
2. EditorConfig
3. EditorConfig settings - Visual Studio (Windows) | Microsoft Learn
4. /utf-8 (Set source and execution character sets to UTF-8) | Microsoft Learn
5. [Solved]-warning c4819 in Visual Studio C++ 2013 express - utf8 files without bom-C++
6. cmake 添加編譯選項的幾種方式 - 簡書
7. VS CMake 禁止警告 - 心靈捕手 - 博客園
?轉載請注明原文鏈接:https://www.cnblogs.com/mechanicoder/p/16894144.html
?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534128.html
標籤:其他
上一篇:類加載機制
