Jak*_*ake 5 static-binding crystal-lang
我想弄清楚晶体中的c绑定是如何工作的.对于初学者,我想知道如何在水晶中加入一个简单的hello world c函数.总是很好从基础知识开始吧?这是我想要包含的功能:
#include <stdio.h>
void hello(const char * name){
printf("Hello %s!\n", name);
}
Run Code Online (Sandbox Code Playgroud)
isa*_*oan 10
这让我有点想弄清楚.首先,您必须将C文件编译为对象.在gcc你会跑gcc -c hello.c -o hello.o.
然后在水晶文件中,您需要链接到C对象.这是一个例子:
#hello.cr
@[Link(ldflags: "#{__DIR__}/hello.o")]
lib Say
fun hello(name : LibC::Char*) : Void
end
Say.hello("your name")
Run Code Online (Sandbox Code Playgroud)
现在你只需编译你的水晶应用程序就可以了.
crystal build hello.cr