[英]Creating FFI between C and my language
Let's say that I have implemented a programming language, (we'll call it A for now). 假设我已经实现了一种编程语言(现在将其称为A )。 A is pretty similiar to C.
A与C非常相似。
I want my users to be able to access functions and data structures from already-existing C libraries. 我希望我的用户能够从已经存在的C库中访问函数和数据结构。 Is this possible?
这可能吗? If it is, how would a naive implementation look ?
如果是这样,天真的实现会是什么样子?
A 一种
This largely depends on what sort of language A is. 这在很大程度上取决于A语言的种类。
If it is a compiled language, then you need to produce appropriate assembler code (look up "calling conventions", particularly the C calling convention) to call the appropriate C function. 如果是编译语言,则需要生成适当的汇编代码(查找“调用约定”,尤其是C调用约定)以调用适当的C函数。 If you're using LLVM, you can do this fairly easily by using the
declare
and call
statements. 如果您使用的是LLVM,则可以通过使用
declare
和call
语句轻松地做到这一点。 After you've done that, you'll need to link the executable against the C library in question. 完成此操作后,您需要将可执行文件链接到相关的C库。
If it is an interpreted language, however, then you'll need to dynamically load the library. 但是,如果它是一种解释性语言,则需要动态加载该库。 How you do this is platform-specific: for instance, on unix-type systems, this can be achieved via the
dlopen
function. 具体操作方式是特定于平台的:例如,在UNIX类型的系统上,可以通过
dlopen
函数来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.