Reputation: 31
We are converting a set of C++/CLI projects from vs2017 targeting .netframework 471 to vs2019 targeting .net5.
The following code will compile and link when targeting .netframework471, but will generate a linker error when targeting .net5. Note that it seems to be only the method which uses gcrootSystem::String^, when the method foobar is commented out the project builds and links successfully (meaning that the foobar2 method with the gcrootSystem::Exception^ parameter does not cause a problem).
1>MSVCMRTD_netcore.LIB(mstartup.obj) : error LNK2022: metadata operation failed (80131195) : 1>LINK : fatal error LNK1255: link failed because of metadata errors
// UnmanagedClassWithGcHandle.h
#pragma once
#include <vcclr.h>
class UnmanagedClassWithGcHandle
{
public:
int age;
// error LNK2022 : metadata operation failed(80131195) : ( ONLY TARGETING .NET 5.0 )
void foobar(gcroot<System::String^>);
// this is successful
void foobar2(gcroot<System::Exception^>);
};
#include "pch.h"
#include "UnmanagedClassWithGcHandle.h"
// UnamanagedClassWithCcHandle.cpp
void UnmanagedClassWithGcHandle::foobar(gcroot<System::String^>)
{
}
void UnmanagedClassWithGcHandle::foobar2(gcroot<System::Exception^>)
{
}
Upvotes: 3
Views: 345
Reputation: 62492
I've had exactly the same problem. The problem is with:
#include <vcclr.h>
This is the old file for accessing gcroot
. Instead you need to:
#include <msclr/gcroot.h>
This will bring in the gcroot
that is in the msclr
namespace. Change your code to:
void foobar(msclr::gcroot<System::String^>);
And you should be able to build.
Upvotes: 1