PDA

View Full Version : Exporting function under a namespace always lead to unresolve reference when linking



grayfox
5th July 2011, 18:15
In a shared lib, I have the following:


#ifndef SHAREDLIB_H
#define SHAREDLIB_H
#include "SharedLib_global.h"

namespace Fx{
SHAREDLIBSHARED_EXPORT int FunctionUnderNamespace();
}

SHAREDLIBSHARED_EXPORT int FunctionOutsideNamespace();
#endif // SHAREDLIB_H


This will compile without problem, but in the app source file:


#include <SharedLib.h>
#include <QDebug>
int main()
{
//int v = Fx::FunctionUnderNamespace(); // link error
int v = FunctionOutsideNamespace(); // works fine
qDebug() << v;
return 0;
}

Calling the function under a namespace will not even be compiled.
Do I actually need to export the namespace?

d_stranz
5th July 2011, 21:12
Your SHAREDLIBSHARED_EXPORT macro is probably not including the namespace when it declares the function for export. The linker is looking for Fx::FunctionUnderNamespace() and your macro apparently is exporting the global function ::FunctionUnderNamespace() instead.

Try doing this instead:


SHAREDLIBSHARED_EXPORT int Fx::FunctionUnderNamespace();

and get rid of the namespace Fx {} scope declaration that wraps the export declaration.

grayfox
6th July 2011, 10:07
Your SHAREDLIBSHARED_EXPORT macro is probably not including the namespace when it declares the function for export. The linker is looking for Fx::FunctionUnderNamespace() and your macro apparently is exporting the global function ::FunctionUnderNamespace() instead.

Try doing this instead:


SHAREDLIBSHARED_EXPORT int Fx::FunctionUnderNamespace();

and get rid of the namespace Fx {} scope declaration that wraps the export declaration.

I tried your suggestion but the shared lib won't even compile.

MarekR22
6th July 2011, 10:53
link error means that there is no definition of problematic function!
The main question is how did you define this symbol in dll? (I assume that macro SHAREDLIBSHARED_EXPORT is properly handled).
As a safety check use this function in your dll using proper namespace.