无法使用 Visual Studio 导出 C++ dll

Posted

技术标签:

【中文标题】无法使用 Visual Studio 导出 C++ dll【英文标题】:Can't export a C++ dll with visual studio 【发布时间】:2016-04-24 21:24:05 【问题描述】:

我正在尝试创建一个 C++ dll。我已按照 msdn 教程进行操作,但无法正确编译我的 dll。

问题是任何函数都被导出。我已经使用dumpbin.exe 工具和nm 工具对其进行了测试。

在这两种情况下,都没有检测到符号。

这是这个库的代码:

头文件:

#ifndef NLIB_H
#define NLIB_H

#ifdef _WINDLL
#define NLIB_EXPORTS __declspec( dllexport )
#else
#define NLIB_EXPORTS __declspec( dllimport )
#endif

#ifdef __cplusplus
extern "C"
#endif

    NLIB_EXPORTS int fun1(int n);

#ifdef __cplusplus

#endif

#endif

源代码文件:

#include "nlib.h"

int fun1(int n) 
    return 100;

【问题讨论】:

你在构建 dll 时是否定义了_WINDLL 是的,如果您选择动态库作为项目类型,则由 Visual Studio 定义。此外,VS 2013 指示定义了哪些宏,儿子我确定 _WINDLL 正在工作。 _WINDLL 在哪里定义 - 在某个头文件中,还是在项目设置中? 在项目设置中。 【参考方案1】:

我发现了错误。还需要将NLIB_EXPORTS 添加到*.c 文件中,如下所示:

#include "nlib.h"

NLIB_EXPORTS int fun1(int n) 
    return 100;

【讨论】:

以上是关于无法使用 Visual Studio 导出 C++ dll的主要内容,如果未能解决你的问题,请参考以下文章