Namespace and Linkage

Namespace and Linkage

Post by Joe Laughl » Thu, 24 May 2001 12:31:33



My professor thinks stuff inside namespaces (both named and unnamed)
is internal.  I say it's external (both named and unnamed).  I say so
because of 3.5/4 and 7.3.1.1, and also footnote 82.

But...

This won't compile.

// file_1.cpp
int main
{
  Joe::k = 3;

Quote:} // end file_1.cpp

// file_2.cpp
namespace Joe { int k } // end file_2.cpp

I'm compiling this with GCC 2.95.2 with 'g++ file_1.cpp file_2.cpp'
I get this error:


file_1.cpp: In function `int main()':
file_1.cpp:6: `Joe' undeclared (first use this function)
file_1.cpp:6: (Each undeclared identifier is reported only once
file_1.cpp:6: for each function it appears in.)
file_1.cpp:6: parse error before `::'
file_1.cpp:7: `k' undeclared (first use this function)

Isn't the namespace Joe supposed to have external linkage, and thus
shouldn't k be seen in main?   (by the way, I've also played around
with 'using namespace Joe' and 'using Joe::k3' and the same results
occur.)

Puzzled,
Joe

 
 
 

Namespace and Linkage

Post by Mike Wahle » Thu, 24 May 2001 13:52:29



Quote:>My professor thinks stuff inside namespaces (both named and unnamed)
>is internal.  I say it's external (both named and unnamed).  I say so
>because of 3.5/4 and 7.3.1.1, and also footnote 82.

>But...

>This won't compile.

>// file_1.cpp
>int main

Missing argument list

int main()

Quote:>{
>  Joe::k = 3;

At this point, the compiler has no knowledge of
namespace 'Joe'.

>} // end file_1.cpp

>// file_2.cpp
>namespace Joe { int k } // end file_2.cpp

>I'm compiling this with GCC 2.95.2 with 'g++ file_1.cpp file_2.cpp'
>I get this error:


>file_1.cpp: In function `int main()':
>file_1.cpp:6: `Joe' undeclared (first use this function)

Right.

Quote:>file_1.cpp:6: (Each undeclared identifier is reported only once
>file_1.cpp:6: for each function it appears in.)
>file_1.cpp:6: parse error before `::'

Right.

Quote:>file_1.cpp:7: `k' undeclared (first use this function)

Right.


>Isn't the namespace Joe supposed to have external linkage,

Yes, but you must tell the compiler it exists.

Quote:>and thus
>shouldn't k be seen in main?

Not if 'Joe' isn't seen, since that's where
'k' resides.

Quote:>   (by the way, I've also played around
>with 'using namespace Joe' and 'using Joe::k3' and the same results
>occur.)

Again, because you've never told the compiler
about namespace 'Joe'.

// file_1.cpp
#include file2.cpp
int main()
{
  Joe::k = 3;

Quote:} // end file_1.cpp

// file_2.cpp
namespace Joe { int k } // end file_2.cpp

-Mike

 
 
 

Namespace and Linkage

Post by Phli » Thu, 24 May 2001 15:05:48


Proclaimed Joe Laughlin from the mountaintops:

Quote:> This won't compile.

> // file_1.cpp
> int main
> {
>   Joe::k = 3;
> } // end file_1.cpp

> // file_2.cpp
> namespace Joe { int k } // end file_2.cpp

You might want to consider a .h file containing...

        namespace Joe { extern int k }

Now files who include that .h file will see Joe and his innards.

(You could also dupe the text by hand instead of using a .h file, but
that's just totally bad style.)

About anonymous namespaces, these technically use external linkage, but
anonymity has the same effect as a namespace named something you can't ever
type, so you still can't get to things named inside foreign anonymous
namespaces.

Quote:> Isn't the namespace Joe supposed to have external linkage, and thus
> shouldn't k be seen in main?

Some languages, like Java or Python, both make identifiers visible and link
to them with one 'import' keyword. The C languages assemble this ability
out of more primitive elements. You should #include that which you want to
import, because identifiers need visibility before they can acheive linkage.

--

============== http://phlip.webjump.com ==============
  --  Proud victim of the dreaded boomerang effect  --

 
 
 

Namespace and Linkage

Post by brian whit » Thu, 24 May 2001 19:23:16


The errors you meet are not the linkage error, they are
compile error,say you have to give the declare of the varible
before you using it .

so the file_1.cpp :
// begin
#include "file_2.h"
int main
{
   Joe::k = 3 ;

Quote:}

// end

and the file_2.h
// begin
namespace Joe
{
    extern int k ;

Quote:}

// end


>My professor thinks stuff inside namespaces (both named and unnamed)
>is internal.  I say it's external (both named and unnamed).  I say so
>because of 3.5/4 and 7.3.1.1, and also footnote 82.

>But...

>This won't compile.

>// file_1.cpp
>int main
>{
>  Joe::k = 3;
>} // end file_1.cpp

>// file_2.cpp
>namespace Joe { int k } // end file_2.cpp

>I'm compiling this with GCC 2.95.2 with 'g++ file_1.cpp file_2.cpp'
>I get this error:


>file_1.cpp: In function `int main()':
>file_1.cpp:6: `Joe' undeclared (first use this function)
>file_1.cpp:6: (Each undeclared identifier is reported only once
>file_1.cpp:6: for each function it appears in.)
>file_1.cpp:6: parse error before `::'
>file_1.cpp:7: `k' undeclared (first use this function)

>Isn't the namespace Joe supposed to have external linkage, and thus
>shouldn't k be seen in main?   (by the way, I've also played around
>with 'using namespace Joe' and 'using Joe::k3' and the same results
>occur.)

>Puzzled,
>Joe

 
 
 

1. Anonymous namespaces and static linkage

I was wondering what the difference between the following
C++ constructs was.

namespace {
  bool my_function()
  {
    return true;
  }

and

static bool my_function()
{
  return true;

Do both have the same linkage? Can the address of either
function be passed back as a return value by another
function (with external linkage) within the same translation
unit ?


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. ?NetUserSetLocalGroups

3. namespaces with static linkage?

4. cant upgrade firmware on my plextor 12x10x32x

5. What is the difference between internal linkage and external linkage?

6. Whoopsie...

7. Help: Compiling .cpp including pcap--C++ linkage conflicts with new declaration with C linkage

8. Alpha Pricing was Re: Alpha is not much faster then Intel Pentium II

9. Shell Namespace : How to create a namespace like "My Computer"

10. Root shell namespace and Nonroot namespace.

11. Namespaces: IE4's Address Bar blank for my namespace.

12. Enums in namespaces v/s classes in namespaces.

13. Global namespace vs. base class namespace