Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   namespaces and main() (http://www.velocityreviews.com/forums/t284512-namespaces-and-main.html)

poiuz24 07-21-2004 04:58 PM

namespaces and main()
 
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?

Victor Bazarov 07-21-2004 05:09 PM

Re: namespaces and main()
 
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

JKop 07-21-2004 07:21 PM

Re: namespaces and main()
 
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

Mike Wahler 07-22-2004 12:43 AM

Re: namespaces and main()
 

"JKop" <NULL@NULL.NULL> wrote in message
news:xmzLc.5157$Z14.6504@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

> 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?


Many implementations 'decorate' identifiers before creating
the translated output. Prepending an undescore is a fairly
common practice.

-Mike

>
>
> -JKop




Mark A. Gibbs 07-22-2004 01:12 AM

Re: namespaces and main()
 

JKop wrote:
> PS Why the hell does it say "_main" is undefined? Why the
> underscore?


in c++, the compiler mangles function names in a compiler-specific way
by default (in my case, foo::main became ?main@foo@@YAHXZ). in "c"
linkage (extern "c" in a c++ program, default in a c program), the only
mangling is to prepend an underscore onto the function name. ::main()
uses c linkage.

indi


Owen Jacobson 07-22-2004 03:11 AM

Re: namespaces and main()
 
On Wed, 21 Jul 2004 13:09:56 -0400, Victor Bazarov wrote:

> 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.

>
> 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.


What I am about to write is probably undefined behaviour. To the OP:
DON'T DO THIS.

g++ -W -Wall -ansi -pedantic accepts the following without complaint:
--- g++-test.cpp
#include <iostream>
using std::cout;
using std::endl;

namespace foo {
extern "C"
int main () {
cout << "Main!" << endl;
}
}
---
$ g++ -W -Wall -ansi -pedantic g++-test.cpp -o g++-test
$ ./g++-test
Main!

Is this, in fact, undefined behaviour? The program entry point is defined
in terms of the function declaration, not linker names, right? I'm not
losing my mind, right?

Owen

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.


poiuz24 07-22-2004 06:26 AM

Re: namespaces and main()
 
> 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


but i don't have no library: above is all in _one_ file. thus, the
program == one translation unit == one file

i thought the using stmt would introduce ::foo::main into ::
since the using stmt is in ::

???

poiuz24 07-22-2004 06:26 AM

Re: namespaces and main()
 
> 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


but i don't have no library: above is all in _one_ file. thus, the
program == one translation unit == one file

i thought the using stmt would introduce ::foo::main into ::
since the using stmt is in ::

???

John Harrison 07-22-2004 06:46 AM

Re: namespaces and main()
 
On 21 Jul 2004 23:26:46 -0700, poiuz24 <poiuz24@web.de> wrote:

>> 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

>
> but i don't have no library: above is all in _one_ file. thus, the
> program == one translation unit == one file
>
> i thought the using stmt would introduce ::foo::main into ::
> since the using stmt is in ::
>
> ???


By library code Victor means the library code that calls main.

You must have a function called main in the global namespace.

A using statement just changes the way names are lookup up, it does not
means that a name defined in one namespace is now defined in another.

john

poiuz24 07-22-2004 06:55 AM

Re: namespaces and main()
 
> 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


i don't get it: there is only _one_ translation unit
involved: foo.cpp

but ok, you say: if foo::main would reside in a different,
separately compiled translation unit, then the compiler
would leave the mangled foo::main in say libXXX.o which would
be different from a mangled ::main. no using stmt could
change that symbol in libXXX.o from another translation unit.

so far so good, but even then: in my driving program, i
introduce foo::main as a synonym for ::main. why is then no
symbol emitted for ::main in my driving program .o?

actually, g++ really emits the mangled foo::main :

-bash-2.05b$ g++ -c foo.cpp
-bash-2.05b$ nm foo.o
00000000 t ___gnu_compiled_cplusplus
00000000 T _f__3fooi
00000018 T _main__3foov
00000000 t gcc2_compiled.
-bash-2.05b$ g++ --version
2.95.3
-bash-2.05b$


tob@blackbox ~/test4
$ g++ -c foo.cpp

tob@blackbox ~/test4
$ nm foo.o
00000000 b .bss
00000000 d .data
00000000 t .text
00000000 T __ZN3foo4mainEv

tob@blackbox ~/test4
$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


tob@blackbox ~/test4
$


btw: i checked if "using foo::main;" would do any better. nope.
same mangled name.

this is all strange .. "using" introduces synonyms which are
fully transparent for the programmer/compiler, but not for
the linker?


All times are GMT. The time now is 01:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.