Dear all,

I am not sure whether the problem I am going to describe is a problem with
gcc or with the dynamic linker on Mac OS X, but maybe someone here knows a
way to deal with it or could suggest a more appropriate mailing list. I
use gcc 3.3 on Darwin 7.9.0. On this platform the following happens.

// file test.h

#include <iostream>
#include <vector>

template <class T> class test
{   public:
        test()
        {   std::cout << "the address of v is " << (int)&v << std::endl;
        }
        void method() {}
    private:
        static std::vector<T> v;
};

template <class T> std::vector<T> test<T>::v;

extern test<int> t;

// file test.C

#include "test.h"

test<int> t;

// file main.C

#include "test.h"

int main()
{       t.method();
        test<int> t2;
        return 0;
}

// compiling:

$ g++    -c -o test.o test.C
$ g++    -c -o main.o main.C
$ g++ -dynamiclib -o test.dylib test.o
$ g++ -o main main.o -L. -ltest
$ ./main
the address of v is 4563560
the address of v is 343368
$ g++ -o main main.o test.o
$ ./main
the address of v is 343376
the address of v is 343376

The first result when using the dynamic library seems wrong. Only one
instance of the static member should have been created. Any ideas how to
circumvent this?

Best wishes,
Chris

Reply via email to