www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking from source-code

reply "Andrea Fontana" <nospam example.com> writes:
Is there a way to define a library to link inside d source? I 
can't find anything about it.

IMHO it would very useful for deimos libraries or curl. So when 
you import a library, it has embedded the linking instruction.

For import we write:

import std.stdio;

That means "more or less": look for std/stdio.d inside search 
directories.

Why not then:

import_lib curl;  // or other kw

It should mean link libcurl on linux, curl.dll on windows and so 
on.

Running code with rdmd will not need any extra configuration.
Using version we can link right library (debug/release/etc).

Writing:

dmd hello_world.d -L-lcurl

sounds me like we have to write:
dmd hello_world.d -I"std/stdio.d" -I"std/curl.d" -L-lcurl

To import "headers"
Nov 14 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/14/2013 03:43 AM, Andrea Fontana wrote:
 Is there a way to define a library to link inside d source? I can't find
 anything about it.
I have never used it but there is pragma(lib): http://dlang.org/pragma.html Ali
Nov 14 2013
next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
On Thursday, 14 November 2013 at 11:46:19 UTC, Ali Çehreli wrote:
 On 11/14/2013 03:43 AM, Andrea Fontana wrote:
 Is there a way to define a library to link inside d source? I 
 can't find
 anything about it.
I have never used it but there is pragma(lib): http://dlang.org/pragma.html Ali
Hmmm. I see there's a pragma("lib", "curl") inside std.net.curl. but look at simple test: import std.net.curl; import std.stdio; void main() { get("dlang.org").writeln; } rdmd test.d -> linking error on curl dmd test.d -> linking error on curl dmd test.d -L-lcurl -L-lphobos2 -> works So I though this function didn't exist. Why doesn't it work?
Nov 14 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-11-14 13:12, Andrea Fontana wrote:

 So I though this function didn't exist. Why doesn't it work?
I doesn't work when D modules are used like header files. See: https://d.puremagic.com/issues/show_bug.cgi?id=2776 Another idea would be to use dub: http://code.dlang.org/ -- /Jacob Carlborg
Nov 14 2013
parent reply "Andrea Fontana" <nospam example.com> writes:
On Thursday, 14 November 2013 at 12:35:16 UTC, Jacob Carlborg 
wrote:
 On 2013-11-14 13:12, Andrea Fontana wrote:

 So I though this function didn't exist. Why doesn't it work?
I doesn't work when D modules are used like header files. See: https://d.puremagic.com/issues/show_bug.cgi?id=2776 Another idea would be to use dub: http://code.dlang.org/
I think it's a better idea to make it works from dmd/rdmd. It should be a (working) language feature. It makes no sense for me to use a third party tool for this. Have I to use dub to compile a single-file-project that use just curl or a simple library? If implemented inside language, all compiler should support it, and IDE just support it automagically :) It would be cool to just write rdmd main.d ignoring dependencies hell :) BTW: you say "used like header files". You mean files that are not "compiled" but "imported"?
Nov 14 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 14 November 2013 at 13:49:02 UTC, Andrea Fontana 
wrote:
 I think it's a better idea to make it works from dmd/rdmd.
pragma(lib) works almost everywhere with dmd*.... except phobos, due to how that is compiled. rdmd ignores modules in the std namespace when building the dependency list, so dmd only sees the std.net.curl as a header import, and doesn't pass on the library. If you did dmd main.d dmd2/src/phobos/std/net/curl.d, it would work, but not just dmd main.d where it finds phobos automatically. * pragma(lib) doesn't work on ldc and gdc last i heard though. But, I put the blame here on phobos being compiled as a .lib without curl built in.
Nov 14 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-11-14 14:49, Andrea Fontana wrote:

 BTW: you say "used like header files". You mean files that are not
 "compiled" but "imported"?
Yes. When you have a pre-compiled library and need to declarations. -- /Jacob Carlborg
Nov 15 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-11-14 12:46, Ali Çehreli wrote:

 I have never used it but there is pragma(lib):

    http://dlang.org/pragma.html
Unfortunately that doesn't work with .di files. Although it might work for Deimos projects. -- /Jacob Carlborg
Nov 14 2013