Sunkyu Hwang

Hello,

I am trying to use some .NET stuffs inside already existing native C++ source pool.

I experienced wierd "unresolved symbol" error and ultimately solved the problem but cannot explain why.

Could anyone please tell me what I did Thank you very much,

[ Problem Source Files ]

MixMode.lib (static lib created with /clr)

// ========= "MixMe.h" =========

#pragma once

#include <string>

namespace Minerva { namespace MixMode {

public ref class MixMe

{

protected:

System::Xml::XmlDocument^ _doc;

public:

MixMe(const std:Tongue Tiedtring& fileName);

virtual ~MixMe();

};

}}

// ========= "MixMe.cpp" ========

#include "MixMe.h"

Minerva::MixMode::MixMe::MixMe(const std:Tongue Tiedtring& fileName)
{
_doc = gcnew System::Xml::XmlDocument();
}

Minerva::MixMode::MixMe::~MixMe() {}

"MixTool.cpp" (console app with /clr) trying to link with "MixMode.lib"

#include "MixMe.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
printf("Hello, mixed mode operation...\n");

return 0;
}

Eventually, I am getting

Error 1 error LNK2020: unresolved token (06000001) Minerva.MixMode.MixMe::.ctor MixTool.obj
Error 2 error LNK2020: unresolved token (06000002) Minerva.MixMode.MixMe::~MixMe MixTool.obj
Error 3 fatal error LNK1120: 2 unresolved externals G:\PROJECT\Test\MixMode\Debug\MixTool.exe

[ Solved Source Files ] // Very strange why this works ...

MixMode.lib (static lib created with /clr)

// ========= "MixMe.h" =========

#pragma once

#include <string>

namespace Minerva { namespace MixMode {

public ref class MixMe

{

protected:

System::Xml::XmlDocument^ _doc;

public:

MixMe(const std:Tongue Tiedtring& fileName)

{ _doc = gcnew System::Xml::XmlDocument(); }

virtual ~MixMe() {}

};

}}

// ========= "MixMe.cpp" ========

#include "MixMe.h"

"MixTool.cpp" ==> Same as above. / Link success / Runtime Behaviour OK.



Re: Visual C++ Language Mixed Mode : Should definitions be in the headers when creating static mixed lib?

Viorel.

I cannot explain why, but if you add these lines to MixMe.cpp file:

void dummy()

{

}

and then add these lines to main function:

extern void dummy();

dummy();

and also ensure that the main project is made dependent on static library, then the linker errors seem to disappear.





Re: Visual C++ Language Mixed Mode : Should definitions be in the headers when creating static mixed lib?

crescens2k

This is basically because no matter what the compiler allows you to do, the .net part of it wasn't designed to work with static libraries. There are some things which aren't stored when you create the library and I think it is actually the metadata or something else just as important here. This is why there is no static library .net template when you create a new project.

If you want to do this, you should create a netmodule, this is done by using the /LN compiler option and the /noassembly linker option and then link the resulting .netmodule to the executable.






Re: Visual C++ Language Mixed Mode : Should definitions be in the headers when creating static mixed lib?

Sunkyu Hwang

Hi, Viorel,

You are right. Here are what I am planning to do until some fix is coming or some magical options being discovered.

For creating mixed static library ...

[1] WheneverI need C-like functions rather than class object (like mini tool), I think I can separate the definition out of the header file. (So I will have function declaration in header namespace, definition in source files)

[2] When I really need some class object requires .NET classes inside it (I have to create some ref class in this case), I will create them as C# style. (Putting all definitions in the C++ header file)

By doing [1] and [2], my apps (whether it is "main(int, char**)" form or "main(array<System:Tongue Tiedtring ^> ^args)" form) will not have big problems in linking.

Runtime behavior has not been investigated yet (I hope I am not getting a bomb by the CLR in the middle of exe runs), but time will tell.

I am just stunned how this simple thing makes incredible complexity to the linker. Especially when the maker of the OS is same as the C++/CLI language syntax creator.

Thanks,





Re: Visual C++ Language Mixed Mode : Should definitions be in the headers when creating static mixed lib?

Sunkyu Hwang

Hi,

I found an elegant solution for my problem. (Thanks to Nishant's Book !)

This solution will reduce the overhead of forcing C++/CLI syntax when native developers are trying to use my classes as well.

Still memory management is a big topic, I think I have to study further before delving in.

Thank you,

-- Sunkyu

// ============ LIB HEADER ======================

#include <string>
#include <msclr\auto_gcroot.h>

namespace Minerva {

namespace MixMode {

class MixMe
{
protected:
msclr::auto_gcroot<System::Xml::XmlDocument^> _doc;

public:
MixMe(const std:: string& fileName);
virtual ~MixMe();
};
}
}

// ============ LIB SOURCE ======================

#include "MixMe.h"

using namespace Minerva::MixMode;
using namespace System::Xml;

MixMe::MixMe(const std:: string& fileName)
{
_doc = gcnew XmlDocument();
}

MixMe::~MixMe() {}

// ============ APP CODE ======================

#include "MixMe.h"
#include <stdio.h>

using namespace Minerva::MixMode;

int main(int argc, char* argv[])
{
MixMe* mix = new MixMe("Hello, world");
delete mix;

return 0;
}