简体   繁体   English

VS2015 - 使用C ++ DLL链接CRL

[英]VS2015 — Link CRL with C++ DLL

I'm a C# programmer and I'm trying this to make a C++/CLI wrapper. 我是一名C#程序员,我正在尝试制作一个C ++ / CLI包装器。 I've got three projects inside the solution: 我在解决方案中有三个项目:

  • C++ DLL C ++ DLL
  • C++ CLR C ++ CLR
  • WPF C# WPF C#

I'm just doing tests, so I got these implementations: 我只是在做测试,所以我得到了这些实现:

C++ DLL Testing.h: C ++ DLL Testing.h:

class Test
{
public:
    Test();
    ~Test();

    int Increment();

private:
    int counter = 5;
};`

C++ DLL Testing.cpp C ++ DLL Testing.cpp

#include "Testing.h"

int Test::Increment()
{
    return counter++;
}

CLR Wrapper.h CLR Wrapper.h

#pragma once
#include "Testing.h"

using namespace System;

namespace Wrapper
{
    public ref class Wraptest
    {
    public:
        Wraptest();
        int Increment();

    private:
        Test* t;
    };
}

CLR Wrapper.cpp CLR Wrapper.cpp

#include "stdafx.h"
#include "Wrapper.h"
#include "PITest.h"

Wrapper::Wraptest::Wraptest()
{
    t = new Test();
}

int Wrapper::Wraptest::Increment()
{
    return t->Increment();
}

I've added a reference to the Testing C++ DLL project inside the Wrapper project. 我在Wrapper项目中添加了对Testing C ++ DLL项目的引用。 I've also added the DLL solution header files to the Additional Includes of the Wrapper. 我还将DLL解决方案头文件添加到包装器的其他包含中。

The C++ DLL project builds well, but when I build the Wrapper project I get these errors: C ++ DLL项目构建良好,但是当我构建Wrapper项目时,我得到了以下错误:

Severity Code Description Project File Line Suppression State Error LNK2028 unresolved token (0A000007) "public: __thiscall Test::Test(void)" (??0Test@@$$FQAE@XZ) referenced in function "public: __clrcall Wrapper::Wraptest::Wraptest(void)" (??0Wraptest@Wrapper@@$$FQ$AAM@XZ) Wrapper C:\\Users\\mytoy\\Documents\\Visual Studio 2015\\Projects\\CodeTest\\Wrapper\\Wrapper.obj 1 严重程序代码说明项目文件行抑制状态错误LNK2028未解析的令牌(0A000007)“public:__ thiscall Test :: Test(void)”(?? 0Test @@ $$ FQAE @ XZ)在函数“public:__clrcall Wrapper :: Wraptest”中引用:: Wraptest(void)“(?? 0Wraptest @ Wrapper @@ $$ FQ $ AAM @XZ)Wrapper C:\\ Users \\ mytoy \\ Documents \\ Visual Studio 2015 \\ Projects \\ CodeTest \\ Wrapper \\ Wrapper.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall Test::Test(void)" (??0Test@@$$FQAE@XZ) referenced in function "public: __clrcall Wrapper::Wraptest::Wraptest(void)" (??0Wraptest@Wrapper@@$$FQ$AAM@XZ) Wrapper C:\\Users\\mytoy\\Documents\\Visual Studio 2015\\Projects\\CodeTest\\Wrapper\\Wrapper.obj 1 严重性代码说明项目文件行抑制状态错误LNK2019未解析的外部符号“public:__thiscall Test :: Test(void)”(?? 0Test @@ $$ FQAE @ XZ)在函数“public:__clrcall Wrapper :: Wraptest ::”中引用Wraptest(void)“(?? 0Wraptest @ Wrapper @@ $$ FQ $ AAM @XZ)Wrapper C:\\ Users \\ mytoy \\ Documents \\ Visual Studio 2015 \\ Projects \\ CodeTest \\ Wrapper \\ Wrapper.obj 1

Severity Code Description Project File Line Suppression State Error LNK1120 2 unresolved externals Wrapper C:\\Users\\mytoy\\Documents\\Visual Studio 2015\\Projects\\CodeTest\\Debug\\Wrapper.dll 1 严重级代码描述项目文件行抑制状态错误LNK1120 2未解析的外部包装器C:\\ Users \\ mytoy \\ Documents \\ Visual Studio 2015 \\ Projects \\ CodeTest \\ Debug \\ Wrapper.dll 1


I can't figure out the solution. 我无法弄清楚解决方案。 Thanks. 谢谢。

Okay, I managed to get it working. 好的,我设法让它运转起来。 It's prety easy. 这很简单。

I checked this link and I was missing this: 我检查了这个链接 ,我错过了这个:

// C++ DLL Header
#ifdef TESTING_EXPORTS  
    #define TESTING_API __declspec(dllexport)   
#else  
    #define TESTING_API __declspec(dllimport)   
#endif

On the wrapper I just added the header location to the Additional Included Directories (Linker unchanged) and I just works out of the box. 在包装器上,我刚刚将标题位置添加到附加包含目录(链接器未更改),我只是开箱即用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM