VC + + Export functions with namespaces

Source: Internet
Author: User

      • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.