| Thread overview | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 18, 2013 How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Is there a way to use a function from a static D library without importing their D sources nor their DI interface?
--
Jordi Sayol
| ||||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jordi Sayol | On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote: > Is there a way to use a function from a static D library without importing their D sources nor their DI interface? Yes you should be able to do it, but not everything can be imported without the source code, for example function templates are not included in the library object file unless you make the effort to wrap them into specific typed versions that will be compiled into the object file. If you don't have a di file for the library, you can always make one. See this related thread which is basically the same subject http://forum.dlang.org/thread/jhmgqvvlyrunleknkaxz@forum.dlang.org --rt | |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jordi Sayol | On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:
> Is there a way to use a function from a static D library without importing their D sources nor their DI interface?
lib.d:
extern(C) void printf(const char*, ...);
void foo() {
printf("%s".ptr, "hi".ptr);
}
test.d:
extern(C) void _D3lib3fooFZv();
void main() {
_D3lib3fooFZv();
}
Hehe.
Now, to be honest that is a good question. How to handle name mangling?
Maybe pragma(mangleOf, "") by Alex Petterson could help.
| |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to nazriel | On Friday, 18 January 2013 at 17:47:42 UTC, nazriel wrote:
> On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:
>> Is there a way to use a function from a static D library without importing their D sources nor their DI interface?
>
> lib.d:
>
> extern(C) void printf(const char*, ...);
>
> void foo() {
> printf("%s".ptr, "hi".ptr);
> }
>
> test.d:
>
> extern(C) void _D3lib3fooFZv();
>
> void main() {
> _D3lib3fooFZv();
> }
>
> Hehe.
>
> Now, to be honest that is a good question. How to handle name mangling?
> Maybe pragma(mangleOf, "") by Alex Petterson could help.
----lib.d----
extern(C) void printf(const char*, ...);
extern(C) void foo() {
printf("%s\n".ptr, "hi".ptr);
}
----main.d----
extern extern(C) void foo();
void main() {
foo();
}
--------------
# dmd mylib.d -lib -oflibmylib.a
# dmd main.d -L-lmylib -L-L.
This is possible if inside library non-member functions are marked as extern(C). Otherwise a .di file is needed.
| |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Friday, 18 January 2013 at 18:10:35 UTC, Maxim Fomin wrote:
> On Friday, 18 January 2013 at 17:47:42 UTC, nazriel wrote:
>> On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:
>>> Is there a way to use a function from a static D library without importing their D sources nor their DI interface?
>>
>> lib.d:
>>
>> extern(C) void printf(const char*, ...);
>>
>> void foo() {
>> printf("%s".ptr, "hi".ptr);
>> }
>>
>> test.d:
>>
>> extern(C) void _D3lib3fooFZv();
>>
>> void main() {
>> _D3lib3fooFZv();
>> }
>>
>> Hehe.
>>
>> Now, to be honest that is a good question. How to handle name mangling?
>> Maybe pragma(mangleOf, "") by Alex Petterson could help.
>
> ----lib.d----
> extern(C) void printf(const char*, ...);
>
> extern(C) void foo() {
> printf("%s\n".ptr, "hi".ptr);
> }
> ----main.d----
> extern extern(C) void foo();
>
> void main() {
> foo();
> }
> --------------
>
> # dmd mylib.d -lib -oflibmylib.a
> # dmd main.d -L-lmylib -L-L.
>
> This is possible if inside library non-member functions are marked as extern(C). Otherwise a .di file is needed.
Nice!
This should be mentioned at Language Reference, so it won't get lost.
| |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to nazriel | On 1/18/13, nazriel <spam@dzfl.pl> wrote: > extern(C) void _D3lib3fooFZv(); > > void main() { > _D3lib3fooFZv(); > } That's a *terrible* idea, you are calling a D function using the C convention, you're going to have all sorts of problems. extern(D) is not just used for mangling, it's also used for designating how parameters are passed to and results are returned from a function (via stack/registers). E.g.: import std.stdio; int foo(int x, int y) { return x + y; } alias extern(C) int function(int, int) Func; void main() { Func func = cast(Func)&foo; writeln(func(1, 2)); } $ rdmd test.d > object.Error: Access Violation | |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to nazriel | Am Fri, 18 Jan 2013 18:47:41 +0100 schrieb "nazriel" <spam@dzfl.pl>: > lib.d: > void foo() { > printf("%s".ptr, "hi".ptr); > } > > test.d: > extern(C) void _D3lib3fooFZv(); > I think this is dangerous, there's no guarantee that extern(D) equals extern(C). > How to handle name > mangling? > Maybe pragma(mangleOf, "") by Alex Petterson could help. extern(D) + pragma(mangleOf) could work. But it's probably simpler and safer to just write a .di file for that function manually: lib.di: module lib; void foo(); | |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to nazriel | Am Fri, 18 Jan 2013 19:17:33 +0100
schrieb "nazriel" <spam@dzfl.pl>:
> [...]
>
> Nice!
> This should be mentioned at Language Reference, so it won't get
> lost.
>
Isn't this documented? I thought it was well known that you can mark D functions as extern(C). It's needed when implementing callback functions for C, for example.
You also get all the problems of unmangled C names, I remember some nice segfaults in Derelict related to this (fixed some time ago in derelict.)
| |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Friday, 18 January 2013 at 18:18:07 UTC, Andrej Mitrovic wrote:
> On 1/18/13, nazriel <spam@dzfl.pl> wrote:
>> extern(C) void _D3lib3fooFZv();
>>
>> void main() {
>> _D3lib3fooFZv();
>> }
>
> That's a *terrible* idea, you are calling a D function using the C
> convention, you're going to have all sorts of problems. extern(D) is
> not just used for mangling, it's also used for designating how
> parameters are passed to and results are returned from a function (via
> stack/registers). E.g.:
>
Oh really?
Thanks, I didn't know.
| |||
January 18, 2013 Re: How to use a function without their sources | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | On Friday, 18 January 2013 at 18:23:03 UTC, Johannes Pfau wrote: > Am Fri, 18 Jan 2013 19:17:33 +0100 > schrieb "nazriel" <spam@dzfl.pl>: > >> [...] >> >> Nice! >> This should be mentioned at Language Reference, so it won't get lost. >> > > Isn't this documented? I thought it was well known that you can mark D > functions as extern(C). It's needed when implementing callback > functions for C, for example. > I mean, some kind of pointer how to achieve such thing (Question in first post). To be honest, I did think that there is some other way than using extern (C) way. > You also get all the problems of unmangled C names, I remember some > nice segfaults in Derelict related to this (fixed some time ago in > derelict.) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply