【发布时间】:2013-06-21 15:56:30
【问题描述】:
我正在使用 Visual Studio 2010 构建一个 .dll。我写了一个试验:
// trialDLL.h
#ifndef TRIALDLL_H_
#define TRIALDLL_H_
// ... MyMathFuncs class definition omitted
#ifdef __cplusplus
extern "C"{
#endif
#ifdef TRIALDLL_EXPORT
#define TRIALDLL_API __declspec(dllexport)
#else
#define TRIALDLL_API __declspec(dllimport)
#endif
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset);
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath);
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs* myMath, double a, double b);
// some other similar stuff
#ifdef __cplusplus
}
#endif
#endif
以及triallDLL.cpp文件:
// trialDLL.cpp
#include "trialDLL.h"
TRIALDLL_API MyMathFuncs* __stdcall new_MyMathFuncs(double offset)
{
return new MyMathFuncs(offset);
}
TRIALDLL_API void __stdcall del_MyMathFuncs(MyMathFuncs *myMath)
{
delete myMath;
}
TRIALDLL_API double __stdcall MyAdd(MyMathFuncs *myMath, double a, double b)
{
return myMath->Add(a, b);
}
// ... some other definitions
有了项目中的这两个文件,我通过visual studio 2010属性管理器在项目中添加了一个属性表,并在用户宏中添加了TRIALDLL_EXPORT。毕竟,漂亮的 Intellisense 给了我在 .cpp 文件中定义的每个函数的错误,并抱怨“错误:可能未定义声明为 'dllimport' 的函数”。因此,Intellisense 似乎没有找到 TRIALDLL_EXPORT 定义。我认为如果我实际构建项目可能会有所不同,但结果表明同样的错误:“错误 C2491:'new_MyMathFuncs':不允许定义 dllimport 函数”。那么很明显宏TRIALDLL_EXPORT在编译时仍然没有定义。
在通过visual studio添加宏失败后,我也尝试将代码行:#define TRIALDLL_EXPORT放入trialDLL.cpp,但它也没有帮助。我想知道这样做的正确方法是什么?如何通知编译器定义了 micro 以便 TRIALDLL_API 计算为 dllexport 而不是 dllimport?
另外,如果我可以成功构建 .dll,是否有任何系统的方法来测试/验证 .dll 的功能?
提前感谢您的帮助! (虽然我知道在 stackoverflow 上将赞赏放在问题中是一个问题,但我觉得自己不这样做是不礼貌的。请原谅我因这些行造成的效率低下。)
【问题讨论】:
标签: c++ visual-studio-2010 dll macros