Why is this program erroneously rejected by three C++ compilers?

355,174

Solution 1

In the standard, §2.1/1 specifies:

Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary.

Your compiler doesn't support that format (aka cannot map it to the basic source character set), so it cannot move into further processing stages, hence the error. It is entirely possible that your compiler support a mapping from image to basic source character set, but is not required to.

Since this mapping is implementation-defined, you'll need to look at your implementations documentation to see the file formats it supports. Typically, every major compiler vendor supports (canonically defined) text files: any file produced by a text editor, typically a series of characters.


Note that the C++ standard is based off the C standard (§1.1/2), and the C(99) standard says, in §1.2:

This International Standard does not specify
— the mechanism by which C programs are transformed for use by a data-processing system;
— the mechanism by which C programs are invoked for use by a data-processing system;
— the mechanism by which input data are transformed for use by a C program;

So, again, the treatment of source files is something you need to find in your compilers documentation.

Solution 2

Originally from Overv @ reddit.

Solution 3

Try this way:

enter image description here

Solution 4

Your < and >, ( and ), { and } don't seem to match very well; Try drawing them better.

Solution 5

You could try the following python script. Note that you need to install PIL and pytesser.

from pytesser import *
image = Image.open('helloworld.png')  # Open image object using PIL
print image_to_string(image)     # Run tesseract.exe on image

To use it, do:

python script.py > helloworld.cpp; g++ helloworld.cpp
Share:
355,174
James McNellis
Author by

James McNellis

C++ maven. Systems programmer. Scotch connoisseur. Classical music enthusiast. Photographer. Principal Engineer at Microsoft working on compilers. 25th legendary Stack Overflow contributor.

Updated on July 03, 2020

