3

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>
4

1 に答える 1

2

なぜあなたは使わないのdmd -lib -oflibmylib.a file1.d ...ですか?

最善の方法は、GDB を実行して、アプリケーションのセグメンテーション違反の原因を確認することです。segfault の理由がライブラリからの関数ポインタの逆参照であることがわかった場合は、その考えは正しく、リンクで何か問題が発生した可能性が高いです。

GDB に慣れていない場合は、簡単な記事http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.htmlを参照してください。

このトピックに関するWiki4Dの素晴らしい記事もあります。

libdstlib.a以下は、 2 つのファイルdstlib/foo.dからライブラリをコンパイルする方法のセッション全体dstdlib/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);
}
于 2012-11-06T09:25:20.490 に答える