Why is this program erroneously rejected by three C++ compilers?(为什么这个程序被三个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
沮丧,我尝试了 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 版主干 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),而 C(99) 标准在 §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++编译器错误拒绝了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么这个程序被三个C++编译器错误拒绝了?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01