Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unnamed namespace problem

Reply
Thread Tools

unnamed namespace problem

 
 
Sandy
Guest
Posts: n/a
 
      09-24-2005
Hi,
I have two files as folllows

file1.cpp
#include<iostream>
using namespace std;
namespace {
void show();
void fun() { cout<<"fun called\n"; }
}

int main()
{
show();
return 0;
}


file2.cpp
#include<iostream>
using namespace std;
namespace{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}

While trying to run this the linker is giving following message
Undefined first referenced
symbol in file
(anonymous namespace)::fun() file2.o
(anonymous namespace)::show() file1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


As far as i think, it should be able to find the definitions because
everything here belongs to a single "un-named" namespace.
Then why am i getting the problem?


 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      09-24-2005

Sandy wrote:
> Hi,
> I have two files as folllows
>
> file1.cpp
> #include<iostream>
> using namespace std;
> namespace {
> void show();
> void fun() { cout<<"fun called\n"; }
> }
>
> int main()
> {
> show();
> return 0;
> }
>
>
> file2.cpp
> #include<iostream>
> using namespace std;
> namespace{
> void fun();
> void show(){
> fun();
> cout<<"show called\n";
> }
> }
>
> While trying to run this the linker is giving following message
> Undefined first referenced
> symbol in file
> (anonymous namespace)::fun() file2.o
> (anonymous namespace)::show() file1.o
> ld: fatal: Symbol referencing errors. No output written to a.out
> collect2: ld returned 1 exit status
>
>
> As far as i think, it should be able to find the definitions because
> everything here belongs to a single "un-named" namespace.
> Then why am i getting the problem?


Every unnamed namespace is unique - which means that every unnamed
namespace is a different namespace from every other unnamed namespace.
Not only across files and even across time. An unnamed namespace may
not even be the same unnamed namespace that it was before it was last
compiled.

In this case there are two show() functions and two fun() functions
declared, but fewer than four functions are actually defined.

Greg

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-24-2005
Greg wrote:

> Every unnamed namespace is unique - which means that every unnamed
> namespace is a different namespace from every other unnamed namespace.
> Not only across files and even across time. An unnamed namespace may
> not even be the same unnamed namespace that it was before it was last
> compiled.


That's NOT true.

All the unnamed namespaces in a single translation unit are the same
namespace. I'm not even sure what the point you're trying to make
about time is. Multiple unnamed namespaces in the same file are hence
the same namespace.

The namespace is however different from other namespaces in other
translation units (which is what the user has).
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      09-25-2005
"Sandy" wrote:
> Hi,


Hello.

> I have two files as folllows
>
> file1.cpp
> #include<iostream>
> using namespace std;


Ewwwwwwwww. That dumps thousands of names from namespace "std"
into the global namespace. Bad idea. (Creates danger of name
collision.) I wish textbook authors would stop telling people
to do that.

> namespace {
> void show();
> void fun() { cout<<"fun called\n"; }
> }
>
> int main()
> {
> show();
> return 0;
> }
>


And the name of this namespace is??????????????
If you want to be able to access stuff in that
namespace from other files, you need to name it.
Try this instead:

// file1.cpp
#include<iostream>
using std::cout;
namespace MyNiftyNameSpace
{
void show();
void fun() {cout << "fun called\n";}
}

int main()
{
MyNiftyNameSpace::show();
return 0;
}


> file2.cpp
> #include<iostream>
> using namespace std;
> namespace{
> void fun();
> void show(){
> fun();
> cout<<"show called\n";
> }
> }


Once you've named a namespace, you can add to it in other
files, like this:

// file2.cpp
#include<iostream>
using std::cout;
namespace MyNiftyNameSpace
{
void fun();
void show(){
fun();
cout<<"show called\n";
}
}

> While trying to run this the linker is giving following message
> Undefined first referenced
> symbol in file
> (anonymous namespace)::fun() file2.o
> (anonymous namespace)::show() file1.o
> ld: fatal: Symbol referencing errors. No output written to a.out
> collect2: ld returned 1 exit status
>
>
> As far as i think, it should be able to find the definitions because
> everything here belongs to a single "un-named" namespace.
> Then why am i getting the problem?


You were using MULTIPLE un-named namespaces (one per file).
Interection equals null set. (Ie, the namespaces are disjoint.)

But if you name a namespace, you can add stuff to in in many files,
and the linker will hunt-down the missing pieces and link them
together for you.


Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


 
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
friend and unnamed namespace Ivan Mascovich C++ 9 03-03-2006 01:53 PM
unnamed namespace collision Marco Jez C++ 1 03-28-2005 12:58 PM
unnamed namespace and friend marco_segurini C++ 1 02-03-2005 02:48 PM
using a class in an unnamed namespace as friend marco_segurini C++ 4 06-16-2004 03:10 PM
C++ programmers! How do you use unnamed 'namespace's ? Razmig K C++ 3 09-05-2003 06:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57