Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > namespaces and main()

Reply
Thread Tools

namespaces and main()

 
 
poiuz24
Guest
Posts: n/a
 
      07-21-2004
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-21-2004
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
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      07-21-2004
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-22-2004

"JKop" <(E-Mail Removed)> wrote in message
newsmzLc.5157$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Mark A. Gibbs
Guest
Posts: n/a
 
      07-22-2004

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

 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      07-22-2004
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.

 
Reply With Quote
 
poiuz24
Guest
Posts: n/a
 
      07-22-2004
> 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 ::

???
 
Reply With Quote
 
poiuz24
Guest
Posts: n/a
 
      07-22-2004
> 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 ::

???
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-22-2004
On 21 Jul 2004 23:26:46 -0700, poiuz24 <(E-Mail Removed)> 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
 
Reply With Quote
 
poiuz24
Guest
Posts: n/a
 
      07-22-2004
> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 and namespaces in web apps Amil ASP .Net 2 03-14-2006 09:28 PM
Namespaces in Vs2005 and ASP.net 2.0 Tariq ASP .Net 1 11-29-2005 07:14 PM
Namespaces and Naming conventions Floppy Jellopy ASP .Net 4 07-21-2005 01:36 PM
@Import Syntax and Importing Namespaces in global.asax file D. Shane Fowlkes ASP .Net 1 01-13-2004 02:55 PM
JSTL, XML and namespaces Digby Java 0 10-29-2003 07:54 AM



Advertisments