In visual studio C++ 2013 express it seems that unless utf8-encoded file has BOM mark, compiler fails to understand that the file being compiled is in UTF8 encoding and trea
Update to Visual Studio 2015. It supports new compiler options for source and execution characters sets.
You can use the
/utf-8
option to specify both the source and execution character sets as encoded by using UTF-8. It is equivalent to specifying/source-charset:utf-8 /execution-charset:utf-8
on the command line. Any of these options also enables the/validate-charset
option by default....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. Visual Studio allows you to save your C++ source code by using any of several character encodings....
Ref: https://msdn.microsoft.com/en-us/library/mt708821.aspx
How should the compiler guess which encoding you intend the file to be interpreted with? That said, there are a few options:
#pragma
to tell MSC about the encoding, although I never used this myself.To the date I have not encountered any solution to the problem.
If a fix for this behavior exists, apparently it is well guarded secret.
If your system locale is not English (E.g. Chinese or other language), a simple way to fix this is to change your system setting of 'Region and Language' to be English. Just follow steps below:
Control Panel -> Clock,Language,and Region -> Region and Language ->
Administrative -> Language for non-Unicode programs -> Change system locale.
Is that simple? It fixes my problem as my system locale is Chinese. The description of 'Language for non-Unicode programs' is clear:
This setting (system locale) controls the language used when displaying
text in programs that do not support Unicode.
More details in the image
I met this problem when I tried to build my project on Windows, which is successful on another Windows machine. I was crazy to modify all non-Unicode characters (All of then are comments) and so that compiler could move on. But there are too many files with this problem.