tags:

views:

693

answers:

3

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;
}
+1  A: 

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.

Reed Copsey
yes...they are all managed. this is really puzzling.
ShaChris23
+3  A: 

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.

Bojan Resnik
Thank you, Bojan; problem solved. I wish I could have voted for you 10 times..but stackoverflow only allows 1. ;-)
ShaChris23
Anytime - I'm glad it helped :)
Bojan Resnik
A: 

Here's the final modification as proposed by Bojan. Thanks Bojan!

Note: dont't forget to add "Lib" to your "LibTest" Reference!

Lib Project (compiled as .dll project)

//Lib.h

#pragma once

public 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"

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

int main()
{
  return 0;
}
ShaChris23