Comments

  • James McNellis
    James McNellis almost 4 years

    I am having some difficulty compiling a C++ program that I've written.

    This program is very simple and, to the best of my knowledge, conforms to all the rules set forth in the C++ Standard. I've read over the entirety of ISO/IEC 14882:2003 twice to be sure.

    The program is as follows:

    enter image description here

    Here is the output I received when trying to compile this program with Visual C++ 2010:

    c:\dev>cl /nologo helloworld.png
    cl : Command line warning D9024 : unrecognized source file type 'helloworld.png', object file assumed
    helloworld.png : fatal error LNK1107: invalid or corrupt file: cannot read at 0x5172
    

    Dismayed, I tried g++ 4.5.2, but it was equally unhelpful:

    c:\dev>g++ helloworld.png
    helloworld.png: file not recognized: File format not recognized
    collect2: ld returned 1 exit status
    

    I figured that Clang (version 3.0 trunk 127530) must work, since it is so highly praised for its standards conformance. Unfortunately, it didn't even give me one of its pretty, highlighted error messages:

    c:\dev>clang++ helloworld.png
    helloworld.png: file not recognized: File format not recognized
    collect2: ld returned 1 exit status
    clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
    

    To be honest, I don't really know what any of these error message mean.

    Many other C++ programs have source files with a .cpp extension, so I thought perhaps I needed to rename my file. I changed its name to helloworld.cpp, but that didn't help. I think there is a very serious bug in Clang because when I tried using it to compile the renamed program, it flipped out, printed "84 warnings and 20 errors generated." and made my computer beep a lot!

    What have I done wrong here? Have I missed some critical part of the C++ Standard? Or are all three compilers really just so broken that they can't compile this simple program?

  • jweyrich
    jweyrich about 13 years
    The chicken & egg dilemma: Is it possible to write C++ code for an OCR software and compile it without an OCR?
  • James McNellis
    James McNellis about 13 years
    Hmmm. Thankfully this ridiculous rule has been removed in C++0x. That said, how does one end such a file with a newline? I thought I had left sufficient room at the end of the text (if you highlight the source file, you should see the extra room I left). Thanks for the tip, though!
  • James McNellis
    James McNellis about 13 years
    I think that sentence is ambiguous at best. The Merriam-Webster dictionary says that text is the original words and form of a written or printed work or a work containing such text. This source file clearly falls under that definition. Do you think I should file a defect report with the Core Language Working Group?
  • Kevin Lacquement
    Kevin Lacquement about 13 years
    Well, duh, you use assembly for the original OCR.
  • James McNellis
    James McNellis about 13 years
    While I don't appreciate you making fun of my handwriting, this might be the real issue, and would explain the error I get when I try compiling the renamed helloworld.cpp with Visual C++: "fatal error C1004: unexpected end-of-file found" I'll try again and report back soon. Thanks!
  • Michael Burr
    Michael Burr about 13 years
    @jweyrich - I think you'll need to get your C++/OCR bootstrapped with your asm/OCR toolchain first.
  • James McNellis
    James McNellis about 13 years
    Oh, that kind of makes sense... I have a box of 96 crayons, so I'm sure I have the correct foreground color. I'll pick up some colored construction paper tomorrow and try it out on a different color of paper.
  • jweyrich
    jweyrich about 13 years
    Oh ASM, yes! ASM in punched cards.
  • James McNellis
    James McNellis about 13 years
    Unfortunately, this is the only font that my hand supports. That would be very sad if I am unable to program in C++ because of this. Do you think Java would support this font?
  • James McNellis
    James McNellis about 13 years
    Oh! I thought the preprocessor was included with the compiler! I'll try to find a preprocessor that works on my Windows laptop.
  • James McNellis
    James McNellis about 13 years
    Oh; I completely forgot to read all of the referenced documents. I think that paragraph is taken out of context, though, so I shall go and read the entirety of ISO/IEC 9899:1990 and will post back here once I fully understand it.
  • Dean Burge
    Dean Burge about 13 years
    @James make sure you turn off all png optimizations. it makes debugging easier.
  • helloworld922
    helloworld922 about 13 years
    Just to be safe, you better get some coloring pencils and oil-based paint as well. It's a well known fact that C++ is meant to be a very difficult language to format correctly.
  • sharptooth
    sharptooth about 13 years
    Yeap, and don't forget to use the highlighting marker.
  • sharptooth
    sharptooth about 13 years
    You will need Comic Sans when you think of drawing comics anyway, so you should seriously consider upgrading hands.
  • Tobias Kienzler
    Tobias Kienzler about 13 years
    Best. BMP format. Specification illustration. Ever.
  • Tobias Kienzler
    Tobias Kienzler about 13 years
    You should probably use 80 or even 72 columns instead
  • Raedwald
    Raedwald about 13 years
    Yes, try installing Linux. I blame Windows for your problem.
  • Frank Osterfeld
    Frank Osterfeld about 13 years
    C++ requires year-long training in calligraphy. If you don't have the time, try Visual Basic or just binary machine code (You just have to get the 0's and 1's right then).
  • James P.
    James P. about 13 years
    Check the date on your PC.
  • Cody Gray
    Cody Gray about 13 years
    Haha, but I finally found an easy one I could answer!
  • Admin
    Admin about 13 years
    @sharptooth - syntax highlighting is an IDE feature - you're not meant to do it by hand. So make sure you get a robot arm to go with that highlighting marker.
  • Admin
    Admin about 13 years
    A Wacom Cintiq is much more appropriate for a manager. It's expensive, and makes you feel really important. Any graphic designers in your company will have much lower status, and should therefore use EGA monitors. Janitors should use CGA monitors. Programmers should use second hand monochrome terminals.
  • Icode4food
    Icode4food about 13 years
    I personally prefer #include <JonSkeet>.
  • James P.
    James P. about 13 years
    Wow, OCR has improved since I attempted to scan my handwriting (spent hours writing it straight too).
  • sharptooth
    sharptooth about 13 years
    @James McNellis: The preprocessor isn't a program, it's a hardware thing that looks like a highlighting marker - you move it over your text and it gets preprocessed.
  • PJP
    PJP about 13 years
    I had a "Life Like" monitor for a long time. It was so realistic that you'd swear the screensaver of swimming fish was real, and the little diver man looked like he was swimming. I kept getting my arm wet trying to get the treasure chest from the bottom it was so real. The only problem was the screen saver was always on and the realistic bubbling noises made it hard to hear. Oh, and they said for maintenance I had to sprinkle stuff in the top of the monitor daily or the screen saver would stop working. It did that once, and boy, the smell two days later was really realistic.
  • PJP
    PJP about 13 years
    This is silly. We all know the compiler would optimize out the whitespace, leaving only heavily compressed black-space, which is all ones and would compress down to a binary 1 which would be returned as an error. The code needed to be written using white-out which would compile to 0 and not return an error.
  • PJP
    PJP about 13 years
    If you don't have enough whitespace I can try compiling it on my system. I have four monitors so I could try compiling from my leftmost one.
  • Admin
    Admin about 13 years
    I thought #pragma was the correct way to "get a message" to the compiler?
  • Rup
    Rup about 13 years
    I think there's a typo - it should be endl (L) not end1 (one). But +1 nicely done!
  • Michael Burr
    Michael Burr about 13 years
    I had a professor way back when who would take off points your homework or exams if you put a slash through a zero digit since zero isn't the null set. He would appreciate this answer.
  • Carson63000
    Carson63000 about 13 years
    @James: "unexpected end-of-file" almost certainly means that it is your } that is causing the problem. Try focusing on matching that with the {
  • oosterwal
    oosterwal about 13 years
    I've been staring at this for three hours but I still can't see a dinosaur or the space shuttle. :-(
  • Andreas Grech
    Andreas Grech about 13 years
    Inspired by this beautiful answer, I decided I'd do something similar for brainf***: blog.dreasgrech.com/2011/04/…
  • MSalters
    MSalters about 13 years
    I think we need to add a Perl tag.
  • Mateen Ulhaq
    Mateen Ulhaq about 13 years
    @Frank C++0x §42.1/1 specifies "All strings must be in Gothic."
  • Mateen Ulhaq
    Mateen Ulhaq about 13 years
    I prefer typing out std all the time. Reminds me not to get one.
  • Mateen Ulhaq
    Mateen Ulhaq about 13 years
    It's a capital letter O with a special line we call "diameter", which tells the compiler to use the Midpoint Circle Algorithm, obviously. I think you should get your eyes checked.
  • Adrian Ratnapala
    Adrian Ratnapala over 12 years
    Last I heard C++ does not specify that the programs have to be written in ASCII, UTF-8 or anything else.