OK, my terminology may not be correct in C++ terms (I’ve been doing a lot
of Ruby and not a lot of C++ until the last few weeks), but, say I’ve got
a C++ class with a class variable (statically scoped), like:
//Person.h
class Person {
static unsigned numPeople;
const char* name;
unsigned age;
public:
Person(const char* nm, unsigned age=0);
Person(const char* nm, int age=0);
static unsigned numberOfPeople() { return numPeople; }
void identify() const;
};
unsigned Person::numPeople = 0;
//end of Person.h
I did:
swig -c++ -ruby Person.i
g++ -c Person.cpp
g++ -c Person.cc
g++ -c Person_wrap.cxx -I /usr/lib/ruby/1.6/i686-linux-gnu
<all ok, till here:>
g++ -shared Person.o Person_wrap.o -o Person.so
Trying to compile that shared lib gave me:
Person_wrap.o(.data+0x20): multiple definition of `Person::numPeople’
Person.o(.data+0x0): first defined here
collect2: ld returned 1 exit status
So SWIG interpretted numPeople as a static member function instead of as a
static variable (class variable).
Anyway to get around this?
Phil
Phil Tomson wrote:
OK, my terminology may not be correct in C++ terms (I’ve been doing a lot
of Ruby and not a lot of C++ until the last few weeks), but, say I’ve got
a C++ class with a class variable (statically scoped), like:
//Person.h
class Person {
static unsigned numPeople;
const char* name;
unsigned age;
public:
Person(const char* nm, unsigned age=0);
Person(const char* nm, int age=0);
static unsigned numberOfPeople() { return numPeople; }
void identify() const;
};
unsigned Person::numPeople = 0;
//end of Person.h
I did:
swig -c++ -ruby Person.i
g++ -c Person.cpp
g++ -c Person.cc
g++ -c Person_wrap.cxx -I /usr/lib/ruby/1.6/i686-linux-gnu
<all ok, till here:>
g++ -shared Person.o Person_wrap.o -o Person.so
Trying to compile that shared lib gave me:
Person_wrap.o(.data+0x20): multiple definition of `Person::numPeople’
Person.o(.data+0x0): first defined here
collect2: ld returned 1 exit status
So SWIG interpretted numPeople as a static member function instead of as a
static variable (class variable).
Ummm, no. The problem is that (as the error message states) you have
multiple definitions of Person::numPeople, because it’s declared in the
Person.h header file and that file gets included in more than one C++
source file.
You should remove the definition of Person::numPeople, i.e. the line:
unsigned Person::numPeople = 0;
out into the implementation file (I guess Person.cpp?) and this should
fix things.
I’m also a little concerned that your trace of commands shows you
compiling both a “Person.cpp” and a “Person.cc” (in addition to the
SWIG-generated “Person_wrap.cxx”). Compiling Person.cpp would result in
a Person.o object file, and following that with a compile of Person.cc
would do the same, overwriting the previous Person.o file.