Hello, all ...
This has become an issue having to do with a package i'm involved in
porting to OSX 10.2 / Darwin 6.0. The Apple gcc3.1 compiler seems to
have a problem with static local symbols as part of template classes.
Attached is a sample (courtesy of Martin Johnson).
To illustrate the problem, be sure to use the gcc3.1 compiler that came
with Jaguar:
g++ main.cpp a.cpp b.cpp test_static.cpp -o test_static
On my tangerine iBook, I get the following:
[Pugsley:~/test_gcc31_statics] jmzorko% g++ a.cpp b.cpp main.cpp
test_static.cpp -o test_static
ld: multiple definitions of symbol
_ZZN11test_staticIiE18method_with_staticEvE12local_static
/var/tmp//cc7xbS1a.o definition of
_ZZN11test_staticIiE18method_with_staticEvE12local_static in section
(__DATA,__data)
/var/tmp//ccPyahMN.o definition of
_ZZN11test_staticIiE18method_with_staticEvE12local_static in section
(__DATA,__data)
[Pugsley:~/test_gcc31_statics] jmzorko%
Compiling the same code under gcc3.1.1 on Red Hat Linux doesn't give
the error. However, commenting out the static local in
test_static::method_with_static(), and instead using a static data
member in test_static.h, resolves the problem on Darwin, but this seems
like not such a great solution, for scoping reasons (I now have to be
extra careful, something that is only meant to be local is actually
scoped far larger).
Is this a bug in Apple's gcc3.1, or Apple's ld, or something else? I
don't know if everything that builds on Linux and doesn't on Darwin is
really a 'bug' but this seems to be.
Regards,
John
#include "a.h"
#include "test_static.h"
void a::use_template_class()
{
xxxx x;
x.method_with_static();
}
#ifndef _a_
#define _a_
class a
{
public:
a() {;}
virtual ~a() {;}
void use_template_class();
};
#endif
#include "b.h"
#include "test_static.h"
void b::use_template_class()
{
xxxx x;
x.method_with_static();
}
#ifndef _b_
#define _b_
class b
{
public:
b() {;}
virtual ~b() {;}
void use_template_class();
};
#endif
#include <iostream>
int main (int argc, const char * argv[])
{
return 0;
}
#ifndef _test_static_c
#define _test_static_c
#include "test_static.h"
template <class T> void
test_static<T>::method_with_static(void)
{
// The troble is here.
static int local_static = 0;
}
template <class T> T&
test_static<T>::instance()
{
return test_static<T>::instance_;
}
#endif
#ifndef _test_static_h
#define _test_static_h
template <class T>
class test_static
{
private:
static T instance_;
//static int local_static;
public:
test_static(void){;}
virtual ~test_static(){;}
void method_with_static();
static T& instance();
};
#include "test_static.cpp"
typedef test_static<int> xxxx;
#endif _test_static_
Falling You - exploring the beauty of voice and sound
New EP, "hope thrown down," available now at
http://www.mp3.com/fallingyou