【发布时间】:2017-08-09 08:14:24
【问题描述】:
我正在编写一个示例以使用 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);
}
}
cpp:
#include <iostream>
using namespace std;
int Sayhi(){
cout<<"hello world from cpp!"<<endl;
return 1;
}
如果我运行 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) in /home/xxx/invoke/Program.cs:line 17
我认为编译器可能会更改函数的名称,因此无法找到它。如何解决?
【问题讨论】:
-
看起来像name mangling 在行动。检查this