链接CLR控制台项目中的C ++ / CLI库

时间:2012-02-25 11:36:00

标签: c++ c++-cli

我在一个问题上看到无法链接静态C ++ / CLI库,因此我生成了一个动态库并尝试在C ++ CLR控制台项目中将其链接但没有成功 我明白了: 错误1错误LNK2020:未解析的令牌(06000001)cBox ::。ctor pTest9.obj
我把dll(Project - > reference-> add new reference)设置为项目中的include文件 (包括没有代码的文件)

我不知道该怎么做(我是C ++ / CLI新手)

感谢您的建议/解决方案

库项目声明为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;
};

图书馆代码:

#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;
}

然后在Console CLR项目中我尝试链接这个库,我得到它的.h文件

#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;
}

1 个答案:

答案 0 :(得分:3)

您需要进行一些更改才能使其正常运行。首先,您需要创建类public,以便您的应用程序可以使用它:

public ref class cBox
{
    ...

您需要从控制台应用程序项目中删除#include "cBox.h";使用托管库时,包含是隐式的。