简体   繁体   中英

VS2015 — Link CRL with C++ DLL

I'm a C# programmer and I'm trying this to make a C++/CLI wrapper. I've got three projects inside the solution:

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

I'm just doing tests, so I got these implementations:

C++ DLL Testing.h:

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

    int Increment();

private:
    int counter = 5;
};`

C++ DLL Testing.cpp

#include "Testing.h"

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

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

#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. I've also added the DLL solution header files to the Additional Includes of the Wrapper.

The C++ DLL project builds well, but when I build the Wrapper project I get these errors:

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

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

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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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