473,381 Members | 1,467 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,381 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 4465
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.