linker - 如何使用 DMD 编译/链接静态库

标签 linker d dmd

每当我使用 DMD 构建静态库时,我都能够将它链接到我的应用程序并且编译正常,但是在应用程序中调用库的任何时候我都会得到:

Segmentation fault (core dumped)

为了构建我做的图书馆

# $(FILE) for each file in "find source -name "*.d"
# $(OBJ) is $(FILE) with the extension ".o"
# $(IMP) is $(FILE) with the extension ".di"
dmd -O -d -m64 -L-ldl -m64 -Isource -c $(FILE) -ofbuild/$(OBJ)

ar rcs ./lib/libvibe.d-dmd.a build/*
ranlib ./lib/libvibe.d-dmd.a

dmd -O -d -m64 -L-ldl -m64 -Isource -c -o- $(FILE) -Hfimport/$(IMP)

并用于构建应用程序

SRC = $(shell find src -name "*.d")
dmd -debug -odbuild -I../../vibe.d/source -L-L../../vibe.d/lib -L-lvibe.d-dmd $(SRC) -ofbin/test

我做错了什么?


更新

将 vibe.d 编译为 libvibe.d-dmd.a

dmd -g -lib -oflib/libvibe.d-dmd.a $(SOURCES) -L-levent_pthreads -L-levent -L-lssl -L-lcrypto

示例代码:

import vibe.core.file;
void main()
{
  openFile("test.d", FileMode.Read);
}

编译示例

dmd -g test.d vibe.d/lib/libvibe.d-dmd.a -Ivibe.d/source

还有一些 gdb 输出:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004554f5 in vibe.core.file.openFile() (mode=<incomplete type>, path=...) at source/vibe/core/file.d:29
29      return getEventDriver().openFile(path, mode);
(gdb) backtrace
#0  0x00000000004554f5 in vibe.core.file.openFile() (mode=<incomplete type>, path=...) at source/vibe/core/file.d:29
#1  0x000000000044f7d2 in vibe.core.file.openFile() (mode=<incomplete type>, path=...) at source/vibe/inet/path.d:24
#2  0x000000000044f539 in D main () at test.d:5
#3  0x000000000046b9e4 in rt.dmain2.main() ()
#4  0x000000000046b35e in rt.dmain2.main() ()
#5  0x000000000046ba2b in rt.dmain2.main() ()
#6  0x000000000046b35e in rt.dmain2.main() ()
#7  0x000000000046b2e9 in main ()
(gdb) fram 2
#2  0x000000000044f539 in D main () at test.d:5
5     openFile("test.d", FileMode.Read);
(gdb) frame 1
#1  0x000000000044f7d2 in vibe.core.file.openFile() (mode=<incomplete type>, path=...) at source/vibe/inet/path.d:24
24  struct Path {
(gdb) print mode
$1 = <incomplete type>

最佳答案

你为什么不使用 dmd -lib -oflibmylib.a file1.d ...

你能做的最好的事情就是运行 GDB,看看你的应用程序为什么会出现段错误。如果您发现段错误的原因是从您的库中取消引用函数指针,那么您很可能是对的,链接出了点问题。

如果您不熟悉 GDB,这里有一篇关于如何操作的简单文章:http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html .

Wiki4D 上也有一篇不错的文章关于这个话题。

这是一个完整的 session 如何编译一个库libdstlib.a两个文件中的dstlib/foo.ddstdlib/bar.d :

dejan@homeserver:~/work/dstlib> ls -R
.:
driver.d  dstlib

./dstlib:
bar.d  foo.d
dejan@homeserver:~/work/dstlib> dmd -lib -oflibdstlib.a dstlib/*.d
dejan@homeserver:~/work/dstlib> dmd driver.d libdstlib.a
dejan@homeserver:~/work/dstlib> ./driver
w: 80, h: 40
dejan@homeserver:~/work/dstlib> cat driver.d dstlib/foo.d dstlib/bar.d
module driver;
import std.stdio;
import dstlib.bar;

void main()
{
    auto rect = getRectangle();
    rect.display();
}    
module dstlib.foo;
import std.stdio : writefln;
struct Rectangle
{
    int width, height;
    void display()
    {
        writefln("w: %s, h: %s", width, height);
    }
}    
module dstlib.bar;
import dstlib.foo;

Rectangle getRectangle()
{
    return Rectangle(80, 40);
}

关于linker - 如何使用 DMD 编译/链接静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13235592/

相关文章:

d - 为什么 int x=08;当 int x=078 时有效;在 `DMD` 中无效?

D 编程语言 : module stdio cannot read file std\stdio. d

mixins - 从字符串获取类——通过字符串名称调用函数

C++ 将静态库链接到共享库

linux - LD_LIBRARY_PATH 不工作

D2 : std. algorithm.indexOf 不再起作用

d - 使用 DUB 构建 D 项目时如何忽略第 3 方包中的警告

d - 如何在 DUB 项目中定义包的版本

c - 链接中的重定位条目(C编程)

android - ar 命令行长度