Linker 如何用DMD编译/链接静态库
每当我使用DMD构建静态库时,我都能够将其链接到我的应用程序,并且它可以很好地编译,但在应用程序中的任何时候,该库都被称为“我得到”:Linker 如何用DMD编译/链接静态库,linker,d,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 ".
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>
程序接收信号SIGSEGV,分段故障。
在source/vibe/core/file.d:29处的vibe.core.file.openFile()(模式=,路径=…)中的0x00000000004554f5
29返回getEventDriver().openFile(路径、模式);
(gdb)回溯
#0 0x00000000004554f5,位于/vibe/core/file.d:29的vibe.core.file.openFile()中(模式=,路径=…)
#1 0x000000000044f7d2,位于/vibe/inet/path.d:24的vibe.core.file.openFile()中(模式=,路径=…)
#测试时,D main()中的2 0x000000000044f539。D:5
#rt.dmain2.main()中的3 0x000000000046b9e4()
#rt.dmain2.main()中的4 0x000000000046b35e()
#rt.dmain2.main()中的5 0x000000000046ba2b()
#rt.dmain2.main()中的6 0x000000000046b35e()
#7 0x000000000046b2e9在主()
(gdb)fram 2
#测试时,D main()中的2 0x000000000044f539。D:5
5 openFile(“test.d”,FileMode.Read);
(gdb)第1帧
#1 0x000000000044f7d2,位于/vibe/inet/path.d:24的vibe.core.file.openFile()中(模式=,路径=…)
24结构路径{
(gdb)打印模式
$1 =
为什么不使用dmd-lib-oflibmylib.a文件1.d…
你能做的最好的事情就是运行GDB,看看你的应用程序为什么会出现SEGFULT。如果你看到SEGFULT的原因是从你的库中取消引用函数指针,那么很可能你是对的,链接出了问题
如果您不熟悉GDB,这里有一篇简单的文章介绍如何使用它:
还有一篇关于这个话题的好文章
下面是一个完整的课程,介绍如何从两个文件中编译库libdstlib.a
,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);
}
嗯,它可能与链接或编译无关。分段错误表示您的程序在内存访问方面有问题。Wikipedia链接。您还应该删除gcc标记,因为您的问题与它无关。@undu错误链接的应用程序将分段错误(当取消对函数指针的排序时)@在DMD的链接部分,直接从dlang.org下载undu:“实际的链接是通过运行gcc完成的。”我体验了Wiki4D链接的不同方法,并在原始问题中添加了gdb输出。感谢您的帮助。