编译一个空文件是否遵循C ++标准? [英] Does compiling an empty file follow the C++ standard?

查看:131
本文介绍了编译一个空文件是否遵循C ++标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1994年混乱的C大赛中有一个条目,该条目被认定为最小的奎宁。它只是一个空文件。

There was an entry in the 1994 Obfuscated C contest that qualified as the smallest quine. It was just an empty file.

C ++规范中是否有允许编译空文件的东西?如果没有,一个有效的程序的最低限度是多少?我模糊地记得读某处有一个特殊的情况,一个空文件在C ++规范中给出一个默认实现,但我找不到参考。

Is there something in the C++ spec that allows for compiling empty files? If not, what is the bare minimum for a "valid program?" I vaguely remember reading somewhere that there was a special case where an empty file is given a default implementation in the C++ spec, but I cannot find the reference.

我试过这个,虽然我不知道它必然是令人信服的。

I tried this, though I don't know that it is necessarily convincing.

$ rm main_empty.cpp
rm: cannot remove `main_empty.cpp': No such file or directory
$ touch main_empty.cpp
$ g++ -o empty main_empty.cpp
/usr/lib/gcc/.../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

缺少主。

$ g++ -Wl,--defsym=_start=_exit -Wl,--undefined=_exit \
    -nostartfiles -static -o empty main_empty.cpp

UPDATE:

注意到 main_empty.cpp 是多余的。

我添加了一些静态垃圾到 main_empty.cpp 看看它是否表现在不同的行为,它没有。但它确实改变了可执行文件的大小。

I added some static junk to the main_empty.cpp to see if it manifested in different behavior and it did not. It did change the executable size however.

#include <iostream>

struct Foo {
    Foo() {
        std::cout << "hi" << std::endl;
    }
} foo;

如果您向文件中添加 main

推荐答案

C ++ draft从2012-11- 02。 3.6.1:

C++ draft from 2012-11-02. 3.6.1:


程序应包含一个名为main的全局函数,它是程序的指定开始。它
是实现定义的是否需要在独立环境中的程序来定义主
函数。 [注:在独立的环境中,启动和终止是由实现定义的; start-
up包含对具有静态存储持续时间的命名空间范围对象的构造函数的执行;
终止包含对静态存储持续时间的对象执行析构函数。 - end note]

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; start- up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. — end note ]

这篇关于编译一个空文件是否遵循C ++标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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