聊一聊 C# NativeAOT 多平台下的函数导出

// 动态加载库 int main() {

void *handle = dlopen("./Example_20_1_1.so", RTLD_LAZY);

if (!handle)

{
    fprintf(stderr, "Error: %s\n", dlerror());
    return 1;
}

// 获取函数指针

int (*ComplexCalculation)(int, int, const char *) =
    (int (*)(int, int, const char *))dlsym(handle, "ComplexCalculation");

if (!ComplexCalculation)

{
    fprintf(stderr, "Error: %s\n", dlerror());
    dlclose(handle);
    return 1;
}

// 调用函数

int result = ComplexCalculation(10, 20, "double");
printf("Result: %d\n", result);

dlclose(handle); // 关闭句柄

return 0;

} “` 使用 vscode 远程调试,哈哈,得到了我们想要的结果,截图如下:

三:总结

这篇我们演示了 windows 上的 C# 调用 C# AOT 及 linux 上的 C 调用 C# AOT,是不是挺有意思,也算是给训练营学员提供的一份资料参考。