c ++:不允许定义dllimport函数,使用visual studio 2010构建

spr*_*oll 8 c++ macros dll visual-studio-2010

我正在使用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
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

通过项目中的这两个文件,我通过visual studio 2010属性管理器向项目添加了一个属性表,并添加TRIALDLL_EXPORT到用户宏中.在所有这些之后,好的Intellisense给了我.cpp文件中定义的每个函数的错误并且抱怨"错误:声明'dllimport'的函数可能没有被定义".所以似乎Intellisense找不到TRIALDLL_EXPORT定义的.我认为如果我实际构建项目可能会有所不同,但结果表明同样的错误:"错误C2491:'new_MyMathFuncs':不允许定义dllimport函数".然后很明显,宏TRIALDLL_EXPORT仍然没有在编译时定义.

在通过visual studio无法添加宏之后,我也试过把代码行#define TRIALDLL_EXPORT放在trialDLL.cpp中,但它也没有帮助.我想知道这样做的正确方法是什么?如何通知编译器微定义是为了TRIALDLL_API评估dllexport而不是dllimport

另外,如果我能成功构建.dll,是否有任何系统的方法来测试/验证.dll的功能?

在此先感谢您的帮助!(虽然我知道堆栈溢出是一个问题,在这个问题上加以理解,我觉得自己不礼貌,不这样做.请原谅我因这些问题导致的任何低效率.)

Rei*_*ica 12

VS属性表中的"用户宏"与预处理器宏无关.放入TRIALDLL_EXPORT属性表的部分C/C++ > Preprocessor > Preprocessor Definitions

"用户宏",这只能在属性表中定义,允许你创建自己的"变量"可在Visual Studio中的属性,类似于内置的人$(TargetName),$(SolutionDir)等等.