C ++ / CLI的> C#错误C2526:C联动功能无法返回C ++类 [英] C++/CLI->C# error C2526: C linkage function cannot return C++ class

查看:1337
本文介绍了C ++ / CLI的> C#错误C2526:C联动功能无法返回C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有VS2010 C#暴露类

I have a simple .NET dll built with VS2010 C# that exposes 2 static members of a class

public class Polygon
{
    public static void Test(int test) {}
    public static void Test(List<int> test) {}
}

然后我从VS2010的C创建一个控制台应用程序++,并添加这个功能上面_tmain

I then created a Console app from VS2010 C++ and added this function above _tmain

extern "C" void TestMe()
{
    Polygon::Test(3);
}

添加参考,并编制给了我这个错误

Adding the reference and compiling gives me this error

1>WierdError.cpp(9): error C2526: 'System::Collections::Generic::List<T>::GetEnumerator' : C linkage function cannot return C++ class 'System::Collections::Generic::List<T>::Enumerator'
1>          with
1>          [
1>              T=int
1>          ]
1>          WierdError.cpp(9) : see declaration of 'System::Collections::Generic::List<T>::Enumerator'
1>          with
1>          [
1>              T=int
1>          ]
1>          WierdError.cpp(9) : see reference to class generic instantiation 'System::Collections::Generic::List<T>' being compiled
1>          with
1>          [
1>              T=int
1>          ]



我的一些意见:

Some of my observations:


  • 它编译成功,如果我删除的externC

  • ,成功,如果我重新命名编译测试(列表< INT>测试)的Test2(列表< INT>测试)

  • It compiles successfully if I remove extern "C"
  • It compiles successfully if I rename Test(List<int> test) to Test2(List<int> test)

我的问题是,什么错误,以及如何从C ++侧修复它。

My question is, what is going wrong and how to fix it from the C++ side.

我目前的解决办法是重命名在C#中的方法,但我宁可不要要做到这一点,我有一种感觉,还有我会在我的C ++项目中缺少一个设定

My current workaround is to rename the method in C#, but I would rather not have to do this, I have a feeling there is a setting I might be missing in my C++ project.

编辑:

我发现在C ++更好的解决办法,它看起来像我可以包装.NET在另一个函数调用。

I found a better workaround in the C++, it looks like I can wrap the .NET calls in another function.

void wrapper()
{
    Polygon::Test(3);
}

extern "C" void TestMe()
{
    wrapper();
}



看来愚蠢不得不这样做,我想知道,如果它是一个编译器错误?是什么让我害怕用这样的方法,并不必担心,C#开发者可以在以后添加这样一个静态方法,打破了C ++的基础之上。

It seems silly to have to do this, I'm wondering if it's a compiler bug? What scares me is using such methods and having to worry that the C# developer may at a later point add such a static method and break the C++ builds.

推荐答案

我只是要在这里采取了狂射,但有以下理由:

I am just going to take a wild shot here, with the following reasoning:

在编译期间MSVC的C ++编译器看到为externC功能 TESTME()是调用一个函数测试()里面类多边形多边形是一个不完整类型的编译器。我猜编译器不能看有无功能多边形::测试(3)将返回一个不完整的类型或在所有返回任何东西,它决定了它需要在返回一个错误如果该点型真可谓不是一个普通的C风格的POD类型。

During compilation MSVC's C++ compiler sees the extern "C" function TestMe() is calling an function Test() inside a class Polygon. Polygon is an incomplete type for the compiler. I guess the compiler cannot see whether the function Polygon::Test(3) is returning an incomplete type or returning anything at all, it decides that it needs to return an error at that point in case the type turns out to not be a plain C-style POD type.

以上,似乎对MSVC的一部分是合理的假设为(7.5 / 9 联动规范)C ++标准说:

The above seems a reasonable assumption on part of MSVC as in (7.5/9 "Linkage specifications") the C++ standard says:

链接从C ++到其他语言定义的对象,并从其他语言在C ++中定义的对象由实现定义与语言相关的,只有在两个语言实现的对象布局策略是相似的足够可这种联系可以实现。

这将解释错误消失一旦你删除外部C 链接规范或更换调用一个函数Cstyle

That would explain the error vanishing once you remove the extern C linkage specification or replace the call to a Cstyle function.

这篇关于C ++ / CLI的&GT; C#错误C2526:C联动功能无法返回C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