Re: Can I use a global in one lib that's defined in another?
Re: Can I use a global in one lib that's defined in another?
- Subject: Re: Can I use a global in one lib that's defined in another?
- From: Andreas Grosam <email@hidden>
- Date: Thu, 8 Sep 2005 17:40:46 +0200
On 22.08.2005, at 23:42, Ando Sonenblick wrote:
Just discover this *old* mail unanswered,
<x-tad-bigger>Gang,</x-tad-bigger>
<x-tad-bigger> </x-tad-bigger>
<x-tad-bigger>I have a bunch of static libs that I’m porting from CW to Xcode.</x-tad-bigger>
<x-tad-bigger> </x-tad-bigger>
<x-tad-bigger>I’m getting link errors and have discovered that Lib A uses a global variable that is defined in Lib B. (Lib A simply externs it).</x-tad-bigger>
<x-tad-bigger> </x-tad-bigger>
<x-tad-bigger>Will this work now that I’m building my static libs in Xcode? Evidently it doesn’t (or I need to set one of the very few and very intuitive compiler/linker settings)…. </x-tad-bigger><x-tad-bigger>J</x-tad-bigger><x-tad-bigger>
</x-tad-bigger>
This should work. names (e.g variables, functions) having external linkage should be referencable from any other module.
AFAIK, the *visibility of symbols* shouldn't be a problem in static libraries as opposed to dynamic shared libraries.
Note, however that there are differences in C and C++, and that you need to take care if you mix C and C++:
names declared/defined in a C module:
A variable gets external linkage if you specify it with "extern".
extern int gGlobal;
Just be aware that you specify the "correct" linkage. If you define the global variable in a C module, ensure firstly that the module is actually compiled as a C module, and secondly you should explicitly define "C linkage" in the header file, if the names will be referenced in C and C++ sources:
#if defined __cplusplus
extern "C" {
#endif
int gGlobal;
int foo();
#if defined __cplusplus
}
#endif
In C++ it is slightly different, in that a non static variable has per default extern, and a constant has per default intern linkage.
names defined/declared in C++ module:
Examples of extern linkage:
int gGlobal; // non-static, non const -> thus gets external C++ linkage.
extern int kConstant; // explicit extern const -> gets external C++ linkage
Examples of intern linkage:
static int sIntern;
const int kKonstant = 5;
If you still have link errors, I need more info.
Regards
Andreas
<x-tad-bigger> </x-tad-bigger><x-tad-bigger>
</x-tad-bigger><x-tad-bigger>Thx,</x-tad-bigger>
<x-tad-bigger>Ando</x-tad-bigger>
<x-tad-bigger> </x-tad-bigger>
_______________________________________________
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