【发布时间】:2021-01-03 12:16:36
【问题描述】:
dotnet 运行错误定义here。我正在尝试使用 .Net core x64 Linux 在 C# 中通过 C 库 DllImport 运行汇编代码。如何正确地做到这一点?
C#:
using System.Runtime.InteropServices;
class Program{
[DllImport("lib.so")] public static extern int foo ();
static void Main(string[] args)
{
int code = foo();
System.Console.WriteLine(code);
}
}
Cmy.c:
#define EXPORT __attribute__((visibility("default")))
EXPORT int foo(void);
int foo(void)
{
extern int _start();
return _start();
}
大会asm.s:
.text
.globl _start
_start:
mov $1, %rax
ret
程序目标是通过 C 库从汇编返回零结果代码。打印以下错误消息;不会引发异常:
Hosting components are already initialized.
Re-initialization to execute an app is not allowed.
我已经使用 GCC 编译了 C 和汇编源代码:
gcc -shared -fpic -o lib.so my.c asm.s
【问题讨论】:
-
gcc 命令行错误:
-nostartfiles在编译 (-c) 时没有意义,但只有在链接时才有意义。而lib.so不包含asm.o。
标签: .net linux assembly .net-core x86-64