外部源文件中没有发生重新定义错误 [英] redefinition error doesn't happen in external source file

查看:99
本文介绍了外部源文件中没有发生重新定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码:

//main.c 

int t;
double t = 0;

int main()
{
   return 0;
}

我们可以看到上面的代码没有编译,并且gcc抛出错误:"t redefinition;不同的基本类型",就足够公平了.

we can see that above code doesn't compile and gcc throw an error:"t redefinition; different basic types", fair enough.

但是如果我添加另一个源文件来将t定义为:

But if I add another soure file to define t as:

//main.c 

int t;

int main()
{
   return 0;
}

//test.c

double t = 0;

现在代码可以编译了,但是这里不是同样的问题吗?在main.c中,返回类型为int,而在test.c中,返回类型为doublle,类型不一致吗?为什么这次可以编译?

now the code compiles, but isn't it still same problem here? in main.c, the return type is int while in test.c the return type is doublle, inconsistent type? why it can compile this time?

推荐答案

这里不是同样的问题吗?

isn't it still same problem here?

是,不是.语言规范的第6.2.7/2段说:

Yes and no. Paragraph 6.2.7/2 of the language specification says:

所有引用相同对象或函数的声明都应具有 兼容类型否则,行为是不确定的.

All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.

假设在您的第二个示例中,给出的两个翻译单元将被评估为对一个程序有贡献,那么您的两个版本的代码都确实违反了该规定,因此它们具有未定义的行为.但是,不需要C实现即可诊断出该问题,也不需要以某种特定的方式(或根本没有失败)来解决该问题.

Supposing that in your second example, the two translation units presented are to be evaluated as contributing to one program, then both versions of your code do violate that provision, and therefore they have undefined behavior. But a C implementation is not required to diagnose that problem, nor to fail in any particular way (or at all) in the face of it.

但是,您的第一个示例也违反了6.7/4节,而您的第二个示例没有:

However, your first example also violates paragraph 6.7/4, whereas your second example does not:

在同一范围内引用相同对象或函数的所有声明都应指定兼容类型.

All declarations in the same scope that refer to the same object or function shall specify compatible types.

这是一种语言约束,这意味着需要实施才能对其进行诊断(就像您所做的那样).不需要 拒绝不符合此约束的代码,但是这种代码的行为是不确定的,并且在可用行为的范围内,拒绝它是更好的选择之一.

This one is a language constraint, which means that implementations are required to diagnose it (as yours does). Implementations are not required to reject code that fails to conform to this constraint, but the behavior of such code is undefined, and rejecting it is among the better alternatives in the universe of available behaviors.

在main.c中,返回类型为int,而在test.c中,返回类型为doublle,类型不一致吗?为什么这次可以编译?

in main.c, the return type is int while in test.c the return type is doublle, inconsistent type? why it can compile this time?

C语言规范没有说,但是由于在这种情况下未定义实现在翻译程序时的行为和已翻译程序的行为(如果有),因此接受代码并将其成功编译为可执行文件是允许的.我推测您的特定实现完全不会从程序中删除t,因为它从未被访问过.

The C language specifications do not say, but inasmuch as the behavior of the implementation in translating the program and the behavior of the translated program, if any, are undefined in this case, accepting the code and successfully compiling it to an executable is permitted. I speculate that your particular implementation just omits t from the program altogether, on account of it never being accessed.

这篇关于外部源文件中没有发生重新定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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