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

时间:2021-12-14 15:01:14

Whenever I build a static library using DMD, I'm able to link it to my application and it compiles fine but anytime in the application the library is called I get:

每当我使用DMD构建一个静态库时,我就可以将它链接到我的应用程序,它可以很好地编译,但是在应用程序中任何时候这个库被称为:

Segmentation fault (core dumped)

For building the library I do

为了建图书馆,我做了

# $(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)

and for building the application

并用于构建应用程序

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

What am I doing wrong?

我做错了什么?


Update

更新

Compiling vibe.d as libvibe.d-dmd.a

编译氛围。d libvibe.d-dmd.a

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

Example code:

示例代码:

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

Compiling the example

编译示例

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

And some gdb output:

和一些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>

1 个解决方案

#1


2  

Why don't you use the dmd -lib -oflibmylib.a file1.d ...?

你为什么不用dmd -lib -oflibmylib。file1。d……?

The best thing you can do is to run GDB, and see why your application segfaults. If you see that the reason for segfault is dereferencing a function pointer from your library then it is most likely you are right and something went wrong with linking.

您可以做的最好的事情是运行GDB,并查看应用程序分段错误的原因。如果您看到了segfault的原因是从您的库中取消了一个函数指针,那么您很可能是对的,链接出现了问题。

If you are not familiar with GDB, here is an easy article how to do it: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html .

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

There is also a nice article on Wiki4D about this topic.

关于这个话题,维基4d也有一篇不错的文章。

Here is a whole session how to compile a library libdstlib.a out of two files dstlib/foo.d and dstdlib/bar.d:

下面是如何编译libdstlib库的整个会话。两个文件中有一个是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);
}

#1


2  

Why don't you use the dmd -lib -oflibmylib.a file1.d ...?

你为什么不用dmd -lib -oflibmylib。file1。d……?

The best thing you can do is to run GDB, and see why your application segfaults. If you see that the reason for segfault is dereferencing a function pointer from your library then it is most likely you are right and something went wrong with linking.

您可以做的最好的事情是运行GDB,并查看应用程序分段错误的原因。如果您看到了segfault的原因是从您的库中取消了一个函数指针,那么您很可能是对的,链接出现了问题。

If you are not familiar with GDB, here is an easy article how to do it: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html .

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

There is also a nice article on Wiki4D about this topic.

关于这个话题,维基4d也有一篇不错的文章。

Here is a whole session how to compile a library libdstlib.a out of two files dstlib/foo.d and dstdlib/bar.d:

下面是如何编译libdstlib库的整个会话。两个文件中有一个是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);
}