[英]Does extern declaration on static function break internal linkage?
I have a theoretical question. 我有一个理论上的问题。
Here's an example: 这是一个例子:
file1.c 文件1.c
static void foo()
{
...
}
file2.c 文件2.c
extern void foo();
main()
{
foo();
}
The compilation is OK. 编译正常。
So, the extern breaks the internal linkage of static declaration? 那么,外部中断了静态声明的内部链接吗?
Is the "compilation ok" only in theory, or did you actually test this? “编译还可以”仅是理论上的,还是您实际进行过测试?
A static
function should not be visible from outside the compilation unit (C file, typically) that it's in. 从其所在的编译单元(通常为C文件)外部不应看到
static
函数。
Declaring a function of the same name with extern
in a different compilation unit should not change this. 在不同的编译单元中使用
extern
声明相同名称的函数不应对此进行更改。
Make sure you really build and link the test program properly, since otherwise all that you're testing is that you can have a "dangling" reference to an external symbol (the foo
function referenced from 2.c). 确保您确实正确构建并链接了测试程序,因为否则,您要测试的全部就是可以对外部符号(从2.c引用的
foo
函数)进行“悬挂”引用。 Building 2.c into an executable (ie linking it) should fail. 将2.c编译为可执行文件(即链接它)应该会失败。
The compilation should be OK. 编译应该可以。 If you can also link that, your compiler have a problem.
如果还可以链接该链接,则说明编译器存在问题。
A quick test confirms the expected linker failure (VC++ 2010 express): 快速测试确认预期的链接器故障(VC ++ 2010 Express):
1>test.obj : error LNK2019: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ) referenced in function _wmain 1> test.obj:错误LNK2019:函数_wmain中引用的未解析的外部符号“ void __cdecl foo(void)”(?foo @@ YAXXZ)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.