c++ - Why does linker fail when accessing static member in inline constructor -
just started doing coding simple automated test framework internal use. (i know there hundreds of them out there - ones too, @ moment that's not interesting, don't point me of those, please ;)) came across following problem can't explain, asking help.
i have following code part of dll:
(the code barely embryo , took me <2 minutes write, it's logic, structure - nothing - refined, in way yet.)
h-file:
#pragma once #ifdef __dll__ // defined in dll-project #define dllexport __declspec( dllexport ) #else #define dllexport #endif class dllexport autotest { public: enum etestid {testid_somefunction}; autotest(etestid id, lpvoid lpdata) { if(sm_btestsactive) exectest(id, lpdata); } void activatetests(); private: static void exectest(etestid id, lpvoid lpdata) { } static bool sm_btestsactive; };
cpp-file:
#include "stdafx.h" #include "autotest.hpp" bool autotest::sm_btestsactive = false; void autotest::activatetests() { sm_btestsactive=true; }
this compiles fine , dll gets generated.
here's problem though - when instantiating class with:
autotest(autotest::testid_somefunction, &somedata);
from main application, linker fails with
error lnk2001: unresolved external symbol "private: static int autotest::sm_btestsactive" (?sm_btestsactive@autotest@@0ha)
<2 minutes write - going on 5 hours understand why fails!!! :o
here's interesting - if move constructor cpp-file (not inlined) works fine!?!?
here's code:
h-file:
#pragma once #ifdef __dll__ // defined in dll-project #define dllexport __declspec( dllexport ) #else #define dllexport #endif class dllexport autotest { public: enum etestid {fk3059}; autotest(etestid id, lpvoid lpdata); void activatetests(); private: static void exectest(etestid id, lpvoid lpdata) { } static bool sm_btestsactive; };
cpp-file:
#include "stdafx.h" #include "autotest.hpp" bool autotest::sm_btestsactive = false; autotest::autotest(etestid id, lpvoid lpdata) { if(sm_btestsactive) exectest(id, lpdata); } void autotest::activatetests() { sm_btestsactive=true; }
(i've made minor edits in code after pasting, there may or may not simple syntax errors.)
also, if remove reference static member inline versions constructor, works fine.
any ideas why inline version won't work?
should be:
#ifdef __dll__ // defined in dll-project #define dllexport __declspec( dllexport ) #else #define dllexport __declspec( dllimport ) #endif
you can declare c++ classes
dllimport
ordllexport
attribute. these forms imply entire class imported or exported. classes exported way called exportable classes.
more information in the documentation.
Comments
Post a Comment