-
- Problem phenomenon
- Cause analysis
- Solutions
1 Problem phenomena
Exports functions and classes that have namespaces. The source code is as follows:
Header file MiniMFC.h
namespace MiniMFC{ void f(); class __declspec(dllexport) MyClass { public: void Mf(); };}
Implementing File MiniMFC.cpp
#include "stdafx.h"#include <iostream>usingnamespacestd;#include "MiniMFC.h"usingnamespace MiniMFC;void MyClass::Mf(){ cout"I‘m MiniMFC::MyClass::Mf() from DLL" << endl;}void f(){ cout"I‘m MiniMFC::f() from DLL" << endl;}
There are no exported functions in the resulting DLL file void f()
. Use Dependency Walker to view the results as:
[Email Protected]@@[email protected]@@z
[email protected]@[email protected] @QAEXXZ
Only the constructors and member functions of the class are exported, and the global function f()
is not exported.
2 Cause Analysis
The definition of a function in the implementation file f()
does not use namespaces MiniMFC
, so only a function named in the global namespace is obtained, and the f
function named in the namespace declared in the header file MiniMFC
f
does not implement the code. Let's change the function definition in the. cpp file to the following:
void f(){ cout"I‘m MiniMFC::f() from DLL" << endl;}
And then recompile, using Dependency Walker, you can see that this DLL does export a f
function named, but it is global, not in the Minimfc namespace.
[Email Protected]@@[email protected]@@z
[email protected]@[email protected] @QAEXXZ
[email protected] @YAXXZ
3 Solutions
Knowing the reason, solving it is very simple, only need to use the f()
Minimfc namespace to qualify in the definition of the implementation file. As shown below:
void MiniMFC::f(){ cout"I‘m MiniMFC::f() from DLL" << endl;}
After recompiling, this time the correct result was obtained:
[Email Protected]@@[email protected]@@z
[email protected]@[email protected] @QAEXXZ
[email protected]@ @YAXXZ
VC + + Export functions with namespaces