pasparis
pasparis

Reputation: 43

Link a C++/CLI library in a CLR console project

I see on a question that it wasn't possible to link a static C++/CLI library, so I generated a dynamic one and try to link it in a C++ CLR console project with no success I get: Error 1 error LNK2020: unresolved token (06000001) cBox::.ctor pTest9.obj
I put the dll (Project ->reference->add new reference) set the include files in the project (include file with no code)

I don't know what to do (I am new with the C++/CLI)

thanks for suggestions/solution

Library Project declared as DLL

#include "stdafx.h"

using namespace System;

ref class cBox
{
public:
cBox() ;
cBox(double lv,double bv,double hv);

double Volume();

private:
double Length;
double Width;
double Height;
};

CODE OF THE LIBRARY :

#include "stdafx.h"
#include "cBox.h"


cBox::cBox() 
{
Console::WriteLine(L"No arg constructor called");

Length  = 1.0 ;
Width       = 1.0 ;
Height  = 1.0   ;

}

cBox::cBox(double lv,double bv,double hv)
{
Console::WriteLine(L"Constructor called");

Length  =   lv;
Width       =   bv;
Height  =   hv;

}
double cBox::Volume()
{
    return Length*Width*Height;
}

Then in the Console CLR Project I try to link this library, I get its .h file

#include "stdafx.h"
#include "cBox.h"

using namespace System;


int main(array<System::String ^> ^args)
{

Console::WriteLine(L"Test Library:\n");

cBox^ oBox; // handle of type box

oBox = gcnew cBox;

Console::WriteLine(L"Default Box Volume {0}",oBox->Volume());

  return 0;
}

Upvotes: 2

Views: 1765

Answers (1)

spencercw
spencercw

Reputation: 3358

You need to make a couple of changes to get this to work. First, you need to make the class public so your application can use it:

public ref class cBox
{
    ...

And you need to remove the #include "cBox.h" from your console application project; the inclusion is implicit when using managed libraries.

Upvotes: 3

Related Questions