为什么这个程序被三个C ++编译器错误地拒绝? [英] Why is this program erroneously rejected by three C++ compilers?

查看:284
本文介绍了为什么这个程序被三个C ++编译器错误地拒绝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个我编写的C ++程序时遇到了一些困难。

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

这个程序非常简单,据我所知,符合所有在C ++标准中阐述的规则。我已经阅读过两次ISO / IEC 14882:2003。

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.

程序如下:

>

这是我试图使用Visual C ++ 2010编译此程序时收到的输出:

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,我试过g ++ 4.5.2,但同样没有帮助:

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



我认为Clang(3.0版本的trunk 127530)必须工作,称赞其标准一致性。不幸的是,它甚至没有给我一个漂亮的,突出显示的错误消息:

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.

许多其他C ++程序的源文件扩展名为.cpp ,所以我想我可能需要重命名我的文件。我改名为 helloworld.cpp ,但没有帮助。我认为Clang有一个非常严重的错误,因为当我试图使用它来编译重命名的程序,它翻转出来,打印84警告和20错误生成。并让我的电脑嘟嘟了很多!

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!

我在这里做错了什么?我错过了C ++标准的一些关键部分吗?或者是所有三个编译器真的只是这么破,他们不能编译这个简单的程序?

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?

推荐答案

在标准中,§2.1/ 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.

请注意,C ++标准基于C标准(§1.1/ 2)标准在§1.2中说:

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


本国际标准没有规定

- C程序的机制转换以供数据处理
系统使用;

- 调用C程序以供数据处理
系统使用的机制;

- 输入数据被转换以供C程序使用的机制;

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.

这篇关于为什么这个程序被三个C ++编译器错误地拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