20

我已通读本教程中的“C 绑定”,但我是 C 方面的新手。

有人可以让我知道是否可以将 Crystal 程序构建为要链接的静态库,如果可以,请提供一个简单的示例?

4

1 回答 1

29

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

无论如何,这是一个简单的例子:

记录器.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

记录器.h

#ifndef _CRYSTAL_LOGGER_H
#define _CRYSTAL_LOGGER_H

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

主程序

#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_

Alright, time to compile our C program

# 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

Or the static version

# 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

With much inspiration from https://gist.github.com/3bd3aadd71db206e828f

于 2015-10-03T10:17:33.193 回答