X-Spam-Check-By: sourceware.org Message-ID: <89c3ea2b0704150746j2ac95003y6478e30f103dc74c@mail.gmail.com> Date: Sun, 15 Apr 2007 10:46:39 -0400 From: "andy wang" To: cygwin AT cygwin DOT com Subject: Behavior of static variable in Template in cygwin dynamic library (dll) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Hi, All, Recently I met the problem that I have seen the similar problem reported before in this forum " cygwin, g++, templates, and DLLs" http://cygwin.com/ml/cygwin/2006-04/msg00480.html (1) you have a DLL whose source code includes a template class. (2) that template class has a static member variable (3) the DLL and the DLL's client both use a specific instantiation of that template. But the DLL has one copy of the static member var, and the app has a different copy. I have tested the same case in FC4 with g++ version is 4.0.2 and it's fine. So my question is the problem is related to GCC (which the current version I am using in cygwin is 3.4.4) or it is the limitation of cygwin? To solve this problem, the workaround method is when you compile the DLL's client problem you just comment out the "static member variable" initialization then the DLL's client will work fine. ================= Test case ============ ==> pool.h #include class dummy2 { }; template class pool { public: void static init(); void static alloc(); private: static dummy2* buf; }; template dummy2* pool::buf = 0; class dummy{ }; template void pool::init() { if(buf == NULL) { buf = new dummy2[256]; printf("buf = %x, addr=%x, type=%d\n",buf, &buf, sizeof(T)); } } template void pool::alloc() { printf("buf = %x, addr=%x, type=%d\n",buf, &buf, sizeof(T)); } ==> pooluser.h class pooluser { public: void static init(void); }; ==> pooluser.cpp #include "pool.h" #include "pooluser.h" void pooluser::init(void) { pool::init(); } ==> test.cpp #include "pool.h" #include "pooluser.h" int main(int, char**) { pooluser::init(); pool::alloc(); return 0; } ==> Makefile.test all: g++ -shared -o libpooluser.dll pooluser.cpp g++ -o test test.cpp -L. -lpooluser linux: g++ -shared -o libpooluser.so pooluser.cpp g++ -o test test.cpp -L. -lpooluser export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. ======================= test result ================ cygwin on Win, gcc 3.4.4: $ ./test.exe buf = 100101f0, addr=10005030, type=1 buf = 0, addr=402000, type=1 linux on FC4, gcc 4.0.2 [root AT localhost ypwang]# ./test buf = 8c7a008, addr=8049820, type=1 buf = 8c7a008, addr=8049820, type=1 -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/