Visual Studio Compilation Character Set [Converted]

C++ cross-platform development. Commonly encountered on Chinese operating systems: error C2001 - constant contains a newline character.

Visual Studio

cmake organizes the project compilation script, generating a temporary solution under the windows system for development. The reason for cross-platform compatibility is that the file encoding is chosen as utf-8.

The cited reference provides a detailed explanation of the causes of the problem from first principles.

Regarding encoding, MSVC has dedicated compilation options /source-charset and /execution-charset. The former indicates the encoding of the file itself, and the latter indicates what encoding the byte array inside the compiled character set is. Encoding issues can basically be solved using these two options.

For example, the windows cmd console defaults to displaying gbk encoding. However, the code file itself is utf-8. Because of cross-platform considerations and it’s inconvenient to directly convert it to gbk, we don’t include the method of writing encoding conversion code for different platforms. On Win10, we can set these compilation options to /source-charset:utf-8 /execution-charset:gbk, indicating that the compiler reads in with utf-8 encoding and then converts it to gbk to store in the array, so printf can display Chinese characters normally in the cmd console.

CMake Configuration for Visual Studio

if(WIN32)
    message(STATUS "Configuring trade on WIN32")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /source-charset:utf-8 /execution-charset:gbk")
endif()

References

A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy