Click here to Skip to main content
16,004,927 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Windows Media Player Pin
Harshabhi20-Apr-06 7:09
Harshabhi20-Apr-06 7:09 
Questionproblem exporting static template data members, but not methods Pin
Marcello17-Apr-06 14:40
Marcello17-Apr-06 14:40 
AnswerRe: problem exporting static template data members, but not methods Pin
Maxwell Chen17-Apr-06 17:14
Maxwell Chen17-Apr-06 17:14 
GeneralRe: problem exporting static template data members, but not methods Pin
Stephen Hewitt17-Apr-06 19:56
Stephen Hewitt17-Apr-06 19:56 
GeneralRe: problem exporting static template data members, but not methods Pin
Maxwell Chen17-Apr-06 20:08
Maxwell Chen17-Apr-06 20:08 
AnswerRe: problem exporting static template data members, but not methods Pin
Branislav17-Apr-06 23:32
Branislav17-Apr-06 23:32 
AnswerRe: problem exporting static template data members, but not methods Pin
Marcello18-Apr-06 9:53
Marcello18-Apr-06 9:53 
GeneralRe: problem exporting static template data members, but not methods Pin
Branislav18-Apr-06 18:53
Branislav18-Apr-06 18:53 
"Windows requires all public symbols to be explicitly exported from a DLL via the __declspec(dllexport) syntax (and correspondingly dllimported when referenced in some other DLL).
I guess it works pretty well on C libraries. But it doesn't work so well on C++ libraries, particularly with template classes--the template itself, of course, doesn't have a link-time presence, so it doesn't make sense to dllexport a template. What you really mean to export is the template instantatiation. But template instantiations are implicit, according to the language, so where do you put the dllexport syntax?
Microsoft came up with a place to put the dllexport syntax, which is to explicitly put a line like this in the DLL that exports a particular template instance:
Code:
template class __declspec(dllexport) my_template_class<my_instance>;
And also put a line like this in a DLL that imports that template instance:
Code:
extern template class __declspec(dllimport) my_template_class<my_instance>;
These lines must be the first instantiation of the template instance, which means they must be seen by the compiler before any other reference to my_template_class<my_instance>.
Of course, this is a Microsoft extension to C++ syntax; the language itself does not require this sort of declaration (and non-Microsoft compilers won't understand it).
The problem with this declaration is that it explicitly expands and exports all methods and components of the template class, whether they are actually used or not; and it requires that nested template classes be explicitly exported before their containing classes are exported.
That's not too bad, but it turns out that the standard STL implementation of list, map, set, multiset, multimap--basically everything other than vector--use nested template classes that are mutually recursive. That is, the nested class includes references to the containing class, and vice-versa. This makes it impossible to export the nested classes first, since expanding them requires expanding the containing class--which has not been exported yet, violating the rule that the export syntax must appear before the first instance is encountered. Similarly, you can't export the containing class first, which would expand the nested class before its export syntax. This means that none of the STL classes except for vector can be exported from a DLL.
But do you really need to export a template instance from a DLL? Isn't each instance of a template technically a completely new copy of the class? If you don't even try to export the class, you should end up with a new copy of the template code in each DLL, but since all of the copies are essentially the same, they should be compatible with each other. So what happens if you don't try to export the class?
It turns out this works, mostly, but the standard implementation of STL relies on some static members of the template class, which the language specifies that the linker should resolve to the same pointer value by runtime. The Microsoft DLL linker doesn't do this (because, without having explicitly exported the template classes, the linker sees them as unrelated classes). The net result is that any class that tries to expose iterators to its internal STL object, even via an inline method, will crash if those iterators are accessed by code in another DLL.
We spent a long time trying to figure out why our code, which worked fine on Unix, was crashing when we ported it to Windows. We finally tracked it down to this, and the only solution we could find--short of making everything non-inline and paying the corresponding run-time performance penalty--was to compile everything into one big DLL. Now, within the one DLL, all the iterators are valid and can be accessed by code in different modules.
This was back on VC6. Beginning with VC7.0, Microsoft started shipping with a third-party reimplementation of STL, that supposedly works around the DLL linker issue by not requiring static members to be unified. So presumably the issue is now a moot point on VC7.0 and above--you should be able to use STL iterators across DLL's now, although you still pay the cost of code bloat--although we haven't tested this."
/////////////////////////////////////////////////////////////////
// header file DLL
#ifdef MYDLL_EXPORTS<br />
#define MYDLL_API __declspec(dllexport)<br />
#else<br />
#define MYDLL_API __declspec(dllimport)<br />
#endif<br />
<br />
class MYDLL_API CMyDLL {<br />
public:<br />
    CMyDLL(void);<br />
    // TODO: add your methods here.<br />
    static void SetN(int x);<br />
    static int GetN();<br />
    static void show();<br />
    static int n;<br />
};<br />
MYDLL_API int CMyDLL::n; // NEED TO SOLVE LINKER ERROR                          : error LNK2001: unresolved external symbol "public: static int CMyDLL::n" <br />

// CPP file DLL
CMyDLL::CMyDLL() { return; }<br />
<br />
void CMyDLL::show() {<br />
		std::cout << "CMyDLL:show()" << std::endl;<br />
		std::cout << n << std::endl;<br />
}<br />
<br />
void CMyDLL::SetN(int x) { n = x; }<br />
<br />
int CMyDLL::GetN() { return n; }


// main file
#include "stdafx.h"<br />
#include <iostream><br />
#include "myDLL.h"<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
	CMyDLL::n = 2;<br />
	CMyDLL::SetN(9);<br />
	int k1 = CMyDLL::n;<br />
	std::cout << k1 << std::endl;<br />
	CMyDLL::show(); // Okay<br />
	return 0;<br />
}<br />

