visual-c++ - 是否可以强制 CMake/MSVC 对没有 BOM 的源文件使用 UTF-8 编码? C4819

标签 visual-c++ utf-8 cmake character-encoding

我们所有的源代码都是有效的 UTF-8,但是 Windows 上的一些用户无法构建它们,因为他们的系统配置为不同的编码。

不加BOM对于源文件,是否可以告诉 MSVC 将所有源视为 UTF-8,而不管用户系统编码如何?

See MSDN's link regarding this topic (需要添加 BOM 表头)。

最佳答案

你可以试试:

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

By default, Visual Studio detects a byte-order mark to determine if the source file is in an encoded Unicode format, for example, UTF-16 or UTF-8. If no byte-order mark is found, it assumes the source file is encoded using the current user code page, unless you have specified a code page by using /utf-8 or the /source-charset option.



引用文献
  • Docs - Visual C++ - ‎Documentation - IDE and Tools - Building - Build Reference: /utf-8 (Set Source and Executable character sets to UTF-8)
  • 关于visual-c++ - 是否可以强制 CMake/MSVC 对没有 BOM 的源文件使用 UTF-8 编码? C4819,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47690822/

    相关文章:

    c++ - MSVC(似乎不完美)对 constexpr 的支持

    c++ - 无效重载与 glutDisplayFunc 不兼容

    php - UTF8 中的制表符字符

    c++ - 有没有一种简单的方法可以在 Visual Studio 中编写 UTF-8 八位字节?

    php - html php 元字符集 UTF-8 不工作?

    build - SCons 和/或 CMake : any way to automatically map from "header included during compilation" to "corresponding object file must be linked"?

    visual-studio-2010 - bison.exe 找不到 VS2010 中的 MSBuild 所需的文件 m4sugar.m4

    c++ - 从 DLL 导出类

    c++ - CMake target_include_directories 和 target_compile_definitions 替代方案

    c++ - 在cmake中在find_package中传递多个值