473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function template methods and DLL exports

I have a class declared as ff:

class __declspec(dlle xport) 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_str ing<_Elem,_Trai ts,_Ax>'
needs to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char> ,
_Ax=std::alloca tor<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(dlle xport) - 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 4498
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(dlle xport) 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_str ing<_Elem,_Trai ts,_Ax>' needs
to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char> ,
_Ax=std::alloca tor<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(dlle xport) - 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_str ing<_Elem,_Trai ts,_Ax>'
needs to have dll-interface to be used by clients of class 'A'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char> ,
_Ax=std::alloca tor<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_VE R).

#ifndef DllExportH
#define DllExportH

#if defined(_MSC_VE R)

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

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

#define EXPORT_STL_VECT OR_CONST_ITERAT OR(declspec_, T_) \
class_it_base declspec_ std::_Iterator_ base; \
template class declspec_ std::_Vector_co nst_iterator<T_ ,\
std::allocator< T_ > >;

#define EXPORT_STL_DEQU E(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_trai ts<T_, std::less<T_ >, std::allocator< T_ >, false> \::_Node>; \ template class declspec_ std::allocator< std::_Tree_ptr< \
std::_Tset_trai ts<T_, std::less<T_ >, std::allocator< T_ >, false> \::_Node*>; \ template class declspec_ std::set<T_ >;

#define EXPORT_STL_MULT ISET(declspec_, T_) \
template class declspec_ std::allocator< T_ >; \
template struct declspec_ std::less<T_ >; \
template class declspec_ std::allocator< std::_Tree_nod< \
std::_Tset_trai ts<T_, std::less<T_ >, std::allocator< T_ >, true> \::_Node>; \ template class declspec_ std::allocator< std::_Tree_ptr< \
std::_Tset_trai ts<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_trai ts<K_, V_, std::less<K_ >, \
std::allocator< std::pair<const K_, V_ > >, false> >::_Node>; \
template class declspec_ std::allocator< std::_Tree_nod< \
std::_Tmap_trai ts<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_iterat or;

#define EXPORT_STL_MULT IMAP(declspec_, K_, V_) \
template struct declspec_ std::less<K_ >; \
template class declspec_ std::allocator< std::_Tree_nod< \
std::_Tmap_trai ts<K_, V_, std::less<K_ >, \
std::allocator< std::pair<const K_, V_ > >, true> >::_Node>; \
template class declspec_ std::allocator< std::_Tree_nod< \
std::_Tmap_trai ts<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_VECT OR(declspec_, T_)
#define EXPORT_STL_VECT OR_CONST_ITERAT OR(declspec_, T_)
#define EXPORT_STL_DEQU E(declspec_, T_)
#define EXPORT_STL_LIST (declspec_, T_)
#define EXPORT_STL_SET( declspec_, T_)
#define EXPORT_STL_MULT ISET(declspec_, T_)
#define EXPORT_STL_MAP( declspec_, K_, V_)
#define EXPORT_STL_MAP_ CONST_ITERATOR( declspec_, K_, V_)
#define EXPORT_STL_MULT IMAP(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
6453
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. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a...
13
4104
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 sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain...
7
354
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 b){ return (a + b); } it can only deal with int + int or double + double however, I also want int + double, double + int so I come up with...
7
2102
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor...
8
1523
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*/ template <typename Tstruct Image {...}; template <typename Tstruct GrayImage : public Image<T{...};
1
1636
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 specialize it for U = unsigned char. So I wrote
2
4098
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 typedef'd to take void* parameters, through which the DLL's function passes its void* parameter to the callback function. This allows me to use class...
9
2583
by: Rob | last post by:
I want to do something like this: template <typename MyType> std::vector<MYTypeget(SomeClass c) { ...elided... } int main()
2
2642
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 not compile LONG __cdecl FilterListProc(LPVOID Item1,LPVOID Item2,LPVOID pParam = NULL); template<class TItemList,class TItemData> class...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.