2012-11-05 52 views
3

每當我建立使用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> 
+0

好,它有可能是無關的鏈接或編譯。分段錯誤表明您的程序存在內存訪問問題。維基百科鏈接http://en.wikipedia.org/wiki/Segmentation_fault。你也應該刪除gcc標籤,因爲你的問題與它沒有關係。 – undu

+0

@undu錯誤地鏈接的應用程序將segfault(當延遲函數指針時) –

+0

@undu直接從DMD的鏈接中的dlang.org部分:「實際的鏈接是通過運行gcc完成的。」 –

回答

2

你爲什麼不使用dmd -lib -oflibmylib.a file1.d ...

你可以做的最好的事情就是運行GDB,看看爲什麼你的應用程序存在段錯誤。如果您發現段錯誤的原因是從庫中取消引用函數指針,那麼很可能您是正確的,並且鏈接出了問題。

如果你不熟悉GDB,這裏是一個簡單的文章如何做到這一點:http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

關於此主題,還有一篇關於Wiki4D的文章。

這裏是整個會話如何編譯庫libdstlib.a出來的兩個文件dstlib/foo.ddstdlib/bar.d

[email protected]:~/work/dstlib> ls -R 
.: 
driver.d dstlib 

./dstlib: 
bar.d foo.d 
[email protected]:~/work/dstlib> dmd -lib -oflibdstlib.a dstlib/*.d 
[email protected]:~/work/dstlib> dmd driver.d libdstlib.a 
[email protected]:~/work/dstlib> ./driver 
w: 80, h: 40 
[email protected]:~/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); 
} 
+0

我遇到了來自Wiki4D鏈接的不同方法,並在原始問題中添加了gdb輸出。謝謝您的幫助。 –