可以从 C 静态链接到 Crystal 库吗?

Can a Crystal library be statically linked to from C?

我已经通读了教程中的 "C bindings",但我是 C 语言的新手。

有人可以告诉我是否可以将 Crystal 程序构建为 link 的静态库,如果可以,请提供一个简单的示例吗?

可以,但不建议这样做。 Crystal 依赖于 GC,这使得生成共享(或静态)库变得不太理想。因此,也没有语法级别的构造来帮助创建这样的内容,也没有简单的编译器调用来帮助创建这样的内容。文档中的 C 绑定部分是关于使用 C 编写的库可供 Crystal 程序使用。

这里有一个简单的例子:

logger.cr

fun init = crystal_init : Void
  # We need to initialize the GC
  GC.init

  # We need to invoke Crystal's "main" function, the one that initializes
  # all constants and runs the top-level code (none in this case, but without
  # constants like STDOUT and others the last line will crash).
  # We pass 0 and null to argc and argv.
  LibCrystalMain.__crystal_main(0, Pointer(Pointer(UInt8)).null)
end

fun log = crystal_log(text: UInt8*): Void
  puts String.new(text)
end

logger.h

#ifndef _CRYSTAL_LOGGER_H
#define _CRYSTAL_LOGGER_H

void crystal_init(void);
void crystal_log(char* text);
#endif

main.c

#include "logger.h"

int main(void) {
  crystal_init();
  crystal_log("Hello world!");
}

我们可以创建一个共享库

crystal build --single-module --link-flags="-shared" -o liblogger.so

或者静态库

crystal build logger.cr --single-module --emit obj
rm logger # we're not interested in the executable
strip -N main logger.o # Drop duplicated main from the object file
ar rcs liblogger.a logger.o

让我们确认一下我们的函数是否包含在内

nm liblogger.so | grep crystal_
nm liblogger.a | grep crystal_

好的,是时候编译我们的 C 程序了

# Folder where we can store either liblogger.so or liblogger.a but
# not both at the same time, so we can sure to use the right one
rm -rf lib
mkdir lib
cp liblogger.so lib
gcc main.c -o dynamic_main -Llib -llogger
LD_LIBRARY_PATH="lib" ./dynamic_main

或者静态版

# Folder where we can store either liblogger.so or liblogger.a but
# not both at the same time, so we can sure to use the right one
rm -rf lib
mkdir lib
cp liblogger.a lib
gcc main.c -o static_main -Llib -levent -ldl -lpcl -lpcre -lgc -llogger
./static_main

很多灵感来自https://gist.github.com/3bd3aadd71db206e828f