2

背景/问题描述 我正在尝试将 c# 脚本作为我在 Debian-9 Linux 中编译的 c++ 应用程序的一部分运行。但是,我的应用程序因来自单声道运行时的错误而崩溃。

我已经从 Debian 存储库安装了单声道,甚至尝试使用从源代码编译的单声道版本进行相同的实验。c# 代码,我已经使用mcs -t:library *.cs. 尝试运行我的应用程序时,它崩溃并出现以下错误:

* 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 ....

最小示例 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# 代码(hello.cs):

using System;

namespace Test {
    class Hello {

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

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

    }
}

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})

我整天都在阅读不同的教程并尝试不同的事情(包括从源代码重新编译单声道),但没有任何成功。我没有嵌入单声道的经验,也不知道如何解释崩溃报告或调试它。任何帮助将不胜感激。

4

0 回答 0