【发布时间】:2012-11-05 15:51:14
【问题描述】:
每当我使用 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>
【问题讨论】:
-
嗯,它可能与链接或编译无关。分段错误表明您的程序存在内存访问问题。维基百科链接en.wikipedia.org/wiki/Segmentation_fault。您还应该删除 gcc 标签,因为您的问题与它无关。
-
@undu 错误链接的应用程序将出现段错误(取消引用函数指针时)
-
@undu 直接来自 DMD 链接部分的 dlang.org:“实际链接是通过运行 gcc 完成的。”