473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

function template methods and DLL exports

I have a class declared as ff:

class __declspec(dllexport) A {
public:
A() ;
A(const A&)
A& operator=(const A&) ;
~A() ;

void doThis(void) ;
void doThat(void) ;
template <class T> T doTheOther( const &T, int ) ;

private:
std::string s ;
std::vector< <std::pair> > vp ;
......
};
When I compile the code, the compiler generates several warnings such as:

warning C4251: 'A::s' : class 'std::basic_string<_Elem,_Traits,_Ax>'
needs to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

etc. Why do I have to export private variables (is that what the warning
message is instructing me to do?)

Despite the warnings, the code builds and the dll is built. When I use
the dll in another project, problems (unsuprisingly) arise but they are
of a slightly different kind. The function template method doTheOther
does not seem to be exported and I am getting a link error when I try to
link to A.lib so I can create a project that uses classes exported by A.dll

I have two questions:

1). Why do I need to decorate the private variables with
__declspec(dllexport) - as per compiler warnings?

2). Why is the method function template not getting exported (how do I
export the method in the DLL) ?
Thanks
Nov 17 '05 #1
2 4457
The answer to this and your previous post can both be obtained from this KB
article : http://support.microsoft.com/kb/q168958/

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:db**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
I have a class declared as ff:

class __declspec(dllexport) A {
public:
A() ;
A(const A&)
A& operator=(const A&) ;
~A() ;

void doThis(void) ;
void doThat(void) ;
template <class T> T doTheOther( const &T, int ) ;

private:
std::string s ;
std::vector< <std::pair> > vp ;
......
};
When I compile the code, the compiler generates several warnings such as:

warning C4251: 'A::s' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs
to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

etc. Why do I have to export private variables (is that what the warning
message is instructing me to do?)

Despite the warnings, the code builds and the dll is built. When I use the
dll in another project, problems (unsuprisingly) arise but they are of a
slightly different kind. The function template method doTheOther does not
seem to be exported and I am getting a link error when I try to link to
A.lib so I can create a project that uses classes exported by A.dll

I have two questions:

1). Why do I need to decorate the private variables with
__declspec(dllexport) - as per compiler warnings?

2). Why is the method function template not getting exported (how do I
export the method in the DLL) ?
Thanks

Nov 17 '05 #2
Alfonso Morra wrote:
warning C4251: 'A::s' : class 'std::basic_string<_Elem,_Traits,_Ax>'
needs to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
You may also want to read
http://www.unknownroad.com/rtfm/Visu...ningC4251.html
Although I didn't have a linker error problem related to exporting STL
classes. ("Workaround 2 can cause link errors..." doesn't seem to happen
to me). Be cautious, though. Also, the macros on this site don't work at
all. They may be for a very old version of VC++.

As an experiment, I worked out a small set of macros for exporting STL
containers, if you're interested. It's been tested with VC++ 2005 Beta 2
so far, but should also work with VC++ 2003. You definitely need to
modify it for different STL implementations. It's very compiler / STL
specific, thus the #if defined(_MSC_VER).

#ifndef DllExportH
#define DllExportH

#if defined(_MSC_VER)

#ifdef _DEBUG
# define class_it_base class
#else
# define class_it_base struct
#endif

#define EXPORT_STL_VECTOR(declspec_, T_) \
template class declspec_ std::allocator<T_ >; \
template class declspec_ std::vector<T_ >;

#define EXPORT_STL_VECTOR_CONST_ITERATOR(declspec_, T_) \
class_it_base declspec_ std::_Iterator_base; \
template class declspec_ std::_Vector_const_iterator<T_,\
std::allocator<T_ > >;

#define EXPORT_STL_DEQUE(declspec_, T_) \
template class declspec_ std::allocator<T_ >; \
template class declspec_ std::allocator<T_* >; \
template class declspec_ std::deque<T_ >;

#define EXPORT_STL_LIST(declspec_, T_) \
template class declspec_ std::allocator<T_ >; \
template class declspec_ std::allocator<std::_List_nod<T_,\
std::allocator<T_ > >::_Node>; \
template class declspec_ std::allocator<std::_List_nod<T_,\
std::allocator<T_ > >::_Node*>; \
template class declspec_ std::list<T_ >;

