griffin175
griffin175

Reputation: 131

Why does the g++ compiler add spaces between every character in my cpp file?

I'm trying to compile 3 cpp files, for only one of them, the g++ compiler on linux is reading spaces between every character on the making it impossible to compile. I get hundreds, if not thousands, of x.cpp:n:n: warning: null character(s) ignored (where x is a name and n is a number). I wrote the program in Visual studio and I copied them to linux. The other 2 files compile fine, I've done this for dozens of projects. How does this happen?

I managed to fix this issue by creating a new file and copying the text from the original cpp instead of copying the file.

Now I get an error from the terminal saying Permission Denied when I try launch the .o file

Upvotes: 0

Views: 965

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20812

There is no text but encoded text.

Dogmatic corollaries:

Authors choose a character encoding for a text file.

Readers must know what it is.

Any kind of shared understanding will do: specification, convention, internal tagging, metadata upon transfer, …. (Even last century's way of converting upon transfer would do in some cases.)


It seems you 1) didn't know what you chose. 2) didn't bring that knowledge with you when you copied the file between systems, and 3) didn't tell GCC.

Unfortunately, there has been a culture of hiding these basic communication needs, instead of doing it mindfully; so, your experience too much too common.

To tell GCC,

g++ -finput-charset=utf-16

Obviously, if you are using some sort of project system that supports keeping track of the required metadata of the relevant text files and passing it tools, that would be preferred.


You could try adopting UTF-8 Everywhere. That won't eliminate the need for communication (until maybe the middle of this century) but it could make it more agreeable.

Upvotes: 0

catnip
catnip

Reputation: 25388

Your compiler problem is nothing to do with linebreaks.

You're trying to compile a file saved as UTF-16 (Unicode). Visual Studio will do this behind your back if the file contains any non-ASCII characters.

Solution 1 (recommended): stick to ASCII. Then the problem simply won't arise in the first place.

Solution 2: save the file in Visual Studio as UTF-8, as described here. You might need to save the file without a BOM (byte-order mark) as described here.

WRT your other problem, look for a file called a.out (yes, really) and try running that. And don't specify -c on the g++ command line.

Upvotes: 3

Related Questions