简体   繁体   English

嵌入 Linux 上的 C++ 应用程序时 Mono 运行时崩溃

[英]Mono runtime crash when embedding in C++ application on Linux

Background / Description of problem I'm trying to run c# scripts as part of my c++ application which I'm compiling in Debian-9 Linux.背景/问题描述我正在尝试运行 c# 脚本作为我在 Debian-9 Linux 中编译的 c++ 应用程序的一部分。 However, my application crashes with an error from coming from the mono runtime.但是,我的应用程序因来自 mono 运行时的错误而崩溃。

I have installed mono from Debian repositories and have even tried the same experiment with a version of mono compiled by from source.我已经从 Debian 存储库安装了 mono ,甚至尝试使用从源代码编译的 mono 版本进行相同的实验。 The c# code, I have compiled into a dll using mcs -t:library *.cs . c# 代码,我使用mcs -t:library *.cs编译成 dll 。 When trying to run my application it crashes with the following error:尝试运行我的应用程序时,它崩溃并出现以下错误:

* Assertion at object.c:116, condition `is_ok (error)' not met, 

function:mono_runtime_object_init, (null) assembly:/usr/lib/mono/4.5/mscorlib.dll type:TypeInitializationException member:(null)


=================================================================
    Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x7f80f3fb9ccb - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3fba069 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3f4479f - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3fb9273 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f3e5b510 - /lib/x86_64-linux-gnu/libpthread.so.0 : 
    0x7f80f3994081 - /lib/x86_64-linux-gnu/libc.so.6 : gsignal
    0x7f80f397f535 - /lib/x86_64-linux-gnu/libc.so.6 : abort
    0x7f80f3ea6f94 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41935f6 - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41ae2db - /usr/lib/libmonosgen-2.0.so.1 : 
    0x7f80f41ae89d - /usr/lib/libmonosgen-2.0.so.1 : monoeg_assertion_message
    0x7f80f40bf357 - /usr/lib/libmonosgen-2.0.so.1 : mono_runtime_object_init
    0x561bf41542e0 - ./mono_test : 
    0x7f80f3980bbb - /lib/x86_64-linux-gnu/libc.so.6 : __libc_start_main
    0x561bf415412a - ./mono_test : 
    .... more details follow ....

Minimal example C++ code (main.cpp):最小示例C++ 代码(main.cpp):

#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>

#include <stdexcept>

int main(){
    auto domain = mono_jit_init("MyApp");  
    
    auto assembly = mono_domain_assembly_open(domain, "hello.dll");
    if (assembly == nullptr) {
        throw std::runtime_error("Error loading mono assembly hello.dll");
    }

    auto image = mono_assembly_get_image(assembly);
    auto mono_class = mono_class_from_name(image, "Test", "Hello");
    if (mono_class == nullptr) {
        throw std::runtime_error("Error finding class Test::Hello");
    }

    auto instance = mono_object_new(domain, mono_class);
    mono_runtime_object_init(instance);

    mono_jit_cleanup(domain);
}

C# code (hello.cs): C# 代码(hello.cs):

using System;

namespace Test {
    class Hello {

        Hello() {
            Console.WriteLine("Hello constructor.");
        }

        void Update() {
            Console.WriteLine("Yay update is called.");
        }

    }
}

CMakeLists: CMakeLists:

cmake_minimum_required(VERSION 3.9)

project(mono_test)
set (CMAKE_CXX_FLAGS "-std=c++17")


file(GLOB SOURCES *.cpp)
add_executable(mono_test ${SOURCES})

INCLUDE(FindPkgConfig)

PKG_SEARCH_MODULE(MONO REQUIRED mono-2)

target_include_directories(mono_test SYSTEM PRIVATE ${MONO_INCLUDE_DIRS})

target_link_libraries(mono_test ${MONO_LIBRARIES})

I've been reading different tutorials and trying different things all day (including recompiling mono from source) without any success.我整天都在阅读不同的教程并尝试不同的事情(包括从源代码重新编译 mono),但没有任何成功。 I have no experience with embedding mono and don't really know how to interpret the crash report or debug it.我没有嵌入 mono 的经验,也不知道如何解释崩溃报告或调试它。 Any help would be greatly appreciated.任何帮助将不胜感激。

In case anyone else lands here in the future, you have to call mono_set_assemblies_path() before mono_jit_init() :万一其他人将来登陆这里,您必须在mono_set_assemblies_path() mono_jit_init()

mono_set_assemblies_path("mono/lib");
MonoDomain* domain = mono_jit_init("MyApp");

And make SURE there's this.dll in "[working_dir]/mono/lib/mono/4.5/mscorlib.dll" , otherwise mono_jit_init() will immediately crash the application without any error or call stack.并确保"[working_dir]/mono/lib/mono/4.5/mscorlib.dll"中有 this.dll ,否则mono_jit_init()将立即使应用程序崩溃而没有任何错误或调用堆栈。

Here's a livestream of mono_jit_init() crashing: https://youtu.be/EKnJOwGhPj4?t=7099这是mono_jit_init()崩溃的直播: https://youtu.be/EKnJOwGhPj4?t=7099

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

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