Zac*_*ter 3 .net linker compilation c++-cli
我正在尝试在C++/CLI中做一些非常简单的事情,但是我遇到了Visual Studio的问题,我无法弄清楚为什么.
我有两个项目,一个包含名为JNIUtils.h的头文件.它的内容非常准确 -
#pragma once
class JNIUtils
{
public:
JNIUtils( void );
~JNIUtils( void );
};
Run Code Online (Sandbox Code Playgroud)
它的实现也非常简单 -
#include "stdafx.h"
#include "JNIUtils.h"
JNIUtils::JNIUtils( void )
{
}
JNIUtils::~JNIUtils( void )
{
}
Run Code Online (Sandbox Code Playgroud)
我要做的就是在另一个项目中创建这个新定义类型的对象.在我包含此头文件的项目中,我已经去了
项目属性>配置属性> VC++目录>包含目录
我添加了一个条目,其中包含我所包含的头文件所在的路径(JNIUtils.h)
我正在尝试创建此对象的实例的CPP文件如下所示:
#include "stdafx.h"
#include "JNIUtils.h"
using namespace System;
int main(array<System::String ^> ^args)
{
JNIUtils *util = new JNIUtils();
Console::WriteLine(L"Hello World");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我收到以下错误:
Error 3 error LNK1120: 2 unresolved externals C:\zcarter\UDK\WebSvcClients\Debug\JNITester.exe JNITester
Error 2 error LNK2019: unresolved external symbol "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj JNITester
Error 1 error LNK2028: unresolved token (0A000009) "public: __thiscall JNIUtils::JNIUtils(void)" (??0JNIUtils@@$$FQAE@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) C:\zcarter\UDK\WebSvcClients\JNITester\JNITester.obj JNITester
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里我做错了什么?
如果要将项目中的类用于另一个项目,则需要执行以下两项操作:
1)将库编译代码(.lib)链接到主应用程序代码.
如果你有vs2010,只需在该Project->Properties->Common->References部分添加项目,所有的辛苦工作都将为你完成.
如果您有vs2008或更低版本,则必须将项目添加到依赖项中Project->Dependencies....然后,进入Project->Properties->Linker->Input并将.lib文件添加到现有的.dll列表中.使用相对路径,以便这将在其他计算机上工作.请注意,Referencevs2008中还有一个部分,但我相信它只适用于CLR程序集.
2)将标头添加到包含目录
你已经想到了这个部分,但是我建议你把它们添加到Project->Properties->C++->Additional Include Directories相反的部分,因为你使用的方法不能在另一台计算机上工作,除非它们与你做同样的配置.