#define EXPORT_STL_SET(declspec_, T_) \
template class declspec_ std::allocator<T_ >; \
template struct declspec_ std::less<T_ >; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tset_traits<T_, std::less<T_ >, std::allocator<T_ >, false> \::_Node>; \ template class declspec_ std::allocator<std::_Tree_ptr< \
std::_Tset_traits<T_, std::less<T_ >, std::allocator<T_ >, false> \::_Node*>; \ template class declspec_ std::set<T_ >;

#define EXPORT_STL_MULTISET(declspec_, T_) \
template class declspec_ std::allocator<T_ >; \
template struct declspec_ std::less<T_ >; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tset_traits<T_, std::less<T_ >, std::allocator<T_ >, true> \::_Node>; \ template class declspec_ std::allocator<std::_Tree_ptr< \
std::_Tset_traits<T_, std::less<T_ >, std::allocator<T_ >, true> \::_Node*>; \ template class declspec_ std::multiset<T_ >;

#define EXPORT_STL_MAP(declspec_, K_, V_) \
template struct declspec_ std::less<K_ >; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tmap_traits<K_, V_, std::less<K_ >, \
std::allocator<std::pair<const K_, V_ > >, false> >::_Node>; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tmap_traits<K_, V_, std::less<K_ >, \
std::allocator<std::pair<const K_, V_ > >, false> >::_Node*>; \
template class declspec_ std::allocator<std::pair<const K_, V_ > >; \
template class declspec_ std::map<K_, V_ >;

#define EXPORT_STL_MAP_CONST_ITERATOR(declspec_, K_, V_) \
class_it_base declspec_ std::_Iterator_base; \
template class declspec_ std::_Tree<std::_Tmap_traits<K_, V_, \
std::less<K_>, std::allocator<std::pair<const K_, V_ > >, false> \::const_iterator;

#define EXPORT_STL_MULTIMAP(declspec_, K_, V_) \
template struct declspec_ std::less<K_ >; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tmap_traits<K_, V_, std::less<K_ >, \
std::allocator<std::pair<const K_, V_ > >, true> >::_Node>; \
template class declspec_ std::allocator<std::_Tree_nod< \
std::_Tmap_traits<K_, V_, std::less<K_ >, \
std::allocator<std::pair<const K_, V_ > >, true> >::_Node*>; \
template class declspec_ std::allocator<std::pair<const K_, V_ > >; \
template class declspec_ std::multimap<K_, V_ >;

#else

#define EXPORT_STL_VECTOR(declspec_, T_)
#define EXPORT_STL_VECTOR_CONST_ITERATOR(declspec_, T_)
#define EXPORT_STL_DEQUE(declspec_, T_)
#define EXPORT_STL_LIST(declspec_, T_)
#define EXPORT_STL_SET(declspec_, T_)
#define EXPORT_STL_MULTISET(declspec_, T_)
#define EXPORT_STL_MAP(declspec_, K_, V_)
#define EXPORT_STL_MAP_CONST_ITERATOR(declspec_, K_, V_)
#define EXPORT_STL_MULTIMAP(declspec_, K_, V_)

#endif

#endif
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
7
by: hopangtsun | last post by:
Hi all, I have encountered a problem on using the function template my goal is to add two numbers, which they can be int or double if I do this in this way template<class T> T addition(T a, T...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
8
by: vectorizor | last post by:
Hi all, I have a slight problem with the usage of function templates. It shoud be really easy for you guys to explain me what's wrong. I have one base class and 2 derived classes: /*code*/...
1
by: vectorizor | last post by:
Hello, I've got a template function: template <typename U, typename T> void func(ColourImage<U&input,GrayImage<T&out_r, GrayImage<T> &out_g, GrayImage<T&out_b); and I would like to...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
9
by: Rob | last post by:
I want to do something like this: template <typename MyType> std::vector<MYTypeget(SomeClass c) { ...elided... } int main()
2
by: canderse | last post by:
I am beginning to use templates alot (in visual studio 2008) but I writing this simple template that has a method which takes a function pointer as an argument and i cant figure out why the will...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.