SWIG, C++ and static member variables


(Paul Watkins) #1

If Person.h is being included in multiple compilation units then the static will be defined multiple times. You need to pick a .cpp file and defines it once there.

···

-----Original Message-----
From: ptkwt@shell1.aracnet.com [mailto:ptkwt@shell1.aracnet.com]
Sent: Tuesday, 11 June 2002 15:19
To: ruby-talk@ruby-lang.org
Subject: SWIG, C++ and static member variables

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