简体   繁体   English

是否可以编译使用 .NET DLL 的 C++?

[英]Is it possible to compile C++ that's using a .NET DLL with GCC?

I want to use the AML.Engine.dll ( https://github.com/AutomationML/AMLEngine2.1 ) in a C++ project.我想在 C++ 项目中使用 AML.Engine.dll ( https://github.com/AutomationML/AMLEngine2.1 )。 I have gotten it to work with the Microsoft C++ Compiler, but also need it to work with GCC but couldn't do so yet.我已经让它与 Microsoft C++ 编译器一起工作,但还需要它与 GCC 一起工作,但还不能这样做。 Are there any compiler settings necessary to do so?是否有必要的编译器设置? Or is it just not possible with GCC?还是 GCC 不可能?

The code below shows a simple demo project that I tried to get to work.下面的代码显示了一个我尝试开始工作的简单演示项目。

#include <iostream>
#include <string>
#include <stdlib.h> 
using namespace std;
#using <AML.Engine.dll>
#using <System.dll>
#using <System.Xml.dll>
#using <System.Xml.Linq.dll>
using namespace Aml::Engine::CAEX;

int main()
{
    // create a new empty document
    auto caexDocument = CAEXDocument::LoadFromFile("BSM_Project_210219_V3.1.aml");
    CAEXDocument^ doc = CAEXDocument::New_CAEXDocument();

    for each(auto instanceHierachy in caexDocument->CAEXFile->InstanceHierarchy){
        for each(auto element in instanceHierachy->InternalElement){
            System::Console::WriteLine( element->Name) ;
            System::Console::WriteLine( "----------------------------------------------------------") ;
            for each(auto ielement in element->InternalElement){  
                System::Console::WriteLine( ielement->Name) ;
            }

        }  

    }
}

There are 3 was to use a random dll in a .NET programm and vice versa.有 3 个是在 .NET 程序中使用随机 dll ,反之亦然。

  1. it is a .NET DLL.它是 .NET DLL。 There is nothing to do here.这里没有什么可做的。 you can always use a .NET dll in a .NET Programm.您始终可以在 .NET 程序中使用 .NET dll。 You are already doing it just compiling any start project您已经在编译任何启动项目
  2. COM interop. COM 互操作。 In many respects, .NET was a followup of the older COM approach.在许多方面,.NET 是旧的 COM 方法的后续。 As a result, the .NET Framework has extensive COM Interop support .因此,.NET 框架具有广泛的 COM 互操作支持
  3. P/Invoke . P/调用

Option 2 can run into binarity missmatch issues.选项 2 可能会遇到二进制不匹配问题。 Option 3 is almost guaranteed to.选项 3 几乎可以保证。

It is not really clear if your C++ programm is C++ .NET (a case 1 scenario) or what I call native C++ (wich is any C++ except .NET). It is not really clear if your C++ programm is C++ .NET (a case 1 scenario) or what I call native C++ (wich is any C++ except .NET). By design, .NET/MSIL are source language agnostic.按照设计,.NET/MSIL 与源语言无关。 It being .NET defines more about the limitations then the language does.它是 .NET 定义了比语言更多的限制。 I can not tell if the System.Xml.Linq.dll was written in C++ .NET, C#, F# or VB .NET. I can not tell if the System.Xml.Linq.dll was written in C++ .NET, C#, F# or VB .NET. And I do not need to know to use it.而且我不需要知道使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM