I’m trying to do something very simple in C++/CLI, however I’m running into issues with Visual Studio and I can’t figure out why.
I have two projects, one which contains a header file named JNIUtils.h. Its content is very barebones –
#pragma once
class JNIUtils
{
public:
JNIUtils( void );
~JNIUtils( void );
};
and its implementation is very simple as well –
#include "stdafx.h"
#include "JNIUtils.h"
JNIUtils::JNIUtils( void )
{
}
JNIUtils::~JNIUtils( void )
{
}
All I’m trying to do is to create an object of this newly defined type in another project. In the project where I’m including this header file, I’ve gone to
Project Properties > Configuration Properties > VC++ Directories > Include Directories
I added an entry with the path to where the header file I’m including resides (JNIUtils.h)
The CPP file I’m trying to create an instance of this object in looks as follows:
#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;
}
When I try to compile I get the following errors:
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
Can anyone tell me what I’m doing wrong here?
When you want to use classes from a project into another one, there are two things that you need to do:
1) Link the library compiled code (.lib) to your main application code.
If you have vs2010, simply add the project in the
Project->Properties->Common->Referencessection, all the hard work will be done for you.If you have vs2008 or lower, you must add the project to the dependencies in
Project->Dependencies.... Then, go inProject->Properties->Linker->Inputand add the .lib file to the existing list of .dll. Use a relative path so that this will work on other computers. Note that there is also aReferencesection in vs2008, but I believe it only works for CLR assemblies.2) Add the headers to the include directories
You figured that part already, but I recommend you to add them to the
Project->Properties->C++->Additional Include Directoriesinstead, as the method you used will not work on another computer unless they make the same configuration as you.