【问题标题】:Cannot export functions in a DLL无法导出 DLL 中的函数
【发布时间】:2014-04-03 12:59:14
【问题描述】:

我有一个 .lib,它有一个我想制作成 DLL 的函数。

在项目属性中,我做了两件事, 1.在C/C++ -> General -> Additional Directories:添加.h文件的路径。 2.在Linker->General->Additional Dependies:添加.lib文件的路径

然后我做了一个.h文件

#ifndef _DFUWRAPPER_H_
#define _DFUWRAPPER_H_

#include <windows.h>
#include "DFUEngine.h"

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport) void helloworld(void);
__declspec(dllexport) void  InitDLL();

#ifdef __cplusplus
}
#endif 

#endif 

并制作了 .cpp 文件

#include "stdafx.h"
#include "stdio.h"
#include "DFUWrapper.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

void helloworld(void)
{
    printf("hello world DFU");
}

DFUEngine* PyDFUEngine()
{
    return new DFUEngine();
}

void delDFUEngine(DFUEngine *DFUe)
{
    DFUe->~DFUEngine();
}

void  PyInitDLL(DFUEngine *DFUe)
{
    return DFUe->InitDLL();
}

我用函数 helloword 做了一个测试。我可以在 DLL 中看到这个函数,但在 InitDLL 函数中看不到。 我怎么能解决这个问题?请帮忙

【问题讨论】:

  • Visual Studio,大概。什么版本?您无法“看到”该功能是什么意思?显示一些数据/错误/输出。
  • 我正在使用 VS2005。我正在检查依赖步行者。我可以看到 halloworld() 函数,但看不到 InitDLL()
  • 在你的 .c 文件中是PyInitDll。在您的 .h 中,它是 InitDll

标签: c++ dll visual-studio-2005


【解决方案1】:

在您的 DLL 头文件中定义以下内容

#if defined (_MSC_VER)
#if defined (MY_DLL_EXPORTS)
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT __declspec(dllimport)
#endif
#else
#define MY_EXPORT
#endif

使用该宏声明您的函数

#ifdef __cplusplus
extern "C" {
#endif

MY_EXPORT void helloworld(void);
MY_EXPORT void InitDLL();

#ifdef __cplusplus
}
#endif 

在你的 .cpp 中

MY_EXPORT void helloworld(void)
{
    printf("hello world DFU");
}

MY_EXPORT void InitDLL()
{
    /// blahblah 
}

使用 MY_DLL_EXPORT 定义编译您的 DLL.... 但请确保它没有定义您何时需要 IMPORT ....

【讨论】:

    【解决方案2】:

    我喜欢从 DLL using .DEF files 导出函数。

    这还有一个额外的好处,那就是避免名称重整:不仅是 C++ 复杂重整,还有 __stdcallextern "C" 函数(例如 _myfunc@12)发生的重整。

    您可能只想为您的 DLL 添加一个 DEF 文件,例如:

    LIBRARY   MYDLL
    EXPORTS
       InitDLL     @1
       helloworld  @2
       ... other functions ...
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 2013-11-04
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2010-09-28
      • 2016-01-07
      相关资源
      最近更新 更多