Zachary Carter
Zachary Carter

Reputation: 373

Strange C++/CLI linking issue - unresolved external symbols but header file is linked

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?

Upvotes: 7

Views: 7859

Answers (3)

yu yang Jian
yu yang Jian

Reputation: 7165

In my case the error message show LNK2019 unresolved external symbol __imp_someFunctionName referenced in function ..., and I find that the prefix __imp_ of function name, means that the program is using __declspec(dllimport), not __declspec(dllexport).

So I add the proper name to Project->Properties->Configuration Properties->C/C++->Preprocessor, let __declspec(dllexport) work, then can solve error and build success.

reference to: How can I get rid of the __imp__ prefix in the linker in VC++?

Upvotes: 0

Coincoin
Coincoin

Reputation: 28586

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->References section, 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 in Project->Properties->Linker->Input and 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 a Reference section 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 Directories instead, as the method you used will not work on another computer unless they make the same configuration as you.

Upvotes: 10

davogotland
davogotland

Reputation: 2777

the key to your problem is that your trying to use this object in another project.

to do this, you must compile the class into a class library (i suggest a static one here), and then link this library in your compiler settings for the second program. this is somewhat tedious actually, but very good to try out!

note that there are two separate "linkings" here. one in your code, where you include the header file; and one in your linker (happens after compiler) where you link the object file (which is now inside a static library file).

Upvotes: 1

Related Questions