unnamed namespace problem

Discussion in 'C++' started by Sandy, Sep 24, 2005.

  1. Sandy

    Sandy Guest

    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?
     
    Sandy, Sep 24, 2005
    #1
    1. Advertising

  2. Sandy

    Greg Guest

    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
     
    Greg, Sep 24, 2005
    #2
    1. Advertising

  3. Sandy

    Ron Natalie Guest

    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).
     
    Ron Natalie, Sep 24, 2005
    #3
  4. "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
     
    Robbie Hatley, Sep 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razmig K
    Replies:
    3
    Views:
    717
    John L Fjellstad
    Sep 5, 2003
  2. marco_segurini
    Replies:
    4
    Views:
    1,028
    Rob Williscroft
    Jun 16, 2004
  3. marco_segurini

    unnamed namespace and friend

    marco_segurini, Feb 3, 2005, in forum: C++
    Replies:
    1
    Views:
    367
    msalters
    Feb 3, 2005
  4. Marco Jez

    unnamed namespace collision

    Marco Jez, Mar 28, 2005, in forum: C++
    Replies:
    1
    Views:
    463
    Peter MacMillan
    Mar 28, 2005
  5. Ivan Mascovich

    friend and unnamed namespace

    Ivan Mascovich, Mar 2, 2006, in forum: C++
    Replies:
    9
    Views:
    539
    Victor Bazarov
    Mar 3, 2006
Loading...

Share This Page