Modify Visual STUDIO EXPRESS 2012 new C + + file encoding

Source: Internet
Author: User

    • This site is reproduced in addition to the article, are the original site or translation.
    • The article welcomes all forms of reprint, but please 18 years of age and above the reprint of the source of the article, respect for my labor, but also respect your IQ;
    • This site part of the original and translated articles to provide markdown format source code, welcome to use the source of the article reproduced;
    • Article title: Modify Visual Studio Express 2012 new C + + file encoding
    • This article link: http://zengrong.net/post/1900.htm


2013-12-03 Update: Add a description of UTF-8 (BOM)

I suddenly found out that the source files created by Visual Studio Express 2012 (hereafter referred to as VS) are cp936 (GB2312) encoded, which can be cumbersome to compile later under Linux and Mac.

Preliminary solution

So, I tried to change the files in the existing project to the UTF-8 format, and set the save format to UTF-8, and there are probably a few steps:

    1. Tools-Options-environment-documentation-Save your document as Unicode when you cannot save data in the code page's encoded format
    2. Tools-Options-text editor-general-Automatically detect UTF-8 encodings without signatures
    3. With the file open, select File-Advanced save options, set the encoding and end of line
    4. Select "Encode save" when saving files

Please forgive me to use the Chinese description interface, because at that time installed vs when the hand of the simple interface, and then no matter how to install the English language pack ...

The above 1, 22 seems useless, the key is the 3rd item.

According to my custom, set the encoding to "Unicode (UTF-8 Unsigned) – code page 65001" and set the end of the line to "Unix (LF)".

Then test save, and then use VIM to open the file to view the encoding, found that the file is actually saved as UTF-8 encoded.

But (but), when I reopen the file, re-check the "Advanced Save Options" and find that the code becomes "Simplified Chinese (GB2312)-code page 936".

Then the Lord of the gods Horse!!!

The second step is to solve

I suspect that the VS does not recognize a UTF-8 encoded file without a signature.

To treat a text file as UTF-8 encoding, you may need several conditions (or one):

    1. The file contains the UTF-8 signature;
    2. The file contains UTF-8 encoded characters (not all ASCII-capable characters);
    3. The editor takes precedence over UTF-8 encoding for parsing.

For example, my setting for Vim is to put UTF-8 detection in a preferred position.

Well, let me verify if the guesses are correct.

In advanced save options, set the encoding to Unicode (UTF-8 signed) – code page 65001, and then save. Re-open, the encoding does not change.

This shows that my guess is correct.

Then, verify that the 2nd item on the above condition is correct.

In advanced save options, set the encoding to Unicode (UTF-8 no signature) – code page 65001, add some Chinese content, and then save. Re-open, the encoding does not change.

Well, now I can conclude that vs can normally recognize UTF-8 encoding, but I need to meet the above conditions 1 or 2.

It is important to note that even in existing projects, all files are already UTF-8 encoded (for example, Cocos2d-x's project template), but when you open and save with VS, the original file encoding becomes CP936. This is because the files in the original template file are UTF-8 (unsigned). As for the workaround, refer to the above.

Encoding for new C + + files

Modify the following template file encoding and line end values, the new CPP or H file in VS, the default is UTF-8 encoding (path itself):

    • C:\Program Files (x86) \microsoft Visual Studio 11.0\vc\vcprojectitems_wdexpress\newc++file.cpp
    • C:\Program Files (x86) \microsoft Visual Studio 11.0\vc\vcprojectitems_wdexpress\hfile.h
    • C:\Program Files (x86) \microsoft Visual Studio 11.0\vc\vcnewitems_wdexpress\newc++file.cpp
    • C:\Program Files (x86) \microsoft Visual Studio 11.0\vc\vcnewitems_wdexpress\hfile.h

These files are 0-byte empty files by default, so it is recommended to include some comments and set the file encoding to UTF-8 (with a signature) and the end of the line to UNIX format.

If you want to modify more things, such as the wizard, you can look at this article: VS2008 custom C + + project templates and modify the default encoding of new files

The final question

This method of modifying the default file template to UTF-8 (with a signature) is actually not recommended, because it was Microsoft's wishful approach. And our project should have been cross-platform, this file is compiled under the Mac platform, there may be problems.

See this article: "Utf-8" with BOM and "No BOM utf-8" what is the difference? Which web page code is generally used?

However, since my attempt to develop cocos2d-x (without visual Studio) with ECLIPSE+CDT has failed, it seems to be forced to accept this behavior.

Related articles
    1. Create a Cocos2d-x Win32 project in Visual Studio Express 2012
    2. Solutions for Flash Builder installation and uninstallation errors
    3. Upgrade the air SDK in Flash Builder 4.7
    4. Cocos2d-x VS2012 hellocpp Commissioning
    5. Fast installation with SWFObject for FlashPlayer8 (Express install)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.