名称空间和main() [英] namespaces and main()
问题描述
为什么以下不能链接(gcc 3.3)?
foo.cpp ="""
名称空间foo
{
int main()
{
返回0;
}
}
使用命名空间foo;
"""
它失败并带有符号_main未定义。当我结束使用声明时,它不会链接
:
externC {
使用命名空间foo;
}
这是什么问题?
poiuz24写道:aeh,这可能是愚蠢的,但可能是某人请向我解释为什么以下不能链接(gcc 3.3)?
foo.cpp ="""
namespace foo
{
使用命名空间foo;
"
它失败,带有符号_main未定义。当我像这样结束使用声明时它不会链接
externC {
使用命名空间foo;
}
是什么问题?
每个程序都应包含一个_global_''main''功能。你的函数
不是全局的,它在命名空间里面。 "使用"是为您的代码,而不是图书馆代码
。当你说使用命名空间foo时,你不会导致
重新编译会突然解决你的
" :: foo :: main"的库代码。通过使用不合格的名称main。
IOW,代码不是一个完整的C ++程序,因为它缺少
全局函数' 'main'',这是必需的。这就是为什么它不会链接。
Victor
poiuz24发布:
aeh,这可能是愚蠢的,但可能是某人请向我解释为什么以下不能链接(gcc 3.3)?
foo.cpp ="""
namespace foo
{
使用命名空间foo;
"
它失败,带有符号_main未定义。当我像这样结束使用声明时,它不会链接
:
externC {
使用命名空间foo;
}
是什么问题?
你的想法是正确的:
int foo :: main();
成为:
int :: main ();
后行
使用命名空间foo;
唯一的问题是这只适用于当前
翻译单位。所以当链接器被引入
函数时,它的名字是:
int foo :: main();
<当它想要的时候是
:
int :: main();
PS为什么它会说_main ;未定义?为什么
下划线?
-JKop
" JKop" < NU ** @ NULL.NULL>在留言中写道
news:xm ***************** @ news.indigo.ie ...poiuz24发布了:
aeh,这可能是愚蠢的,但可能是某人请向我解释为什么以下不能链接(gcc 3.3)?
foo.cpp ="""
namespace foo
{
使用命名空间foo;
"
它失败,带有符号_main未定义。它并没有链接当我像这样结束使用语句时:
externC {
使用命名空间foo;
}
是什么问题?
你的想法是正确的:
int foo :: main();
成为:
int :: main();
之后线路
使用命名空间foo;
唯一的问题是这只适用于当前的翻译单元。所以当链接器被引入
函数时,它的名字是:
int foo :: main();
当它想要的是什么时候:
int :: main();
PS为什么它会说_main未定义?为什么
下划线?
许多实现在创建
翻译输出之前''装饰''标识符。预先设定一个不常见的是一个相当的
常见做法。
-Mike
-JKop
aeh, this may be stupid, but could s.b. please explain
to me why the following won''t link (gcc 3.3)?
foo.cpp = """
namespace foo
{
int main ()
{
return 0;
}
}
using namespace foo;
"""
it fails with symbol "_main" undefined. it doesn''t link either
when i wrap up the using statement like so:
extern "C" {
using namespace foo;
}
what''s the problem?
poiuz24 wrote:aeh, this may be stupid, but could s.b. please explain
to me why the following won''t link (gcc 3.3)?
foo.cpp = """
namespace foo
{
int main ()
{
return 0;
}
}
using namespace foo;
"""
it fails with symbol "_main" undefined. it doesn''t link either
when i wrap up the using statement like so:
extern "C" {
using namespace foo;
}
what''s the problem?
Each program shall contain a _global_ ''main'' function. Your function
is not global, it''s inside a namespace. "using" is for your code, not
the library code. When you say "using namespace foo", you do not cause
recompilation of the library code that would suddenly resolve your
"::foo::main" by just using unqualified name "main".
IOW, the code is not a complete C++ program because it''s missing the
global function ''main'', which is required. That''s why it won''t link.
Victor
poiuz24 posted:
aeh, this may be stupid, but could s.b. please explain
to me why the following won''t link (gcc 3.3)?
foo.cpp = """
namespace foo
{
int main ()
{
return 0;
}
}
using namespace foo;
"""
it fails with symbol "_main" undefined. it doesn''t link either when i wrap up the using statement like so:
extern "C" {
using namespace foo;
}
what''s the problem?
You''re correct in thinking that:
int foo::main();
becomes:
int ::main();
after the line
using namespace foo;
The only problem is that this only applies to the current
translation unit. So when the linker is introduced to the
function, it''s name is:
int foo::main();
when what it wants is:
int ::main();
PS Why the hell does it say "_main" is undefined? Why the
underscore?
-JKop
"JKop" <NU**@NULL.NULL> wrote in message
news:xm*****************@news.indigo.ie...poiuz24 posted:aeh, this may be stupid, but could s.b. please explain
to me why the following won''t link (gcc 3.3)?
foo.cpp = """
namespace foo
{
int main ()
{
return 0;
}
}
using namespace foo;
"""
it fails with symbol "_main" undefined. it doesn''t link eitherwhen i wrap up the using statement like so:
extern "C" {
using namespace foo;
}
what''s the problem?
You''re correct in thinking that:
int foo::main();
becomes:
int ::main();
after the line
using namespace foo;
The only problem is that this only applies to the current
translation unit. So when the linker is introduced to the
function, it''s name is:
int foo::main();
when what it wants is:
int ::main();
PS Why the hell does it say "_main" is undefined? Why the
underscore?
Many implementations ''decorate'' identifiers before creating
the translated output. Prepending an undescore is a fairly
common practice.
-Mike
-JKop
这篇关于名称空间和main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!