September 27, 2012
You can build shared libraries on linux by manually compiling object files and linking them. On windows last time I tries it was not possible.
September 27, 2012
Maxim Fomin wrote:
> You can build shared libraries on linux by manually compiling object files and linking them. On windows last time I tries it was not possible.

Can you give detailed steps for doing this on Linux? Because nobody as far as I know has made this work yet?

Jens
September 27, 2012
Hi,

I have same issue, but it is possible make shared library,
first of all you have to make shared variant of druntime and phobos library, than it should work ok.

Now I am at work, when I come back home I will post some more details about this.

Daniel Kozak

On Wednesday, 26 September 2012 at 17:57:29 UTC, Andrei Alexandrescu wrote:
> Haven't done any dynamic linking with D and I need to. I'm using dmd 2.058/Linux at work to build and use dynamic libraries. Here's my attempt:
>
> *** Makefile
> all: main lib.so
>
> main: main.d
> 	dmd main
>
> lib.so: lib.d
> 	dmd -fPIC -shared lib.d -of./lib.so
>
> *** lib.d
> extern(C) int fun(string s)
> {
>     return 42;
> }
>
> *** main.d
> import std.stdio;
> void main()
> {
> }
>
> Running make prints:
>
> dmd -fPIC -shared lib.d -of./lib.so
> /usr/bin/ld: /mnt/vol/engshare/third-party/centos5.2-native/dmd/dmd-2.058-centos5.2-native/bin/../../../../centos5.2-native/phobos/phobos-2.058/ffad884/generated/linux/release/64/libphobos2.a(minfo.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
> /mnt/vol/engshare/third-party/centos5.2-native/dmd/dmd-2.058-centos5.2-native/bin/../../../../centos5.2-native/phobos/phobos-2.058/ffad884/generated/linux/release/64/libphobos2.a: could not read symbols: Bad value
> collect2: ld returned 1 exit status
>
> What steps do I need to take to get off the ground?
>
>
> Thanks,
>
> Andrei


September 27, 2012
Now I try it, and it is not required to build shared variant of druntime and phobos, only rebuild it with -fPIC



On Thursday, 27 September 2012 at 06:12:38 UTC, Daniel Kozak wrote:
> Hi,
>
> I have same issue, but it is possible make shared library,
> first of all you have to make shared variant of druntime and phobos library, than it should work ok.
>
> Now I am at work, when I come back home I will post some more details about this.
>
> Daniel Kozak
>
> On Wednesday, 26 September 2012 at 17:57:29 UTC, Andrei Alexandrescu wrote:
>> Haven't done any dynamic linking with D and I need to. I'm using dmd 2.058/Linux at work to build and use dynamic libraries. Here's my attempt:
>>
>> *** Makefile
>> all: main lib.so
>>
>> main: main.d
>> 	dmd main
>>
>> lib.so: lib.d
>> 	dmd -fPIC -shared lib.d -of./lib.so
>>
>> *** lib.d
>> extern(C) int fun(string s)
>> {
>>    return 42;
>> }
>>
>> *** main.d
>> import std.stdio;
>> void main()
>> {
>> }
>>
>> Running make prints:
>>
>> dmd -fPIC -shared lib.d -of./lib.so
>> /usr/bin/ld: /mnt/vol/engshare/third-party/centos5.2-native/dmd/dmd-2.058-centos5.2-native/bin/../../../../centos5.2-native/phobos/phobos-2.058/ffad884/generated/linux/release/64/libphobos2.a(minfo.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
>> /mnt/vol/engshare/third-party/centos5.2-native/dmd/dmd-2.058-centos5.2-native/bin/../../../../centos5.2-native/phobos/phobos-2.058/ffad884/generated/linux/release/64/libphobos2.a: could not read symbols: Bad value
>> collect2: ld returned 1 exit status
>>
>> What steps do I need to take to get off the ground?
>>
>>
>> Thanks,
>>
>> Andrei


September 27, 2012
On Wednesday, 26 September 2012 at 20:15:35 UTC, nazriel wrote:
> On Wednesday, 26 September 2012 at 20:10:47 UTC, Michael wrote:
>>> Thanks. The loading part is very useful, but I'm still lost when it comes to build the shared library itself.
>>>
>>> Andrei
>>
>> Program loads dll at runtime using loader which is configured to load concrete dll file(s). Like in gtkD http://www.dsource.org/projects/gtkd/browser/trunk/src/gtkc/Loader.d
>
> Loading Shared lib isn't big issues here.
>
> The bigger one is building Shared library (written in D) and running it in host application without issues (EH, shared GC etc).
>
> Andrei, if you find out how to make those things work, please share your findings. I'm also in need of using shared libraries.
>
> And yeah, probably Martin Nowak will be the best bet to get information from.

Native Oberon/BlueBottle OS really a lot in dynamic modules, as everything
is dynamically loaded. As far as I know both operating systems have a global GC.

Not sure how Spin does it.

Singularity, uses local GC per process and does not support dynamic loading.

Bringing this into D's context, I imagine a solution for the GC would be to have it shared across all modules. The biggest problem I see, is that all shared libraries have to use the same D compiler, or a standard GC ABI needs to be defined.

--
Paulo



September 27, 2012
Am Thu, 27 Sep 2012 08:26:36 +0200
schrieb "Daniel Kozak" <kozzi11@gmail.com>:

> Now I try it, and it is not required to build shared variant of druntime and phobos, only rebuild it with -fPIC
> 

In the end you'll probably need a shared druntime & phobos: Let's say your main app doesn't use std.datetime and you statically link libphobos. Then the linker might strip std.datetime from your executable. If your shared library now needs std.datetime it won't work.

The funny part is that on ARM+GDC we can at least build druntime & phobos as a shared library, but on x86/64 last time I checked it didn't work because of non-PIC assembly.

https://bitbucket.org/goshawk/gdc/issue/166/add-shared-lib-support
September 27, 2012
On Thursday, 27 September 2012 at 05:52:44 UTC, Jens Mueller
wrote:
> Maxim Fomin wrote:
>> You can build shared libraries on linux by manually compiling object
>> files and linking them. On windows last time I tries it was not
>> possible.
>
> Can you give detailed steps for doing this on Linux? Because nobody as
> far as I know has made this work yet?
>
> Jens

Dpaste seems not working, so, sorry for code

----lib.d---
import std.stdio;

static this()
{
	writeln("module ctor");
}

static ~this()
{
	writeln("module dtor");
}

class A
{
	private string text;;
	this(string text)
	{
		writeln("class ctor");
		this.text = text;
	}
	void tell()
	{
		writeln(this.text);
	}
	~this()
	{
		writeln(this.text);
		writeln("dtor");
	}
	static this()
	{
		writeln("static ctor");
	}
	static ~this()
	{
		writeln("static dtor");
	}
}
---------------
-----main.d----
import lib;

void main()
{
	auto a = new A("some text");
	a.tell();
}
---------------

dmd -c -fPIC lib.d
gcc -shared lib.o -o liblib.so
dmd -c main.d
gcc main.o -llib -lphobos2 -lrt -lpthread -L. -Wl,-rpath=.
./a.out
ldd a.out
	linux-vdso.so.1 (0x00007fff703ff000)
	liblib.so => ./liblib.so (0x00007f48158f1000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f48156cd000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f48154b1000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f481510c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4815af4000)

September 27, 2012
On 2012-09-27 10:04, Maxim Fomin wrote:
> On Thursday, 27 September 2012 at 05:52:44 UTC, Jens Mueller
> wrote:
>> Maxim Fomin wrote:
>>> You can build shared libraries on linux by manually compiling object
>>> files and linking them. On windows last time I tries it was not
>>> possible.
>>
>> Can you give detailed steps for doing this on Linux? Because nobody as
>> far as I know has made this work yet?
>>
>> Jens
>
> Dpaste seems not working, so, sorry for code
>
> ----lib.d---
> import std.stdio;
>
> static this()
> {
>      writeln("module ctor");
> }
>
> static ~this()
> {
>      writeln("module dtor");
> }
>
> class A
> {
>      private string text;;
>      this(string text)
>      {
>          writeln("class ctor");
>          this.text = text;
>      }
>      void tell()
>      {
>          writeln(this.text);
>      }
>      ~this()
>      {
>          writeln(this.text);
>          writeln("dtor");
>      }
>      static this()
>      {
>          writeln("static ctor");
>      }
>      static ~this()
>      {
>          writeln("static dtor");
>      }
> }
> ---------------
> -----main.d----
> import lib;
>
> void main()
> {
>      auto a = new A("some text");
>      a.tell();
> }
> ---------------
>
> dmd -c -fPIC lib.d
> gcc -shared lib.o -o liblib.so
> dmd -c main.d
> gcc main.o -llib -lphobos2 -lrt -lpthread -L. -Wl,-rpath=.
> ./a.out
> ldd a.out
>      linux-vdso.so.1 (0x00007fff703ff000)
>      liblib.so => ./liblib.so (0x00007f48158f1000)
>      librt.so.1 => /lib64/librt.so.1 (0x00007f48156cd000)
>      libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f48154b1000)
>      libc.so.6 => /lib64/libc.so.6 (0x00007f481510c000)
>      /lib64/ld-linux-x86-64.so.2 (0x00007f4815af4000)

1. Does this actually run?

2. This is statically linked with druntime and Phobos. What happens when you create an executable that links with the D dynamic library?

Last time I tried this (on Mac OS X) I got several symbols missing. This was all symbols that are usually pointing to the executable, inserted by the compiler. One of them would be "main" and symbols like these:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/deh2.d#L27

-- 
/Jacob Carlborg
September 27, 2012
On Thursday, 27 September 2012 at 08:26:08 UTC, Jacob Carlborg wrote:
> 1. Does this actually run?
>

If it were non-runnable, I wouldn't posted it.

> 2. This is statically linked with druntime and Phobos. What happens when you create an executable that links with the D dynamic library?

Solution depends on a problem. I understood Andrei's post that he wanted a .so file or DLL. I told originally that it is possible to make shared libraries on linux. Now I see there is some misunderstanding. Is the problem in diving D application on executables and shared libraries or making druntime+phobos a shared library or loading a library at runtime? A was speaking about the first.

> Last time I tried this (on Mac OS X) I got several symbols missing. This was all symbols that are usually pointing to the executable, inserted by the compiler. One of them would be "main" and symbols like these:
>
> https://github.com/D-Programming-Language/druntime/blob/master/src/rt/deh2.d#L27

I have no idea about D support of shared libraries on Mac OS.
September 27, 2012
Jacob Carlborg wrote:
> On 2012-09-27 10:04, Maxim Fomin wrote:
> >On Thursday, 27 September 2012 at 05:52:44 UTC, Jens Mueller wrote:
> >>Maxim Fomin wrote:
> >>>You can build shared libraries on linux by manually compiling object files and linking them. On windows last time I tries it was not possible.
> >>
> >>Can you give detailed steps for doing this on Linux? Because nobody as far as I know has made this work yet?
> >>
> >>Jens
> >
> >Dpaste seems not working, so, sorry for code
> >
> >----lib.d---
> >import std.stdio;
> >
> >static this()
> >{
> >     writeln("module ctor");
> >}
> >
> >static ~this()
> >{
> >     writeln("module dtor");
> >}
> >
> >class A
> >{
> >     private string text;;
> >     this(string text)
> >     {
> >         writeln("class ctor");
> >         this.text = text;
> >     }
> >     void tell()
> >     {
> >         writeln(this.text);
> >     }
> >     ~this()
> >     {
> >         writeln(this.text);
> >         writeln("dtor");
> >     }
> >     static this()
> >     {
> >         writeln("static ctor");
> >     }
> >     static ~this()
> >     {
> >         writeln("static dtor");
> >     }
> >}
> >---------------
> >-----main.d----
> >import lib;
> >
> >void main()
> >{
> >     auto a = new A("some text");
> >     a.tell();
> >}
> >---------------
> >
> >dmd -c -fPIC lib.d
> >gcc -shared lib.o -o liblib.so
> >dmd -c main.d
> >gcc main.o -llib -lphobos2 -lrt -lpthread -L. -Wl,-rpath=.
> >./a.out
> >ldd a.out
> >     linux-vdso.so.1 (0x00007fff703ff000)
> >     liblib.so => ./liblib.so (0x00007f48158f1000)
> >     librt.so.1 => /lib64/librt.so.1 (0x00007f48156cd000)
> >     libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f48154b1000)
> >     libc.so.6 => /lib64/libc.so.6 (0x00007f481510c000)
> >     /lib64/ld-linux-x86-64.so.2 (0x00007f4815af4000)
> 
> 1. Does this actually run?

I just tried.
$ ./a.out
module ctor
static ctor
class ctor
some text
static dtor
module dtor
some text
dtor

> 2. This is statically linked with druntime and Phobos. What happens when you create an executable that links with the D dynamic library?

a.out is linked dynamically against liblib.so.

> Last time I tried this (on Mac OS X) I got several symbols missing. This was all symbols that are usually pointing to the executable, inserted by the compiler. One of them would be "main" and symbols like these:
> 
> https://github.com/D-Programming-Language/druntime/blob/master/src/rt/deh2.d#L27

I'm running Linux. You can test on Mac OS X.
I'm also astonished that this works. This is great.

Jens