Result: // ???
2 // direct call n
CMyDLL:show()
9 // calling via function
GeneralRe: problem exporting static template data members, but not methods Pin
Marcello21-Apr-06 11:32
Marcello21-Apr-06 11:32 
QuestionLow level operations to devices Pin
fvalerin17-Apr-06 14:02
fvalerin17-Apr-06 14:02 
AnswerRe: Low level operations to devices Pin
David Crow18-Apr-06 3:06
David Crow18-Apr-06 3:06 
GeneralRe: Low level operations to devices Pin
fvalerin18-Apr-06 4:44
fvalerin18-Apr-06 4:44 
GeneralRe: Low level operations to devices Pin
David Crow18-Apr-06 4:52
David Crow18-Apr-06 4:52 
GeneralRe: Low level operations to devices Pin
Marcello18-Apr-06 10:39
Marcello18-Apr-06 10:39 
GeneralRe: Low level operations to devices Pin
David Crow18-Apr-06 10:44
David Crow18-Apr-06 10:44 
GeneralRe: Low level operations to devices Pin
Marcello18-Apr-06 11:15
Marcello18-Apr-06 11:15 
Questionhow to change the color of title bar and its text , in dialog application Pin
Hannan Azam17-Apr-06 13:49
Hannan Azam17-Apr-06 13:49 
AnswerRe: how to change the color of title bar and its text , in dialog application Pin
includeh1017-Apr-06 16:21
includeh1017-Apr-06 16:21 
AnswerRe: how to change the color of title bar and its text , in dialog application Pin
Hamid_RT17-Apr-06 19:12
Hamid_RT17-Apr-06 19:12 
Questionweird error... please help Pin
picazo17-Apr-06 13:43
picazo17-Apr-06 13:43 
AnswerRe: weird error... please help Pin
Stephen Hewitt17-Apr-06 14:44
Stephen Hewitt17-Apr-06 14:44 
GeneralRe: weird error... please help Pin
picazo17-Apr-06 15:10
picazo17-Apr-06 15:10 
GeneralRe: weird error... please help Pin
Stephen Hewitt17-Apr-06 15:14
Stephen Hewitt17-Apr-06 15:14 
GeneralRe: weird error... please help Pin
David Crow18-Apr-06 3:10
David Crow18-Apr-06 3:10 
GeneralRe: weird error... please help - SOLVED Pin
picazo18-Apr-06 6:31
picazo18-Apr-06 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.