简体   繁体   English

如何使Visual Studio将所有文件保存为UTF-8而不在项目或解决方案级别签名?

[英]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. 我试图以一种可以在Linux中由gcc编译的方式配置VS c ++项目。 It seems that I need the files to be encoded as UTF-8 without signature (which is not the default). 似乎我需要将文件编码为UTF-8而不签名(这不是默认值)。 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? 是否可以在项目或解决方案级别设置某些内容,以便在有人打开解决方案并保存其更改后,文件仍为UTF-8?

Please note that it is an open project, so I can't ask everyone to change their Visual Studio settings. 请注意,它是一个开放项目,所以我不能要求每个人都改变他们的Visual Studio设置。

Visual Studio will remove the BOM by going to Save As... and selecting "Save With Encoding..." and selecting "UTF-8 without signature". Visual Studio将通过转到另存为...并选择“使用编码保存...”并选择“无签名的UTF-8”来删除BOM。 Once it is saved without the BOM, VS will not add it again. 一旦没有BOM保存,VS将不再添加它。 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. 不幸的是,没有办法为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版本不同。

in vs2010 should be ok to set globally - see: UTF-8 without BOM 在vs2010中应该可以全局设置 - 请参阅: 没有BOM的UTF-8

have only got vs express with me at the mo, and naturally that doesnt have this. 我只是在mo与我一起表达,自然也没有这个。 sigh. 叹。

The way I did it: 我这样做的方式:

  1. wrote a small app calling a silent process using Notepad. 写了一个小应用程序,使用记事本调用静默进程。 Simply process in shell (using System.Diagnostics) 只需在shell中处理(使用System.Diagnostics)
  2. created a list of files and foreach file, opened and saved as utf-8 创建了一个文件和foreach文件列表,打开并保存为utf-8

took 20 minutes writing 1. and a few minutes executing. 花了20分钟写了1.并且执行了几分钟。
I cannot think of opening and saving all 300 files manually 我想不出手动打开和保存所有300个文件

It doesn't seem like this is a setting you can put into a propertyPage. 这似乎不是你可以放入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. 看来Visual Studio正在根据文件内容猜测字符编码。 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. 如果你在每个文件中放置一个标准的注释标题,并且该注释包含一个明确的UTF-8字符,VS应该做正确的事情。 A side benefit is that some Linux utilities will also recognize the file as being explicitly UTF-8 as well. 另一个好处是,某些Linux实用程序也会将该文件识别为明确的UTF-8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio强制UTF-8没有签名文件编码 - Visual Studio force UTF-8 with no signature file encoding 如何从Visual Studio中的项目解决方案中删除其他构建文件 - How to Delete Additional build files from the Project Solution in visual studio 如何在控制台中使动态字符串与UTF-8一起使用? - How can I make dynamic strings to work with UTF-8 in console? 如何在不将.cpp文件包含到Visual Studio解决方案中的情况下对其进行引用? - How to reference .cpp files without including them into the solution in Visual Studio? 如何从make文件创建Visual Studio解决方案? - How to create Visual studio solution from make files? 在Visual Studio中的解决方案资源管理器中组织项目文件 - Organizing project files in Solution Explorer in Visual Studio 如何在Visual Studio 2010中创建C ++项目/解决方案? - How can I create a C++ project/solution in Visual Studio 2010? 如何运行 Visual Studio 解决方案而无需在解决方案资源管理器中添加包含的文件显示? - How to run a Visual Studio solution without having to add the included files show in the Solution Explorer? 如何在 Visual Studio 项目中“单独”运行 C++ 文件而不受其他文件的影响? - How do I run a C++ file “by itself” in a Visual Studio project without the influence of other files? 如何在没有Qt项目的情况下在Visual Studio中使用Qt资源文件? - How to use Qt resource files in Visual Studio without a Qt project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM