VS2015行为dllexport / dllimport

时间:2015-12-19 11:31:28

标签: c++ inheritance

使用VS2015弹出以下问题: 我想创建抽象基类,在自己的头中继承另一个抽象类,当抽象继承包含在自己的头中时,导入/导出宏似乎被解释为错误,因此不会生成继承的默认构造函数,我也看不到它在.lib中,另外当在exe项目中使用链接时会发生错误。

也许我只是有一些理解的问题......但对我而言,似乎api的定义在“TestDll.h”中是正确的而在“Inherited.h”中是错误的

重现的步骤: 我制作了一个项目“TestDll”,其中包含一个定义INHERITED_IN_SAME_HEADER的标题,包括一个定义INHERITED_IN_SAME_HEADER,用于在相同或自己的标题中的inhertiance声明之间切换。

标题“testdll_api.h”:

#pragma once
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

标题“TestDll.h”:

// include export/import macros
#include "testdll_api.h"  
// define INHERITED_IN_SAME_HEADER to 1 to enable Inherited declaration in same header
// define INHERITED_IN_SAME_HEADER to 0 to enable Inherited declaration in own header Inherited.h
#define INHERITED_IN_SAME_HEADER 0

// abstract class containing default constructor and destructor
class TESTDLL_API CTestDll 
{
public:
    virtual void purevirt() = 0;
protected:
    CTestDll()
    {}
    virtual ~CTestDll()
    {}
};

// Inherited class in same header like CTestDll
// works
#if INHERITED_IN_SAME_HEADER
// another abstract class extending 
class TESTDLL_API Inherited : public CTestDll
{
public:
    virtual void purevirtInherited() = 0;
};
#endif

标题“Inherited.h”:

#pragma once
#include "TestDll.h"
// strange here when following declaration is used intellisense (and potentially compiler) 
// see/uses TESTDLL_API as dllimport instead of dllexport ALTHOUGH WE ARE IN SAME PROJECT?!?!?
// note api definition is fetched indirectly from TestDLL.h which includes itself testdll_api.h
#if !INHERITED_IN_SAME_HEADER
class TESTDLL_API Inherited : public CTestDll
{
public:
    virtual void purevirtInherited() = 0;
};
#endif

在同一个解决方案中,我添加了控制台应用程序“TestConsoleApplication”,引用了“TestDll”项目,只需以下来源:

#include "stdafx.h"
#include "..\TestDll\Inherited.h"
class MyInheritedImpl : public Inherited
{
public:
    void purevirt()
    {}
    void purevirtInherited()
    {}
};
int main()
{
    MyInheritedImpl mimpl;

    return 0;
}

所以当我将INHERITED_IN_SAME_HEADER定义为1时,我将获得正确的控制台应用程序链接,当将INHERITED_IN_SAME_HEADER定义为0时,我会收到以下错误:

1>------ Build started: Project: TestDll, Configuration: Debug Win32 ------
1>  TestDll.cpp
1>  TestDll.vcxproj -> c:\users\chris\documents\visual studio 2015\Projects\TestDll\Debug\TestDll.dll
2>------ Build started: Project: TestConsoleApplication, Configuration: Debug Win32 ------
2>  TestConsoleApplication.cpp
2>TestConsoleApplication.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Inherited::Inherited(void)" (__imp_??0Inherited@@QAE@XZ) referenced in function "public: __thiscall MyInheritedImpl::MyInheritedImpl(void)" (??0MyInheritedImpl@@QAE@XZ)
2>TestConsoleApplication.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall Inherited::~Inherited(void)" (__imp_??1Inherited@@UAE@XZ) referenced in function "public: virtual __thiscall MyInheritedImpl::~MyInheritedImpl(void)" (??1MyInheritedImpl@@UAE@XZ)
2>c:\users\chris\documents\visual studio 2015\Projects\TestDll\Debug\TestConsoleApplication.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

0 个答案:

没有答案