Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managed C++ unresolved token

Tags:

managed-c++

I'm new to managed C++.

I have two managed C++ projects in a single .sln, Project Lib and Project LibTest. LibTest makes use of Lib.

Lib compiles and links fine. The project is set as a .dll.

LibTest is also compiled as .dll, but when it goes into linking, I get "unresolved token" on all of the Lib::methods. Those methods definitions are defined in the Lib .cpp file.

If I moved the definitions into the Lib.h file, everything works.

I have already modified LibTest's Reference to depend on Lib project.

What am I missing?

EDIT: Okay here's exactly what I have and it still doesn't work.

First off, I'm using Visual Studio 2008 SP1.

Secondly, when I did a similar exercise in C#, it worked fine.

I created an empty C++ CLR project. I added a Lib project. I added a managed class. VSTD generated Lib.h and Lib.cpp. The constructor is automatically generated.

Then I added another project to my solution; I called it LibTest. I added another managed class called LibTest. LibTest.h and LibTest.cpp are generated. I tried to instantiate Lib in LibTest constructor, but during linking it simply said:

1>LibTest.obj : error LNK2020: unresolved token (06000002) Lib::.ctor

Here's the exact code:

Lib Project (compiled as .dll project)

//Lib.h
#pragma once

ref class Lib
{
public:
  Lib(void);
};


//Lib.cpp
#include "Lib.h"

Lib::Lib(void)
{
}

LibTest Project (compiled as application.exe)

// LibTest.h
#pragma once

ref class LibTest
{
public:
  LibTest(void);
};

// LibTest.cpp
#include "LibTest.h"
#include "Lib.h"

LibTest::LibTest(void)
{
  Lib^ lib = gcnew Lib;
}

int main()
{
  return 0;
}
like image 215
sivabudh Avatar asked Jun 01 '09 19:06

sivabudh


3 Answers

Managed C++ works just like C# regarding types in different assemblies. What this means is that you need to declare your Lib class as public:

public ref class Lib

And you should not include Lib.h in your LibTest project. When you add the reference to the Lib project, the compiler will be able to resolve any symbols found there.

Your current code includes Lib.h, and so the linker searches for the Lib class in the LibTest assembly and looks for the constructor there.

like image 181
Bojan Resnik Avatar answered Jan 04 '23 04:01

Bojan Resnik


If you're using managed extensions, setting the reference correctly should be all you need.

If you're using standard C++ functions, you probably need to define your functions using __declspec(dllexport) and __declspec(dllimport). See MSDN for details.

__declspec(dllexport) is what adds specific functions into the export library, and __declspec(dllimport) tells the importing library (LibTest) that it needs to import those symbols from the DLL.

like image 20
Reed Copsey Avatar answered Jan 04 '23 06:01

Reed Copsey


In project LibTest, property->Configuration properties->Linker->Input->set Additional Dependecies, include Lib.lib

like image 39
Penny Avatar answered Jan 04 '23 06:01

Penny