如何将C++类导出为dll?

Alw*_*wyn 8 c++ class dllexport visual-studio

我来自C#/ Java背景,所以我试图弄清楚如何创建一个行为类似于C#dll的C++ DLL.

我曾尝试用__declspec(dllexport)__declspec(dllimport),但我只设法得到它的静态方法的工作.我确信这是由于我的理解有限.

如何在C++中导出类(完整地包括私有成员)并能够像引用C#一样在引用端实例化它们?一些指向在线资源/教程的指针也会这样做.

我开始使用MFC DLL模板,老实说,我不知道它们中有90%是什么,为什么我继承CWinApp.我试图标记该类,CCppPracticeLibraryApp但它将不再编译.

// CppPracticeLibrary.h : main header file for the CppPracticeLibrary DLL
//


#pragma once

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols

#ifdef CCppPracticeLibraryApp_EXPORTS
#define CCppPracticeLibraryApp_API __declspec(dllexport) 
#else
#define CCppPracticeLibraryApp_API __declspec(dllimport) 
#endif

// CCppPracticeLibraryApp
// See CppPracticeLibrary.cpp for the implementation of this class
//

class CCppPracticeLibraryApp : public CWinApp
{
public:
    CCppPracticeLibraryApp();
    static CCppPracticeLibraryApp_API void SayHelloWorld();
// Overrides
public:
    virtual BOOL InitInstance();

    DECLARE_MESSAGE_MAP()
};
Run Code Online (Sandbox Code Playgroud)

定义文件:

//CppPracticeLibrary.cpp:定义DLL的初始化例程.

#include "stdafx.h"
#include "CppPracticeLibrary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define CCppPracticeLibraryApp_EXPORTS



BEGIN_MESSAGE_MAP(CCppPracticeLibraryApp, CWinApp)
END_MESSAGE_MAP()


// CCppPracticeLibraryApp construction

CCppPracticeLibraryApp::CCppPracticeLibraryApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

void CCppPracticeLibraryApp::SayHelloWorld()
{
    printf( "Hello world");
}


// The one and only CCppPracticeLibraryApp object

CCppPracticeLibraryApp theApp;


// CCppPracticeLibraryApp initialization

BOOL CCppPracticeLibraryApp::InitInstance()
{
    CWinApp::InitInstance();

    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

客户端/引用方法

// TestConsoleApplication.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TestConsoleApplication.h"
#include "CppPracticeLibrary.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.
            /*CCppPracticeLibraryApp* testCallingLibrary =  new CCppPracticeLibraryApp();
            testCallingLibrary->SayHelloWorld();*/
            CCppPracticeLibraryApp::SayHelloWorld();
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    return nRetCode;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够在上面的代码中取消注释以下行:

        /*CCppPracticeLibraryApp* testCallingLibrary =  new CCppPracticeLibraryApp();
        testCallingLibrary->SayHelloWorld();*/
Run Code Online (Sandbox Code Playgroud)

bas*_*h.d 6

来自MSDN

要导出类中的所有公共数据成员和成员函数,关键字必须出现在类名的左侧,如下所示:

class __declspec(dllexport) CExampleExport : public CObject
{ ... class definition ... };
Run Code Online (Sandbox Code Playgroud)

另外,请考虑有更多方法可以做到这一点,比如.DEF-files.花点时间阅读MSDN网站上的解释.

  • @Alwyn它导出类的所有成员,包括内部编译器生成的代码和数据,如vtable,隐式生成的成员函数,RTTI等. (2认同)

cos*_*arc 6

使用__declspec(dllexport)和__declspec(dllimport),您只需创建一种api,它可用于从您的dll导出方法或成员.通过导出此方法,您可以从另一个dlll访问它.您可以做的是创建一个头文件,您将在其中定义导出宏.

    ifdef MYPROJECT_EXPORTS
      define MYPROJECT_EXPORTS__declspec( dllexport )
   else 
       define MYPROJECT_EXPORTS__declspec( dllimport )
   endif 
Run Code Online (Sandbox Code Playgroud)

当你声明你的方法时,如果你想导出它,你只需要在方法声明之前放置你的宏,如下所示:

MYPROJECT_EXPORTS void myMethod();
Run Code Online (Sandbox Code Playgroud)

此外,您还必须将符号添加到预处理器定义中(在MS Visual Studio中 - >项目属性 - > C/C++ - >预处理器 - >预处理器定义.