我正在编写一个示例以使用 c# 代码调用 c++ 共享库中的某些函数。环境为 Ubuntu 16.04 和 .NET Core 2.0。
class Program
{
[DllImport("libc.so.6")]
private static extern int getpid();
[DllImport("/home/xxx/invoke/hi.so")]
private static extern int Sayhi();
static void Main(string[] args)
{
int pid= getpid();
Console.WriteLine(pid);
Console.WriteLine("Hello World!");
int status= Sayhi();
Console.WriteLine(status);
}
}
Run Code Online (Sandbox Code Playgroud)
中央人民政府:
#include <iostream>
using namespace std;
int Sayhi(){
cout<<"hello world from cpp!"<<endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行 c# 代码,我会收到错误消息:
Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point named 'Sayhi' in DLL '/home/xxx/invoke/hi.so'.
at invoke.Program.Sayhi()
at invoke.Program.Main(String[] args) …Run Code Online (Sandbox Code Playgroud)