Skip to content

关于MSVC构建时出现的编码问题(已解决) #4

@Colasensei

Description

@Colasensei

【Bug】MSVC 编译器出现 C4819 编码警告和 C2001 语法错误

问题描述

在使用 Visual Studio 和 MSVC 编译器构建 C++ 项目时,出现以下编译错误:

  1. 编码警告 (C4819)

    warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
    
  2. 语法错误 (C2001 和 C2146)

    error C2001: 常量中有换行符
    error C2146: 语法错误: 缺少")"(在标识符"SetProperty"的前面)
    error C2146: 语法错误: 缺少";"(在标识符"SetProperty"的前面)
    

影响文件:

  • src/LaminaEditor.cpp
  • include/LaminaEditor.h

开发环境:

  • 操作系统:Windows11
  • 编译器:MSVC适用于 .NET Framework MSBuild 版本 17.14.14+a129329f1
  • CMake 版本:4.2.1

复现步骤

  1. 克隆项目到 Windows 系统
  2. 使用 CMake 配置项目
  3. 使用 Visual Studio/MSVC 编译
  4. 观察到编译输出中的警告和错误

预期行为

项目应正常编译,不出现编码相关的警告和错误。

实际行为

出现 C4819 编码警告和 C2001/C2146 语法错误,编译失败。

相关配置

CMakeLists.txt 相关片段

# 原项目中就出现的Unicode 支持
target_compile_definitions(LaminaIDE PRIVATE UNICODE _UNICODE)

编译命令

# 使用 CMake 生成项目
cmake -B build
cmake --build build --config Release

已尝试的解决方案

  1. ✅ 已添加 UNICODE_UNICODE 编译定义
  2. ❌ 但编码问题依然存在

已经成功的修复方案

方案 :转换文件编码

将源文件转换为 UTF-8 with BOM 编码:

  1. 使用 Visual Studio 的"另存为"功能,选择"Unicode (UTF-8 带签名) - 代码页 65001"
  2. 或使用 Notepad++:"编码" → "转为 UTF-8-BOM 编码"

环境信息

  • 操作系统: Windows 11
  • 编译器版本: MSVC (适用于 .NET Framework MSBuild 版本 17.14.14+a129329f1)
  • CMake 版本: [4.2.1]
  • 项目类型: C++ GUI 应用程序

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions