【发布时间】:2016-02-04 16:37:53
【问题描述】:
我想从 C 程序中调用使用标准 D 库的 D 函数,我该如何在 linux 中调用?
静态链接似乎不起作用(我得到了可怕的“未定义的对`_Dmodule_ref'的引用”以及460个其他错误,即使还链接了一个D主函数),所以我试图按照说明https://dlang.org/dll-linux.html。感谢 Ian Abbott 对说明的帮助。我已将它们提炼为以下工作的最小 hello world 示例:
mkdir -p /tmp/dlib
cd /tmp/dlib
cat ->hello_d.d <<EOF
import core.stdc.stdio;
extern(C) void hello_d() {
printf( "hello from d\n");
}
EOF
cat ->main.c <<EOF
#include <stdlib.h>
#include <dlfcn.h>
int main() {
void *lh = dlopen( "/tmp/dlib/libhello.so", RTLD_LAZY);
if( !lh) exit( 1);
void (*hello_d)() = dlsym( lh, "hello_d");
if( dlerror()) exit( 2);
(*hello_d)();
dlclose(lh);
}
EOF
dmd -c hello_d.d -fPIC
dmd -oflibhello.so hello_d.o -shared -defaultlib=libphobos2.so -L-rpath=/tmp/dlib
gcc -c main.c
gcc -rdynamic main.o -o main -ldl
./main
# Expect "hello from d"
cd -
但是指令依赖于 core.stdc.stdio 和 c printf 函数,但我想使用 d 库 std.stdio 和 d writeln 函数。如果我这样做,我会在运行主程序时遇到分段错误。 请告诉我如何将 d 函数(利用标准 d 库)链接到 c 程序中。
【问题讨论】:
-
“/home/walter/tmp/libdll.so”只是您之前链接到的说明中构建的示例共享库。
-
当我在 Ubuntu 14.04 上使用 dmd v2.068.0 尝试时,您的示例有效。
-
我确认该示例适用于 Archlinux,DMD v2.070.0
标签: c linux linker shared-libraries d