Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a function within its namespace?

I'm fairly certain this isn't possible from what I've read, and tried. Although for ease and continuity in code I would like to ask here.

Is it possible to export a function along with its namespace container?

For example:

namespace Continuity
{
    int Foo(void);
}

Standard usage would be: Continuity::Foo();

I want to be able to export this function to use in a DLL, however I want to keep it in the namespace structure so that I can use the same usage in the DLL.

like image 213
user1591117 Avatar asked Mar 18 '23 07:03

user1591117


2 Answers

You asked:

Is it possible to export a function along with it's namespace container example:

Yes, it is possible.

Use:

namespace Continuity
{
   DLL_EXPORT int Foo(void);
}

Where DLL_EXPORT is #defined to either __declspec(dllexport) or __declspec(dllimport) appropriately.

In the project where you build the DLL, you'll need:

#define DLL_EXPORT __declspec(dllexport)

In projects where you use the DLL, you'll need:

#define DLL_EXPORT __declspec(dllimport)
like image 98
R Sahu Avatar answered Mar 19 '23 20:03

R Sahu


Make sure not to use extern "C" when declaring the function otherwise the namespace is not used for linking the function. You may then have 2 dlls with the same function in 2 different namespaces but only one function will be called randomly based on the order the functions are loaded.

like image 26
AlexP Avatar answered Mar 19 '23 21:03

AlexP