C++ 如何让 Visual Studio 将所有文件保存为 UTF-8 而无需在项目或解决方案级别签名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5418557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make Visual Studio save all files as UTF-8 without signature on Project or Solution level?
提问by Grzenio
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?
我正在尝试以一种可以在 Linux 中由 gcc 编译的方式配置 VS c++ 项目。似乎我需要将文件编码为没有签名的 UTF-8(这不是默认值)。是否可以在项目或解决方案级别进行设置,以便在有人打开解决方案并保存更改后,文件仍然是 UTF-8?
Please note that it is an open project, so I can't ask everyone to change their Visual Studio settings.
请注意,这是一个开放项目,所以我不能要求每个人更改他们的 Visual Studio 设置。
回答by Dave Rager
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.
Visual Studio 将通过转到“另存为...”并选择“使用编码保存...”并选择“无签名的 UTF-8”来删除 BOM。一旦在没有 BOM 的情况下保存,VS 将不会再次添加它。不幸的是,没有办法在 VS 中为所有文件设置这个默认值,并且必须在每次第一次保存文件时手动完成。
If you have Cygwin installed you can batch modify existing files with this little script:
如果您安装了 Cygwin,您可以使用这个小脚本批量修改现有文件:
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.
或者,如果您没有 Cygwin 但有 vim,则可以使用此批处理脚本。
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.
请注意,在批处理脚本中执行此操作,似乎每次 vim 退出时我都需要点击 [return],而 cygwin 版本并非如此。
回答by steve
in vs2010 should be ok to set globally - see: UTF-8 without BOM
在 vs2010 中应该可以全局设置 - 请参阅: UTF-8 without BOM
have only got vs express with me at the mo, and naturally that doesnt have this. sigh.
莫里只有vs express跟我,自然没有这个。叹。
回答by Mark Ransom
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.
看来 Visual Studio 正在根据文件内容猜测字符编码。如果您在每个文件中放置一个标准的注释标题,并且该注释包含一个明确的 UTF-8 字符,那么 VS 应该做正确的事情。一个附带的好处是一些 Linux 实用程序也会将文件识别为明确的 UTF-8。
回答by Avner001
The way I did it:
我是这样做的:
- wrote a small app calling a silent process using Notepad. Simply process in shell (using System.Diagnostics)
- created a list of files and foreach file, opened and saved as utf-8
- 使用记事本编写了一个调用静默进程的小应用程序。只需在 shell 中处理(使用 System.Diagnostics)
- 创建了一个文件列表和 foreach 文件,打开并保存为 utf-8
took 20 minutes writing 1. and a few minutes executing.
I cannot think of opening and saving all 300 files manually
写 1. 花了 20 分钟,执行了几分钟。
我想不出手动打开和保存所有 300 个文件
回答by JCooper
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.
这似乎不是您可以放入 propertyPage 的设置。我能想到的最好方法是创建一个简单的脚本(如本链接中所示),作为预构建步骤运行并重写文件。只要脚本保留在项目中,这可能就可以完成工作,但可能会非常烦人。