11

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.

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • I'm assuming this is a cr/lf vs cr problem, if so an alternative approach might be to change your Linux makefile, so that it dos2unix's the source as part of it's build... or you may be able to reconfigure your source control to extract the appropriate line endings. – forsvarir Mar 24 '11 at 11:50
  • 1
    cr/lf vs cr is fine, its the actual encoding of the file (i.e. the first two bytes in the file and then every second byte - because it seems to be utf-16) – Grzenio Mar 24 '11 at 11:58

5 Answers5

18

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.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
  • I tried the first one in Linux and got `find: missing argument to `-exec'` – Grzenio Mar 25 '11 at 12:07
  • The second one (again in Linux) gives error: `bash: $i: ambiguous redirect` – Grzenio Mar 25 '11 at 12:09
  • Yeah don't do the second one (I removed it from the post now). I never actually tried it, I just saw it somewhere else and it looked right. I missed the `\;` on the first (which I have used before for this exact problem), it is fixed now. – Dave Rager Mar 25 '11 at 14:22
  • If VS is able to remember that it has saved the file without a BOM once, it must store the information somewhere. Open a file, save it with encoding, close VS, and look at the modification times of the files. One of them should have been updated, and you can look inside it to see how the file is flagged. – Mark Ransom Mar 30 '11 at 01:55
  • @Mark, VS doesn't remember anything about how it saved a file (at least not that I am aware of). It just doesn't change the encoding on existing files (hence the script to batch modify the encoding outside VS). – Dave Rager Mar 30 '11 at 15:17
2

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.

Community
  • 1
  • 1
steve
  • 778
  • 1
  • 8
  • 18
0

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.

Community
  • 1
  • 1
JCooper
  • 6,395
  • 1
  • 25
  • 31
0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
-1

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

rptmat57
  • 3,643
  • 1
  • 27
  • 38
Avner001
  • 15
  • 1
  • 15
    It would be better to share your app's code to save 20 minutes to those who are looking for solution. – Artemix Nov 05 '12 at 13:54