Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 317
Zachary Carter Avatar asked Jan 12 '12 03:01

Zachary Carter


People also ask

How do I fix unresolved external symbol?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

What causes unresolved external symbol?

The compiler can identify when a symbol isn't declared, but it can't tell when the symbol isn't defined. That's because the definition may be in a different source file or library. If a symbol is referred to but never defined, the linker generates an unresolved external symbol error.

How do I fix LNK1120?

The LNK1120 message comes last, and shows the unresolved symbol error count. You don't need to fix this error. This error goes away when you correct all of the LNK2001 and LNK2019 linker errors before it in the build output.

What kind of error is unresolved external symbol?

So, as a result - an "undefined reference/unresolved external symbol error" happens when the linker cannot find global symbols in the object files.


1 Answers

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.

like image 107
Coincoin Avatar answered Nov 15 '22 08:11

Coincoin