简体   繁体   中英

How can I make Visual Studio save all files as UTF-8 without signature on Project or Solution level?

I am trying to configure a VS c++ project in a way that it can be compiled by gcc in Linux. It seems that I need the files to be encoded as UTF-8 without signature (which is not the default). Is it possible to set something on the project or solution level, so that after someone opens the solution and saves their changes the files are still UTF-8?

Please note that it is an open project, so I can't ask everyone to change their Visual Studio settings.

Visual Studio will remove the BOM by going to Save As... and selecting "Save With Encoding..." and selecting "UTF-8 without signature". Once it is saved without the BOM, VS will not add it again. Unfortunately, there is no way to make this default for all files in VS and must be done manually each time a file is saved for the first time.

If you have Cygwin installed you can batch modify existing files with this little script:

find . -name "*.cpp" -exec vim -c "set nobomb" -c wq! {} \;

Or, if you don't have Cygwin but you do have vim you can use this batch script.

for %%f in (*.cpp) do call vim -c "set nobomb" -c wq! %%f

Note, doing this in a batch script, it seems I need to hit [return] each time vim exits which isn't the case with the cygwin version.

in vs2010 should be ok to set globally - see: UTF-8 without BOM

have only got vs express with me at the mo, and naturally that doesnt have this. sigh.

The way I did it:

  1. wrote a small app calling a silent process using Notepad. Simply process in shell (using System.Diagnostics)
  2. created a list of files and foreach file, opened and saved as utf-8

took 20 minutes writing 1. and a few minutes executing.
I cannot think of opening and saving all 300 files manually

It doesn't seem like this is a setting you can put into a propertyPage. The best I can think of is to create a simple script (as in this link ) that runs as a pre-build step and re-writes the files. That would probably do the job as long as the script were kept with the project, but might be super annoying.

It appears that Visual Studio is guessing the character encoding based on the file contents. If you put a standard comment header in each file, and have that comment include an unambiguous UTF-8 character, VS should do the right thing. A side benefit is that some Linux utilities will also recognize the file as being explicitly UTF-8 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM