Exporting methods from a dll that uses namespaces

2

2b|!2b==?

I have a C++ library partitioned by namespaces. I want to export some of
the functions in one of the namespace - how do I do it.

Assuming I have the macro MYAPI_EXPORTS #defined to be a declspec import
or export (as appropriate ...

This is what the lib looks like:

MyNamespace
{
//To be exposed
void Foo();
const std::vector<std::string>& Foobar(const std::string);
....

//Not to be exposed
int MyPrivateBit();
void YetAnotherPrivateBit();
....
};

where do I insert my macro MYAPI_EXPORT above, so I can export the
functions I want from the DLL ?
 
D

Doug Harrison [MVP]

I have a C++ library partitioned by namespaces. I want to export some of
the functions in one of the namespace - how do I do it.

Assuming I have the macro MYAPI_EXPORTS #defined to be a declspec import
or export (as appropriate ...

This is what the lib looks like:

MyNamespace
{
//To be exposed
void Foo();
const std::vector<std::string>& Foobar(const std::string);
....

//Not to be exposed
int MyPrivateBit();
void YetAnotherPrivateBit();
....
};

where do I insert my macro MYAPI_EXPORT above, so I can export the
functions I want from the DLL ?

Unlike classes, you can't export namespaces, but you don't want to do that
anyway. You just need to put your macro at the start of the declarations
you do want to export, e.g.

MyNamespace
{
//To be exposed
MYAPI_EXPORT void Foo();
MYAPI_EXPORT const std::vector<std::string>& Foobar(const
std::string);
....

//Not to be exposed
int MyPrivateBit();
void YetAnotherPrivateBit();
....
};

Note that if you don't explicitly instantiate and export vector<string>,
the DLL client will instantiate it itself. (This may also apply to string,
except I think the DLL version of the C++ RTL exports it.) Also, Foobar
should probably take a const string& instead of a const string.
 
2

2b|!2b==?

Doug said:
Unlike classes, you can't export namespaces, but you don't want to do that
anyway. You just need to put your macro at the start of the declarations
you do want to export, e.g.

MyNamespace
{
//To be exposed
MYAPI_EXPORT void Foo();
MYAPI_EXPORT const std::vector<std::string>& Foobar(const
std::string);
....

//Not to be exposed
int MyPrivateBit();
void YetAnotherPrivateBit();
....
};

Note that if you don't explicitly instantiate and export vector<string>,
the DLL client will instantiate it itself. (This may also apply to string,
except I think the DLL version of the C++ RTL exports it.) Also, Foobar
should probably take a const string& instead of a const string.

Doug, I already sorted this out - it was actually being caused by a
'visibility' or 'scope' problem due to where the declspec macro was defined.

Your statements however, intrigue me... How would I explicitly
instantiate and export vector<string>? care to give an example ?

Also, what is the side effect (i.e. gotcha) if the DLL client
instantiates the exported data type itself (what am I missing?)
 
D

Doug Harrison [MVP]

Your statements however, intrigue me... How would I explicitly
instantiate and export vector<string>? care to give an example ?

See:

How to export an instantiation of a Standard Template Library (STL) class
and a class that contains a data member that is an STL object
http://support.microsoft.com/kb/168958
Also, what is the side effect (i.e. gotcha) if the DLL client
instantiates the exported data type itself (what am I missing?)

As long as they're compiled with the same settings, probably nothing beyond
code duplication, which you experience every time you use an inline
function anyway. The primary danger lies in static data, which will be
duplicated in every module that instantiates the template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top