Re: just C++ in xcode
Re: just C++ in xcode
- Subject: Re: just C++ in xcode
- From: Andreas Grosam <email@hidden>
- Date: Fri, 30 Sep 2005 12:49:12 +0200
On 30.09.2005, at 07:58, Markian Hlynka wrote:
I knew, that this was a very nice example, and I also knew that the question from Joe would arise ;-)
Lawrence is right:
if you have a *definition* in a header, and when you include this header in several .cpp files, each (independent) TU will create its own entity. What you actually get in a program linking these TUs, is a multible defined symbol - since this symbol has *external linkage*.
You defined (created) an entitiy several times - having the same name, but want to refere to only one!
The compiler is right not to compain about this - the standard says:
"No diagnostic required".
But the linker is right to complain about this! (multible symbols defined)
To solve your problem (as Lawrence suggested):
As a rule of thumb, if you declare an extern variable in a header "foo.h", you should define it in a corresponding foo.cpp file. Once it is defined (only once!), then you can **use** it several times everywhere, just ensure you include the header foo.h, and finally link all modules together when you build the program.
// file foo1.h
#ifndef FOO1_H
#define FOO1_h
namespace n { extern int x; }
void foo();
#endif
// foo1.cpp
#include <foo1.h>
int n::x = 10;
void foo() { ... }
in main.cpp
#include <foo1.h>
#include ...
using namespace std;
using namespace n;
int main()
{
func();
cout << x << endl; // prints n::x "10" !!
x = 11; // modifies n::x
}
Lawrence is on the money as to the cause, I think. Sorry, I missed that!
But, having to declare x as extern in the headers is, I think, unnesessary; wouldn't that obviate the whole namespace concept?
According the C++ rules, it is **reqiured** because
1) the linkage is *extern*
2) AND it would otherwise be a *definition*, which in fact did also initialize the variable n::x with 0.
Then in a program you would get several entities each having the same name, but you want to refere to only one.
As opposed to external linkage, *internal* linkage:
//file.h
namespace N { const int k = 1; }
since k is a constant, its linkage is **intern**.
When you include this header in moduls each TU would instantiate its **own** entity. Notice, that the compiler will not complain about that there are more than one TUs each having an entity of the same name. And the linker would not complain, because the linkage is intern.
Intuitively, it appears as if there would only one entity of N::k in the program - and indeed, the linker may strip unused entities. Surprisingly, it works exactly as you might think it should ;-)
(frankly, sometimes it seems it would be better to think *intuitively*, than in an *exact* way)
I believe your namespace, as Lawrence says, should go in a .c file. I further believe that you shouldn't need a header file that has extern x.
Rules are rules. They have been made and are fixed. Other programming languages may have different rules.
In C++, if you would like to expose the name n::x, you need a header file.
Um... do preprocessor include flags solve this? (what do you call these things again?) ie
#ifndef MYFILE_H
#define MYFILE_H
Nop. This macro is sometimes called a "header guard". A header guard prevents that a header file will be included more than once in a **translation unit**. Several TUs compile independently, and each may include the same header regardless of a "header guard". Macros do not survive accross several TUs.
... contents of header file here
#endif
//end of header file
Andreas
On Sep 29, 2005, at 19:06, Lawrence Gold wrote:
On Sep 29, 2005, at 5:54 PM, Joe Kurtz wrote:
Okay everyone, we're gettin' plenty of mileage from
this. But there's just one thing I don't yet
understand. I was pretty simplistic in my example. My
code actually looks more like one of Andreas'
examples:
in foo1.h:
#include <iostream>
namespace n { int x; };
void func();
You need to declare the variable extern in the header file and then define it in ONE .c file. Remember that #include literally pastes the text of the header file into the file including it, so every file that includes foo1.h is defining a separate x.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
----
Quantum mechanics -- the dreams that stuff is made of.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden