digitalmars.D.learn - How to use a function without their sources
- Jordi Sayol (3/3) Jan 18 2013 Is there a way to use a function from a static D library without importi...
- Rob T (11/13) Jan 18 2013 Yes you should be able to do it, but not everything can be
- nazriel (15/17) Jan 18 2013 lib.d:
- Maxim Fomin (16/33) Jan 18 2013 ----lib.d----
- nazriel (4/45) Jan 18 2013 Nice!
- Johannes Pfau (8/14) Jan 18 2013 Isn't this documented? I thought it was well known that you can mark D
- nazriel (5/22) Jan 18 2013 I mean, some kind of pointer how to achieve such thing (Question
- Andrej Mitrovic (18/23) Jan 18 2013 That's a *terrible* idea, you are calling a D function using the C
- nazriel (3/17) Jan 18 2013 Oh really?
- nazriel (5/25) Jan 18 2013 As a disclaimer, I did know about this.
- Andrej Mitrovic (4/5) Jan 18 2013 This is D.learn, so people expect to get valid information here and
- nazriel (8/15) Jan 18 2013 It's valid example as long as you objump object file.
- anonymous (5/24) Jan 18 2013 On Friday, 18 January 2013 at 18:50:01 UTC, nazriel wrote:
- Andrej Mitrovic (7/13) Jan 18 2013 That formulation makes no sense. If it's not his library the provider
- nazriel (11/33) Jan 18 2013 Still doesn't answers question from first post.
- Andrej Mitrovic (13/19) Jan 18 2013 You still have to provide .d or .di files, you can't expect the user
- Jordi Sayol (5/22) Jan 20 2013 Thank you for your answer.
- Andrej Mitrovic (5/7) Jan 20 2013 extern(D) is the default linkage for all functions. D has its own
- Johannes Pfau (10/21) Jan 18 2013 I think this is dangerous, there's no guarantee that extern(D) equals
- Jordi Sayol (5/29) Jan 18 2013 Thanks!
- nazriel (10/38) Jan 18 2013 I shouldn't paste this code in first place.
Is there a way to use a function from a static D library without importing their D sources nor their DI interface? -- Jordi Sayol
Jan 18 2013
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
Jan 18 2013
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.
Jan 18 2013
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:----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(); } -------------- This is possible if inside library non-member functions are marked as extern(C). Otherwise a .di file is needed.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.
Jan 18 2013
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:Nice! This should be mentioned at Language Reference, so it won't get lost.On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:----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(); } -------------- This is possible if inside library non-member functions are marked as extern(C). Otherwise a .di file is needed.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.
Jan 18 2013
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.)
Jan 18 2013
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>: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.[...] 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.)
Jan 18 2013
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.dobject.Error: Access Violation
Jan 18 2013
On Friday, 18 January 2013 at 18:18:07 UTC, Andrej Mitrovic wrote:On 1/18/13, nazriel <spam dzfl.pl> wrote:Oh really? Thanks, I didn't know.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.:
Jan 18 2013
On Friday, 18 January 2013 at 18:23:57 UTC, nazriel wrote:On Friday, 18 January 2013 at 18:18:07 UTC, Andrej Mitrovic wrote:As a disclaimer, I did know about this. I wasn't serious by posting this example. So don't take this serious. I just used objdump to get mangled name. ^^On 1/18/13, nazriel <spam dzfl.pl> wrote:Oh really? Thanks, I didn't know.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.:
Jan 18 2013
On 1/18/13, nazriel <spam dzfl.pl> wrote:So don't take this serious.This is D.learn, so people expect to get valid information here and they don't know if you're showing an invalid example or not unless you tell them.
Jan 18 2013
On Friday, 18 January 2013 at 18:44:29 UTC, Andrej Mitrovic wrote:On 1/18/13, nazriel <spam dzfl.pl> wrote:It's valid example as long as you objump object file. Show me a working solution to question from first post. "How to use existing static D library *WITHOUT* using .DI files". You can't edit library itself, so adding extern(C) to functions won't work. You can't create .DI file - which is the main question from first post.So don't take this serious.This is D.learn, so people expect to get valid information here and they don't know if you're showing an invalid example or not unless you tell them.
Jan 18 2013
On Friday, 18 January 2013 at 17:47:42 UTC, nazriel wrote:lib.d: extern(C) void printf(const char*, ...); void foo() { printf("%s".ptr, "hi".ptr); } test.d: extern(C) void _D3lib3fooFZv(); void main() { _D3lib3fooFZv(); }On Friday, 18 January 2013 at 18:18:07 UTC, Andrej Mitrovic wrote: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).On Friday, 18 January 2013 at 18:50:01 UTC, nazriel wrote:It's valid example as long as you objump object file.It's a bad example. You just got lucky in that the calling conventions match in that particular case.
Jan 18 2013
On 1/18/13, nazriel <spam dzfl.pl> wrote:Show me a working solution to question from first post. "How to use existing static D library *WITHOUT* using .DI files". You can't edit library itself, so adding extern(C) to functions won't work. You can't create .DI file - which is the main question from first post.That formulation makes no sense. If it's not his library the provider will either give the .d files or autogenerated/handwritten .di files. He won't just get a naked static library without supporting files. And if he controls the library, he will either have to autogenerate .di files, or handwrite them. Even if he uses extern(C) he will still have to provide a .d file with all extern(C) functions and types.
Jan 18 2013
On Friday, 18 January 2013 at 20:33:04 UTC, Andrej Mitrovic wrote:On 1/18/13, nazriel <spam dzfl.pl> wrote:Still doesn't answers question from first post. So let me quote it for you: "Is there a way to use a function from a static D library without importing their D sources nor their DI interface?" There were 2 types of answers in this topic: 1) Yes, you can if functions in library are marked as extern(C) 2) Yes, you can even if functions are not marked as extern(C) with little hack which is dumping object file. Whatever you think it is *terrible* idea or not, it seems to be the only one working idea in this, specific scenario.Show me a working solution to question from first post. "How to use existing static D library *WITHOUT* using .DI files". You can't edit library itself, so adding extern(C) to functions won't work. You can't create .DI file - which is the main question from first post.That formulation makes no sense. If it's not his library the provider will either give the .d files or autogenerated/handwritten .di files. He won't just get a naked static library without supporting files. And if he controls the library, he will either have to autogenerate .di files, or handwrite them. Even if he uses extern(C) he will still have to provide a .d file with all extern(C) functions and types.
Jan 18 2013
On 1/18/13, nazriel <spam dzfl.pl> wrote:There were 2 types of answers in this topic: 1) Yes, you can if functions in library are marked as extern(C)You still have to provide .d or .di files, you can't expect the user to have to manually write extern declarations.2) Yes, you can even if functions are not marked as extern(C) with little hack which is dumping object file.That little hack is not very reliable. And once Pull 1085 is merged that technique will become completely unreliable. (https://github.com/D-Programming-Language/dmd/pull/1085).Whatever you think it is *terrible* idea or not, it seems to be the only one working idea in this, specific scenario.The terrible idea is using the mangled representation of a D function but calling it as an extern(C) function. This will simply not work properly. Using extern(C) properly is fine, of course. However even that is problematic because extern(C) does not allow you to overload functions and you'll have 64bit breakage due to http://d.puremagic.com/issues/show_bug.cgi?id=5570.
Jan 18 2013
Al 18/01/13 21:32, En/na Andrej Mitrovic ha escrit:On 1/18/13, nazriel <spam dzfl.pl> wrote:Thank you for your answer. What's the sense for extern(D)? and in which case is useful. -- Jordi SayolShow me a working solution to question from first post. "How to use existing static D library *WITHOUT* using .DI files". You can't edit library itself, so adding extern(C) to functions won't work. You can't create .DI file - which is the main question from first post.That formulation makes no sense. If it's not his library the provider will either give the .d files or autogenerated/handwritten .di files. He won't just get a naked static library without supporting files. And if he controls the library, he will either have to autogenerate .di files, or handwrite them. Even if he uses extern(C) he will still have to provide a .d file with all extern(C) functions and types.
Jan 20 2013
On 1/20/13, Jordi Sayol <g.sayol yahoo.es> wrote:Thank you for your answer. What's the sense for extern(D)? and in which case is useful.extern(D) is the default linkage for all functions. D has its own linkage by default. The reason why is probably to allow the compiler to optimize the way the functions are called without breaking compatibility with C.
Jan 20 2013
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();
Jan 18 2013
Al 18/01/13 18:47, En/na nazriel ha escrit:On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:Thanks! Why it works with "extern(C)", but not works with "extern(D)"? -- Jordi SayolIs 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.
Jan 18 2013
On Friday, 18 January 2013 at 18:34:24 UTC, Jordi Sayol wrote:Al 18/01/13 18:47, En/na nazriel ha escrit:I shouldn't paste this code in first place. As far as I know and other folks mentioned there is no "clean" way to call function from D library without using .DI files or marking function in library as extern (C). You can always look up symbol table like I did for this example. Summary: If you write library - you can mark function as extern (C) to call it later from other app without .DI fil. You need to create .DI file if you use someone else library.On Friday, 18 January 2013 at 17:02:51 UTC, Jordi Sayol wrote:Thanks! Why it works with "extern(C)", but not works with "extern(D)"?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.
Jan 18 2013