Thread overview
Creating a shared library under Linux?
Jul 27, 2012
Jens Mueller
Jul 27, 2012
Jens Mueller
Jul 27, 2012
Mike Wey
Jul 29, 2012
Jens Mueller
Jul 29, 2012
Mike Wey
July 27, 2012
Hi,

I've read several threads that about creating shared libraries. But I
could not make it work.
Does anybody know how to create a shared library? I don't care which
compiler I have to use to accomplish it.

Jens
July 27, 2012
On 27-07-2012 07:23, Jens Mueller wrote:
> Hi,
>
> I've read several threads that about creating shared libraries. But I
> could not make it work.
> Does anybody know how to create a shared library? I don't care which
> compiler I have to use to accomplish it.
>
> Jens
>

Well, all three compilers (DMD, GDC, LDC) can create shared libraries at this point. But DMD doesn't actually ship libphobos2 (druntime+phobos) as a shared library yet, so this is not going to work out so well. If, however, you don't use stock libphobos2, you can pass -shared -fPIC to create a shared library.

GDC and LDC, as far as I know, can both create shared libraries. GDC does it with the standard options you'd pass to GCC. LDC's command line is very similar to DMD's if memory serves.

But (another one!): druntime is not yet ready to handle shared libraries properly. This means that exceptions thrown across library boundaries, TLS, etc are not likely to work properly.

So, in short: If you're using stock libphobos2, don't bother. If you're not using stock libphobos2, you can probably trivially create shared libraries.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
July 27, 2012
Alex Rønne Petersen wrote:
> On 27-07-2012 07:23, Jens Mueller wrote:
> >Hi,
> >
> >I've read several threads that about creating shared libraries. But I
> >could not make it work.
> >Does anybody know how to create a shared library? I don't care which
> >compiler I have to use to accomplish it.
> >
> >Jens
> >
> 
> Well, all three compilers (DMD, GDC, LDC) can create shared libraries at this point. But DMD doesn't actually ship libphobos2 (druntime+phobos) as a shared library yet, so this is not going to work out so well. If, however, you don't use stock libphobos2, you can pass -shared -fPIC to create a shared library.

But I can build druntime+phobos as shared libraries and then it should work with dmd?

> GDC and LDC, as far as I know, can both create shared libraries. GDC does it with the standard options you'd pass to GCC. LDC's command line is very similar to DMD's if memory serves.

I tried both gdc and ldc but failed. I will write more specifically what I did later.

> But (another one!): druntime is not yet ready to handle shared libraries properly. This means that exceptions thrown across library boundaries, TLS, etc are not likely to work properly.

I think I can avoid all of these limitations.

> So, in short: If you're using stock libphobos2, don't bother. If you're not using stock libphobos2, you can probably trivially create shared libraries.

I see. Then I will try to build a shared libphobos2 first. Many thanks.

Jens
July 27, 2012
On 27-07-2012 09:31, Jens Mueller wrote:
> Alex Rønne Petersen wrote:
>> On 27-07-2012 07:23, Jens Mueller wrote:
>>> Hi,
>>>
>>> I've read several threads that about creating shared libraries. But I
>>> could not make it work.
>>> Does anybody know how to create a shared library? I don't care which
>>> compiler I have to use to accomplish it.
>>>
>>> Jens
>>>
>>
>> Well, all three compilers (DMD, GDC, LDC) can create shared
>> libraries at this point. But DMD doesn't actually ship libphobos2
>> (druntime+phobos) as a shared library yet, so this is not going to
>> work out so well. If, however, you don't use stock libphobos2, you
>> can pass -shared -fPIC to create a shared library.
>
> But I can build druntime+phobos as shared libraries and then it should
> work with dmd?

No, exceptions and TLS will still be broken, and the runtime doesn't know how to deal with shared libraries as they are loaded, so the GC might break in funny ways.

>
>> GDC and LDC, as far as I know, can both create shared libraries. GDC
>> does it with the standard options you'd pass to GCC. LDC's command
>> line is very similar to DMD's if memory serves.
>
> I tried both gdc and ldc but failed. I will write more specifically what
> I did later.
>
>> But (another one!): druntime is not yet ready to handle shared
>> libraries properly. This means that exceptions thrown across library
>> boundaries, TLS, etc are not likely to work properly.
>
> I think I can avoid all of these limitations.

Well, so long as you don't use exceptions and TLS, it might work. But I wouldn't recommend relying on it.

>
>> So, in short: If you're using stock libphobos2, don't bother. If
>> you're not using stock libphobos2, you can probably trivially create
>> shared libraries.
>
> I see. Then I will try to build a shared libphobos2 first.
> Many thanks.
>
> Jens
>


-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
July 27, 2012
On 07/27/2012 09:33 AM, Alex Rønne Petersen wrote:
> On 27-07-2012 09:31, Jens Mueller wrote:
>> Alex Rønne Petersen wrote:
>>> On 27-07-2012 07:23, Jens Mueller wrote:
>>>> Hi,
>>>>
>>>> I've read several threads that about creating shared libraries. But I
>>>> could not make it work.
>>>> Does anybody know how to create a shared library? I don't care which
>>>> compiler I have to use to accomplish it.
>>>>
>>>> Jens
>>>>
>>>
>>> Well, all three compilers (DMD, GDC, LDC) can create shared
>>> libraries at this point. But DMD doesn't actually ship libphobos2
>>> (druntime+phobos) as a shared library yet, so this is not going to
>>> work out so well. If, however, you don't use stock libphobos2, you
>>> can pass -shared -fPIC to create a shared library.
>>
>> But I can build druntime+phobos as shared libraries and then it should
>> work with dmd?
>
> No, exceptions and TLS will still be broken, and the runtime doesn't
> know how to deal with shared libraries as they are loaded, so the GC
> might break in funny ways.
>
>>
>>> GDC and LDC, as far as I know, can both create shared libraries. GDC
>>> does it with the standard options you'd pass to GCC. LDC's command
>>> line is very similar to DMD's if memory serves.
>>
>> I tried both gdc and ldc but failed. I will write more specifically what
>> I did later.
>>
>>> But (another one!): druntime is not yet ready to handle shared
>>> libraries properly. This means that exceptions thrown across library
>>> boundaries, TLS, etc are not likely to work properly.
>>
>> I think I can avoid all of these limitations.
>
> Well, so long as you don't use exceptions and TLS, it might work. But I
> wouldn't recommend relying on it.
>
>>
>>> So, in short: If you're using stock libphobos2, don't bother. If
>>> you're not using stock libphobos2, you can probably trivially create
>>> shared libraries.
>>
>> I see. Then I will try to build a shared libphobos2 first.
>> Many thanks.
>>
>> Jens
>>
>
>


I've successfully build GtkD as a shared library using LDC using the following steps:

Compiling the source files with fPIC:
ldc -O -m64 -relocation-model=pic -Isrc -c (source file)

Combine all the GtkD object file into one big object file:
ld -r (all GtkD object files) -o output.o

Create the shared lib using LDC:
ldc -shared output.so -L-soname=(so version) output.o

-- 
Mike Wey


July 29, 2012
Mike Wey wrote:
> On 07/27/2012 09:33 AM, Alex Rønne Petersen wrote:
> >On 27-07-2012 09:31, Jens Mueller wrote:
> >>Alex Rønne Petersen wrote:
> >>>On 27-07-2012 07:23, Jens Mueller wrote:
> >>>>Hi,
> >>>>
> >>>>I've read several threads that about creating shared libraries. But I
> >>>>could not make it work.
> >>>>Does anybody know how to create a shared library? I don't care which
> >>>>compiler I have to use to accomplish it.
> >>>>
> >>>>Jens
> >>>>
> >>>
> >>>Well, all three compilers (DMD, GDC, LDC) can create shared libraries at this point. But DMD doesn't actually ship libphobos2 (druntime+phobos) as a shared library yet, so this is not going to work out so well. If, however, you don't use stock libphobos2, you can pass -shared -fPIC to create a shared library.
> >>
> >>But I can build druntime+phobos as shared libraries and then it should work with dmd?
> >
> >No, exceptions and TLS will still be broken, and the runtime doesn't know how to deal with shared libraries as they are loaded, so the GC might break in funny ways.
> >
> >>
> >>>GDC and LDC, as far as I know, can both create shared libraries. GDC does it with the standard options you'd pass to GCC. LDC's command line is very similar to DMD's if memory serves.
> >>
> >>I tried both gdc and ldc but failed. I will write more specifically what I did later.
> >>
> >>>But (another one!): druntime is not yet ready to handle shared libraries properly. This means that exceptions thrown across library boundaries, TLS, etc are not likely to work properly.
> >>
> >>I think I can avoid all of these limitations.
> >
> >Well, so long as you don't use exceptions and TLS, it might work. But I wouldn't recommend relying on it.
> >
> >>
> >>>So, in short: If you're using stock libphobos2, don't bother. If you're not using stock libphobos2, you can probably trivially create shared libraries.
> >>
> >>I see. Then I will try to build a shared libphobos2 first. Many thanks.
> >>
> >>Jens
> >>
> >
> >
> 
> 
> I've successfully build GtkD as a shared library using LDC using the following steps:
> 
> Compiling the source files with fPIC:
> ldc -O -m64 -relocation-model=pic -Isrc -c (source file)
> 
> Combine all the GtkD object file into one big object file:
> ld -r (all GtkD object files) -o output.o
> 
> Create the shared lib using LDC:
> ldc -shared output.so -L-soname=(so version) output.o

Thanks a lot.
This does work if the last command is changed to
ldc2 -shared -of=output.so -L-soname=1 output.o
and ldc was built using -DBUILD_SHARED_LIBS=ON.

Jens
July 29, 2012
On 07/29/2012 02:34 PM, Jens Mueller wrote:
> Mike Wey wrote:
>> I've successfully build GtkD as a shared library using LDC using the
>> following steps:
>>
>> Compiling the source files with fPIC:
>> ldc -O -m64 -relocation-model=pic -Isrc -c (source file)
>>
>> Combine all the GtkD object file into one big object file:
>> ld -r (all GtkD object files) -o output.o
>>
>> Create the shared lib using LDC:
>> ldc -shared output.so -L-soname=(so version) output.o
>
> Thanks a lot.
> This does work if the last command is changed to
> ldc2 -shared -of=output.so -L-soname=1 output.o
> and ldc was built using -DBUILD_SHARED_LIBS=ON.
>
> Jens
>


I looks like i didn't get the soname quite right, it should have been:
ldc -shared output.so -L-soname=output.so.(so version) output.o


the soname is the name of the library ld will try to load at program startup. if you are not going to use to typical linux approach of setting up symlinks to get both a versioned and a unversioned lib in the filesystem. Then you don't have to specify a somane as the default soname is the name of the lib.

-- 
Mike Wey